On Tuesday, stocks made their biggest gains since March as technology companies like Apple and Microsoft soared.

Homebuilders also climbed after the government said sales of new homes reached an eight-year high last month. That was a sign the housing market and the broader economy are still in good shape.

Stocks opened higher after hefty gains in Europe. Technology stocks made their biggest gain in almost three months, erasing their losses from earlier this year. Banks rose as interest rates continued to inch higher, which lets banks make more money on lending.

Stocks have alternated between gains and losses in recent days after a four-week string of losses.

“A little bit of good data has reminded people that things are actually O.K.,” said David Lefkowitz, senior equity strategist at UBS Wealth Management. “It’s almost like a rubber band. When things get too stretched, they snap back.”

The Standard & Poor’s 500-stock index picked up 28.02 points, or 1.4 percent, to rise to 2,076.06. The Nasdaq composite index surged 95.27 points, or 2 percent, to 4,861.06.

The Dow Jones industrial average rose 213.12 points, or 1.2 percent, to 17,706.05.

Tech stocks led the market higher with their biggest jump since March 1. Apple picked up $1.47, or 1.5 percent, to $97.90 and Alphabet, the parent company of Google, added $15.78, or 2.2 percent, to $733.03. Microsoft rose $1.56, or 3.1 percent, to $51.59.

Homebuilding stocks jumped after the Commerce Department said sales of new homes had reached their highest level since January 2008. Sales of both newly built and previously occupied homes grew as job gains and low mortgage rates encouraged Americans to keep buying homes.

Interactive Feature | The Dow Minute by Minute

Toll Brothers also reported better first-quarter results than analysts expected, and the company raised its annual projections for home prices and sales. The stock gained $2.36, or 8.7 percent, to $29.46.

Beazer Homes USA added 66 cents, or 9.2 percent, to $7.86, and PulteGroup rose 91 cents, or 5.1 percent, to $18.73.

Bond prices fell. The yield on the 10-year Treasury note rose to 1.86 percent from 1.84 percent.

JPMorgan Chase climbed $1.08, or 1.7 percent, to $64.54, and Bank of America gained 21 cents, or 1.6 percent, to $14.68.

The agribusiness giant Monsanto rejected an offer from the German company Bayer worth $62 billion, or $122 a share. Monsanto, however, said it was open to talking with Bayer about a possible sale. As investors hoped for a richer offer, Monsanto stock rose $3.30, or 3.1 percent, to $109.30.

The streaming video company Netflix jumped after it said it had struck a deal with Disney. Starting in September, Netflix will have exclusive domestic rights to new movies from Disney, Marvel, Lucasfilm and Pixar. Netflix stock jumped $3, or 3.2 percent, to $97.89.

The fertilizer maker CF Industries ended a deal to buy OCI’s distribution networks for about $8 billion. CF planned to reincorporate in Britain as part of the deal, which would have reduced its tax bill, but the company said new rules from the Treasury Department made the combination less appealing. CF Industries will pay OCI $150 million for calling off the deal. CF Industries shed $2.24, or 7.5 percent, to $27.61.

Oil traded at its highest price since early October, and benchmark United States crude picked up 54 cents, or 1.1 percent, to $48.62 a barrel in New York. Brent crude, used to price international oils, rose 26 cents, or 0.5 percent, to $48.61 a barrel in London.

Interactive Feature | 2-Year Treasury Notes

Retailers continued to struggle. The electronics chain Best Buy said its quarterly sales kept falling and its outlook was weak. That made Best Buy the latest retailer to disclose disappointing quarterly results. Its stock lost $2.45, or 7.4 percent, to $30.55.

The shoe and accessories retailer DSW cut its outlook, saying it expects weaker sales this year. The company reported disappointing results for its first fiscal quarter, and its stock gave up $2.53, or 11.6 percent, to $19.20.

The athletic apparel maker Under Armour rose after it announced a deal with the University of California, Los Angeles worth $280 million over 15 years. The stock jumped 95 cents, or 2.5 percent, to $38.22.

Twitter announced a series of format changes to make its 140-character limit a bit more flexible. While that might make the platform more appealing to new users, Twitter did not abolish that limit entirely, as some had expected. Already trading around record lows, its stock declined 38 cents, or 2.6 percent, to $14.03.

The CAC 40 in France added 2.5 percent while the German DAX gained 2.2 percent. In Britain, the FTSE 100 rose 1.3 percent. Japan’s benchmark, the Nikkei 225, fell 0.9 percent as the yen continued to strengthen, hurting Japanese exporters. The South Korean Kospi edged down 0.9 percent. In Hong Kong, the Hang Seng rose 0.1 percent.

In other energy trading, wholesale gasoline gained 1 cent, to $1.65 a gallon. Heating oil rose 1 cent, to $1.49 a gallon. Natural gas fell 8 cents, to $1.98 per 1,000 cubic feet.

Gold fell $22.30, or 1.8 percent, to $1,229.20 an ounce. Silver slid 17 cents, or 1 percent, to $16.25 an ounce. Copper was unchanged at $2.07 a pound.

The dollar rose to 109.98 yen from 109.19 yen. The euro slipped to $1.1143 from $1.1221.