Outside the New York Stock Exchange in New York City

A Wall Street sign is seen outside the New York Stock Exchange in New York City Jan. 3, 2019.

NEW YORK — Wall Street gained ground on Friday in a broad-based rally as investors were heartened by news that Washington would move to temporarily end the longest U.S. government shutdown in history.

All three major U.S. stock indexes advanced, with the Dow and the Nasdaq eking out their fifth straight weekly gains. But the S&P 500 posted its first weekly loss of the year, snapping a four-week run.

The indexes backed off their highs after President Donald Trump confirmed he and lawmakers agreed to advance a three-week stopgap spending plan to reopen the government.

Investor sentiment had faltered in recent days in the face of revived jitters related to the shutdown and the prolonged U.S.-China tariff spat.

“As some of these uncertainties in the market start to diminish we’ll get a clearer picture as to where things are headed,” said Charlie Ripley, senior market strategist for Allianz Investment Management in Minneapolis. “And today’s news of the ending of the government shutdown certainly alleviates some of that overhang.”

“But most likely some uncertainty will linger as this is only a temporary measure to fund the government for now,” Ripley added.

Among these uncertainties, the ongoing trade dispute between the United States and China continues to worry investors.

With the World Economic Forum in Davos, Switzerland, nearing its conclusion, business leaders have expressed worries over the tariff battles, saying they are “fed up” with Trump’s policies.

An escalation of the U.S.-China trade war would sharpen the global economic slowdown already under way, according to a Reuters poll of hundreds of economists worldwide.

Fourth-quarter corporate earnings season is in high gear, with more than 22 percent of S&P 500 companies having reported. Of those, 72.3 percent have beaten analyst expectations.

The Dow Jones Industrial Average rose 183.96 points, or 0.75 percent, to 24,737.2, the S&P 500 gained 22.43 points, or 0.85 percent, to 2,664.76 and the Nasdaq Composite added 91.40 points, or 1.29 percent, to 7,164.86.