NEW YORK — Wall Street capped a turbulent week with a late-day rally Friday after shaking off an early slump triggered by the latest escalation in the trade war between the United States and China.

The market fell sharply in the early going after the U.S. raised tariffs on $200 billion worth of Chinese goods when negotiators failed to reach a deal. Hours later, remarks from President Donald Trump and Treasury Secretary Steven Mnuchin gave investors reason for optimism.

First, Mnuchin told CNBC that the trade talks had been "constructive," which spurred the market's rebound. Then, in a late-afternoon tweet, Trump suggested the tariffs could be removed and that the trade talks "will continue."

The afternoon pickup led to a broad reversal in the market, leaving only health care stocks with a loss. Still, the buying did little to blunt the overall sharp decline for stocks this week. The benchmark S&P 500 index finished with its worst weekly loss of the year, 2.2 percent.

"Nobody wants to sell too aggressively just in case things get settled and the market rallies," said J.J. Kinahan, chief market strategist for TD Ameritrade. "As long as they're still talking there's a chance that this gets done."

The S&P 500 index rose 10.68 points, or 0.4 percent, to 2,881.40. The broad index, which earlier had been down 1.6 percent, has given back much of the gains it made in April. It's still up 14.9 percent for the year.

The Dow Jones Industrial Average gained 114.01 points, or 0.4 percent, to 25,942.37. It was down as much as 358 earlier. The Nasdaq added 6.35 points, or 0.1 percent, to 7,916.94. The technology heavy index rebounded after having been down as much as 1.9 percent earlier.

The Russell 2000 index of small company stocks also closed higher after being down much of the day. It picked up 2.94 points, or 0.2 percent, to 1,572.99. Major indexes in Europe closed mostly higher.

Bond prices fell. The yield on the 10-year Treasury note rose to 2.47 percent from 2.45 percent late Thursday.

The higher tariffs from the United States and China's response that it would take "necessary countermeasures" rattled investors Friday who had been hoping for a quick resolution to the dispute. Confidence in that outcome had eased investors' concerns this year, along with a more patient Federal Reserve and solid economic data. It all added up to help push stocks to their hottest start to a year in decades.

The trade war has stressed consumers and companies with higher costs on goods. The latest tariff increase raises tariffs from 10 percent to 25 percent on $200 billion of Chinese imports. Trump has signaled that he might expand penalties to all Chinese goods shipped to the U.S.

The reaction in the stock market this week has been sharp, but even after this week's tumult, the S&P 500 index remains within 2.2 percent of its record set on April 30.

That's because many investors continue to expect the United States and China to come to an agreement eventually, said Anthony Saglimbene, global market strategist at Ameriprise Financial. Neither country would benefit from not getting a deal, he said. In the meantime, the U.S. job market continues to grow, and balance sheets for American households remain better than before the Great Recession.

"We have advised long term investors to look through the noise of the next few weeks and what goes on with trade because the economy is strong and earnings should grow better than expected," Saglimbene said. "I wouldn't expect the market would go down 5 or 10 percent just because we put these tariffs on. I would expect it would decline 5 or 10 percent if the trade tensions are escalating."

Things, though, could get dicier not only if the U.S.-China talks break down but also if the trade war intensifies on other fronts. The United States may be nearing a decision on whether to impose tariffs on imports of European automobiles, for example.

The market's gains this year had been slow but steady up until this week. Prior to this week, the S&P 500 only had four losing weeks this year, most of them minor. Other than that, it's been mostly up amid a mostly muted year with no major market-moving news. Investors have been cautiously watching corporate earnings, and have been mostly surprised by solid results, though several big companies, including Mylan, TripAdvisor and Wynn Resorts fell sharply after disclosing disappointing earnings or outlooks.