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.
Markets have fallen recently because investors are becoming increasingly worried about the U.S. economy.
Shortly after the market opened, the Institute of Supply Management said its index measuring the service sector of the U.S. economy grew inJuly at the weakest pace in 17 months. Economists had expected a slight increase.
The report was the latest sign over the last week that the economy may be slowing. Consumers cut their spending in June for the first time in nearly two years; manufacturing slowed, and the government said that in the first half of the year the economy grew at its slowest pace since the recession ended in June 2009.
"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.
The Dow, the Standard & Poor's 500 index and Nasdaq were down more than 1 percent earlier in the day, but edged higher during the afternoon. The Dow Jones industrial average finished with a gain of 0.3 percent, to 11,896.44. The S&P 500 index rose 6.29, or 0.5 percent, to 1,260.34. The S&P had been down for seven straight days through Tuesday. It is up 0.2 percent for the year after being down 0.3 for the year Tuesday.
The Nasdaq composite added 23.83, or 0.9 percent, to 2,693.07.
The broad S&P 500 index-- the index followed by most professional money managers and U.S. mutual funds -- rose after it hit a low for the year of 1,234. Some investors saw it as an opportunity to buy the S&P 500 index. As a whole, companies in the index are expected to have record profits this year.
Some of those gains might also be due to automatic buying triggered when an index reaches a certain level. Many traders use computer programs that buy or sell stocks once they break through their long-term averages.
"It seems like the early money was based on fear, and the market climbed back as computer-program trading took over," said Mark Lamkin, the head of Lamkin Wealth Management in Louisville, Ky.