NEW YORK — U.S. stocks climbed Wednesday, driven by gains for big technology companies and Amazon. The S&P 500 and Nasdaq finished at record highs for the fourth day in a row.

Stocks have rallied over the last four days as investors have grown more hopeful about trade talks between the U.S., Mexico and Canada.

The Commerce Department said Wednesday that the U.S. economy was a bit stronger than it previously thought. It said gross domestic product grew 4.2 percent in the second quarter. Stronger business investment was a factor, as companies spent more on items such as software.

“Corporate spending is up, which is something that is very important for the overall economy,” said Quincy Krosby, chief market strategist at Prudential Financial.

Technology companies including Apple, Microsoft and Alphabet made strong gains.

The S&P 500 advanced 16.52 points, or 0.6 percent, to 2,914.04. The Dow Jones Industrial Average rose 60.55 points, or 0.2 percent, to 26,124.57. The Nasdaq composite jumped 79.65 points, or 1 percent, to 8,109.69.

The Russell 2000 index of smaller-company stocks added 6.33 points, or 0.4 percent, to 1,734.75. It also closed at a record high.

Technology firms and other large companies started climbing as media reports suggested that a breakthrough on trade might be near. On Monday the White House said it had reached a preliminary deal with Mexico to replace the North American Free Trade Agreement. Mexico is the United States’ third-largest trading partner and Canada is second, behind China.

The S&P 500 has risen 3.5 percent in August after a 3.6 percent gain in July. That two-month gain is its best since late 2015.

Amazon jumped 3.4 percent to $1,998.10 after a Morgan Stanley analyst raised his price target on its stock to $2,500 from $1,850. At that price, Amazon would have a market value of $1.2 trillion.

“We have increasing confidence that Amazon’s rapidly growing, increasingly large, high margin revenue streams (advertising, Amazon Web Services, subscriptions) will drive higher profitability,” Brian Nowak wrote.

Apple became the first publicly traded company to reach the $1 trillion mark earlier this month. Investors currently value the iPhone maker at almost $1.08 trillion to Amazon’s $975 billion.

Other retailers struggled. Dick’s Sporting Goods dipped 2.2 percent to $35.60 after its sales fell short of expectations.

Footwear seller Shoe Carnival surged 13.1 percent to $41.74 after it raised its annual forecasts following a second quarter. The company said back-to-school sales are off to a good start.

Energy companies rose along with oil prices. Benchmark U.S. crude rose 1.4 percent to $69.51 a barrel in New York while Brent crude, used to price international oils, gained 1.6 percent to $77.14 a barrel in London.

Homebuilders fell after the National Association of Realtors said fewer Americans signed contracts to buy homes in July compared with the previous month. High home prices and rising mortgage rates are pushing home sales down even though economic growth is solid.

filed under: