Stocks climbed for the second day in a row at the end of a brutal month for the global market.
Investors applauded strong quarterly results from companies including Facebook and General Motors, but US stocks still finished with their worst monthly loss in seven years.
Markets in Europe, Asia and the US rallied following better-than-expected results from various companies and continued hiring by US businesses.
Many of the biggest gains on Wednesday came from technology and internet companies and retailers, which plunged early in October as investors worried about rising interest rates and the US-China trade dispute.
The S&P 500 had not risen for two consecutive days since late September.
It finished October with a loss of 6.9%, its worst since September 2011.
The third quarter of this year was the best in five years for US stocks, but those gains were wiped out this month.
The S&P 500 is now up 1.4% for the year.
Stocks began sinking on October 3 as interest rates rocketed higher.
Even after those gains eased, investors kept selling stocks as they worried about the trade dispute and other factors that could also hurt economic growth and company profits.
Investors are that much more nervous because corporate profit growth is already expected to slow in 2019 after it jumped this year, a big portion of which stemmed from the one-time corporate tax cut.
Schroders Investment Strategist Marina Severinovsky said several different factors could help stocks over the next few weeks: corporate stock purchases are expected to increase, and US President Donald Trump and China’s Xi Jinping could meet next month, an opportunity for progress in US-China trade talks.
“If there’s any kind of movement, even a stay of execution (on tariff hikes), could be a positive for the market,” she said.
Mrs Severinovsky added that whatever the outcome of next week’s midterm elections, stocks will probably rise once they are over.
“Markets tend to rally on certainty,” she said.
The S&P 500 index rose 29.11 points, or 1.1%, to 2,711.74.
The Dow Jones Industrial Average gained 241.12 points, or 1%, to 25,115.76.
The Nasdaq composite jumped 144.25 points, or 2%, to 7,305.90.
The Russell 2000 index of smaller companies edged up 4.78 points, or 0.3%, to 1,511.41.
Smaller and more US-focused companies did even worse than the rest of the stock market in October.
Facebook had a mixed third quarter, with better-than-expected earnings and disappointing revenue.
But after the company’s recent losses, even that was a relief to Wall Street.
After a 2.9% gain on Tuesday, the stock rose 3.8% to 151.79 dollars.