Some days, everything seems to go right.

Stocks rose on Friday, as a burst of optimism on several fronts gave the S&P 500 its first weekly gain after three weeks of decline.

The rally marked a change in mood, after weeks of dour sentiment among investors that had been fueled by a growing list of threats to the health of the economy in the United States and around the world.

Foremost among them had been the toll of the trade battle between China and the United States. In recent weeks, the trade war seemed likely to become something of a frozen conflict, with signs of intransigence on both sides.

But on Friday, President Trump was meeting with Vice Premier Liu He of China at the White House and investors bid up stock prices in anticipation that some kind of an agreement between the two sides would be reached.