Stocks carved a rugged path Wednesday but markets closed with gains, despite early declines in shares of banks and retailers, which would stand to lose the most if the economy slows down.

Chipotle Mexican Grill fell sharply as the company disclosed a federal investigation into its E. coli outbreak, and Yahoo sank as the troubled Internet company announced layoffs and plans to sell businesses.

At the close on Wall Street, the Dow Jones industrial average was up 183.1 points, about 1.1 percent, at 16,336.7. The Standard & Poor’s 500 index gained 9.5 points, about 0.5 percent, to 1,912.5 The Nasdaq composite sank 12.7 points, about 0.3 percent, to 4,504.4.

ENERGY RECHARGE: Crude oil prices recovered some ground after two days of steep losses. On the New York Mercantile Exchange U.S. benchmark crude gained $2.42, about 8.1 percent, to $32.30 a barrel. In London, Brent crude, which is used to price international oils, jumped $2.71, or 8.3 percent, to $35.43 a barrel.

INVESTOR SAFETY: Investors moved money into traditional safe-havens: stocks that pay high dividends, U.S. government bonds, and precious metals.

The Dow Jones utility index, a basket of 15 utility companies, rose 1 percent. That index is up more than 8 percent this year. Utilities and other companies that pay large dividends are popular at times of uncertainty because they provide a regular return and are large, mature businesses that tend to stand up well during economic downturns.

Some traders are taking that a step further.

advertisement | advertise on newsday

SOME IN ALL CASH: “I’ve been telling clients to be in all cash,” said Ian Winer, co-head of equities trading at Wedbush Securities. “There’s too much credit risk out there, S&P 500 earnings could be down this year and it seems an increasing possibility that the U.S. could be in a recession in 2017.”

BANKING ISSUES: Financial stocks were getting another beatdown. Investors worry that lenders could suffer if more energy companies default on their loans. They also think the Federal Reserve might postpone its interest rate increases, which would have helped banks earn more money by raising the rates they charge on loans.