Wall Street capped a day of volatile trading with a late-afternoon buying spree that sent U.S. stock indexes to a mostly higher finish Friday.
Despite the 11th-hour rally, the benchmark S&P 500 index ended with its second weekly loss in four weeks.
Gains in health care and energy companies powered the market higher.
The market got a brief boost after President Donald Trump expressed optimism that the U.S. and China will reach a deal to resolve their costly trade dispute. The remarks came as representatives of both countries have resumed talks.
Large retailers and media and communications companies were the laggards.
"The market and market participants are more unsettled now than they have been in years," said Tom Martin, senior portfolio manager with Globalt Investments. "We're that much further on in the cycle and you have these tariffs and trade wars that are really still in the very early stages."
The S&P 500 index rose 6.07 points, or 0.2 percent, to 2,736.27. The Dow Jones Industrial Average gained 123.95 points, or 0.5 percent, to 25,413.22. The Nasdaq composite slid 11.16 points, or 0.2 percent, to 7,247.87. The Russell 2000 index of smaller companies picked up 3.41 points, or 0.2 percent, to $1,527.53.
The S&P 500, which finished higher for the second straight day, ended the week with a loss of 1.6 percent.
Like much of this week, the market spent much of Friday veering between bouts of listless trading and modest swings.
"Investors are really trying to figure out how they want to be positioned based on the incoming information," Martin said. "It's not surprising to me that at this time of year, given what we've seen, that we're getting the intraday moves we're getting."
One of the day's market swings came as traders reacted to Trump's remarks on trade.
At the White House, speaking about the lingering trade dispute, the president said he hoped the U.S. could make a deal with China.
"I think a deal will be made," Trump said. "We'll find out very soon."
Stocks snapped higher after the remarks were reported, with the Dow briefly jumping as much as 220 points, before pulling back to about where they were beforehand.
Soybean futures spiked after Trump's comments. Soybean prices have fallen sharply since this Spring as the trade dispute with China led to a steep drop in China's purchases of U.S. soybeans. Soybean futures jumped from $8.83 to $8.92 a bushel following the comments. They had traded as high as $10.78 a bushel in early March.
The Trump administration has imposed a 10 percent tariff on $200 billion of Chinese goods over complaints Beijing steals or pressures foreign companies to hand over technology as the price of market access. That tariff is set to rise to 25 percent in January. Another $50 billion of Chinese goods already is subject to 25 percent duties. Beijing has responded with penalty duties on $110 billion of American goods.
Washington and Beijing resumed talks over their spiraling trade dispute this week ahead of a meeting between President Xi Jinping and Trump, China's Commerce Ministry said Thursday.
Health care stocks were among the biggest gainers Friday. Universal Health Services gained 3.9 percent to $133.
Troubled California power provider PG&E surged 37.5 percent to $24.40 after the president of the utility's state regulator said it was essential for a power company to have the financial strength to operate safely. The remark late Thursday by California Public Utilities Commission President Michael Picker appeared to reassure investors concerned the company may face a torrent of costs related to the devastating wildfire in Northern California. There's been speculation that PG&E's equipment may have set off the blaze, which started Nov. 8 and has killed at least 56 people.
Chipmaker Nvidia led a sell-off in technology stocks. The company plunged 18.8 percent to $164.43 after saying it had a large number of unsold chips because of a big drop in mining of cryptocurrencies.
Retailers also weighed on the market. Nordstrom cratered 13.7 percent to $50.93 after the department store issued weak guidance for the full year. That disappointing outlook overshadowed the company's third-quarter results, which topped Wall Street's estimates.
Williams-Sonoma tumbled 11.2 percent to $53.76 after the cookware seller said products were delayed because of shipping congestion out of China ahead of U.S. tariffs.
The price of U.S. crude oil finished flat after a two-day winning streak. Benchmark U.S. crude oil was unchanged at $56.46 a barrel in New York. Brent crude, used to price international oils, gained 0.2 percent to $66.76 a barrel in London. Despite the latest uptick, U.S. crude oil is still down about 13 percent for the month.
The pickup in oil prices helped lift energy stocks. Helmerich & Payne rose 4.2 percent to $62.59.
In other energy trading, heating oil held steady to $2.07 a gallon and wholesale gasoline jumped 1.3 percent to $1.58 a gallon.
Natural gas, which spiked earlier this week amid forecasts calling for a cold snap across much of the Northeast and South, continued to climb Friday, adding 5.8 percent to $4.27 per 1,000 cubic feet. It is now up around 32 percent this month.
Bond prices rose. The 10-year Treasury fell to 3.07 percent from 3.11 percent late Thursday.
The dollar fell to 112.83 yen from 113.58 yen on Thursday. The euro strengthened to $1.1412 from $1.1348. The pound rose to $1.2831 from $1.2791.
The price of gold rose 0.7 percent to $1,223 an ounce. Silver gained 0.8 percent to $14.38 an ounce. Copper climbed 1.9 percent to $2.80 a pound.
Major European stock indexes closed lower as trade tensions and political risks surrounding Britain's exit from the European Union kept investors cautious. Germany's DAX lost 0.1 percent and France's CAC slid 0.2 percent. Britain's FTSE 100 gave up 0.3 percent.
In Asia, Japan's Nikkei 225 index lost 0.6 percent while the Hang Seng in Hong Kong added 0.3 percent. South Korea's Kospi rose 0.2 percent.