(Reuters) - Wall Street climbed on Wednesday, boosted by energy shares and helped by news of an agreement to extend the debt limit, as stocks bounced back from a day-earlier selloff.

U.S. stocks built on moderate gains after news that President Donald Trump, siding with Democrats over his fellow Republicans, said he agreed to pass an extension of the U.S. debt limit until Dec. 15, potentially avoiding an unprecedented default on U.S. government debt.

Data showed U.S. services sector activity accelerated in August amid strong gains in new orders and employment, while another report showed only a modest rise in the trade deficit in July - the latest signs that the economy had gathered momentum early in the third quarter.

A Federal Reserve survey showed the U.S. economy expanded at a modest to moderate pace in July through mid-August.

The S&P 500 on Tuesday had suffered its biggest single-day decline in nearly three weeks amid fresh tensions involving North Korea and a second powerful hurricane bearing down on the U.S. south.

“You have opposing forces kind of keeping the market from breaking out to a new high, but yet the fundamental data seem to be keeping it from breaking down and selling off significantly,” said Walter Todd, chief investment officer of Greenwood Capital in Greenwood, South Carolina.

“You kind of have these opposing forces pushing on each other and keeping the market in a very narrow range,” Todd said.

Traders work on the floor of the New York Stock Exchange (NYSE) in New York, U.S., August 25, 2017. REUTERS/Brendan McDermid

The Dow Jones Industrial Average .DJI rose 54.33 points, or 0.25 percent, to 21,807.64, the S&P 500 .SPX gained 7.69 points, or 0.31 percent, to 2,465.54 and the Nasdaq Composite .IXIC added 17.74 points, or 0.28 percent, to 6,393.31.

Despite Tuesday’s fall, the benchmark S&P 500 ended on Wednesday within 15 points of its record closing high.

“The natural trend in the market is to run scared at the first sign of trouble and then to bounce back,” said Brad McMillan, chief investment officer for Commonwealth Financial Network in Waltham, Massachusetts. “Once investors had a chance to digest some of the events, they realized that from a domestic point of view, it simply isn’t that bad.”

The energy sector .SPNY rose 1.6 percent, for its biggest single-day gain in two months, as oil prices rose. Strong global refining margins and the reopening of U.S. Gulf Coast refineries provided a more bullish outlook after sharp drops due to Hurricane Harvey.

Two-percent gains for shares of oil majors Exxon Mobil XOM.N and Chevron CVX.N supported the S&P 500 and the Dow.

Financials .SPSY climbed 0.2 percent a day after their largest one-day drop since mid May. Nine of 11 major sectors finished in positive territory.

Investors were digesting news that Fed Vice Chair Stanley Fischer said he would step down from his position in mid-October, potentially accelerating Trump’s opportunity to reshape the direction of the central bank.

AT&T T.N shares fell 1.4 percent and Verizon VZ.N slid 1.0 percent, dragging on the S&P. Rival T-Mobile US TMUS.O said it will offer a free subscription to video streaming service Netflix NFLX.O with its unlimited data family plans.

Advancing issues outnumbered declining ones on the NYSE by a 1.65-to-1 ratio; on Nasdaq, a 1.27-to-1 ratio favored advancers.

About 6.3 billion shares changed hands in U.S. exchanges, above the 5.8 billion daily average over the last 20 sessions.