NEW YORK — Stock indexes came back from deep losses in the morning and ended Wednesday with small gains. The Dow Jones industrial average avoided its longest losing streak since Jimmy Carter was president.

The Dow rose 30 points — after being down 166 — to break an eight-day losing streak. Nine days would have been the longest since February 1978. The S&P 500 index rose 6 points and broke a seven-day streak.

Shortly after the market opened, the Institute of Supply Management said its index measuring the service sector grew in July at the weakest pace in 17 months.

“There has been too much at the same time for investors to hang in there and you’re starting to see some element of panic finally showing up,” said Andrew Goldberg, U.S. market strategist at JP Morgan Funds.