Energy companies powered to big gains on Wednesday, leading the broader US stock market higher, after reports Opec nations were moving closer to an agreement to cut oil production.
Stocks switched between gains and losses for most of the day and most industries did not move much.
However, energy firms surged on Wednesday afternoon only hours before Opec confirmed a preliminary agreement had been reached to curb oil production.
"It just creates a lot of optimism that the worst is over for investors," said Brian Youngberg, energy analyst at Edward Jones.
The Dow Jones industrial average rose 110.94 points, or 0.6%, to 18,339.24.
The Standard & Poor's 500 index added 11.44 points, or 0.5%, to 2,171.37. The Nasdaq composite edged up 12.84 points, or 0.2%, to 5,138.55.
A little more than two years ago, a barrel of oil cost around $100 but a huge supply glut built up as the US and other countries produced more and more oil and the global economy slowed, which hurt demand.
Oil hit a low of $26 a barrel in February and has traded between $40 and $50 a barrel since April, but investors doubt the price will rise further without limits on production. Opec produces more than a third of the world's oil.
Benchmark US crude jumped $2.38, or 5.3%, to $47.05 a barrel in New York. Brent crude, the international standard, rose $2.72, or 5.9%, to $48.69 a barrel in London.
Exxon Mobil picked up $3.66, or 4.4%, to $86.90 and Chevron leaped $3.17, or 3.2%, to $102.15.
Mining and industrial companies also climbed. The Dow was aided by a big gain for heavy machinery maker Caterpillar, which climbed $3.71, or 4.5%, to $86.59.
Phone companies suffered some of the largest declines.
AT&T fell 61c, or 1.5%, to 40.85 dollars (£31.37) after a UBS analyst downgraded the company to "neutral" from "buy".
Analyst John Hodulik said profits will get squeezed as the companies offer trade-in deals to try to win customers. He cut his profit forecast for Verizon, which lost 43c to $52.06.
AT&T has climbed 19% this year and Verizon has risen 13% as investors sought stocks that pay big dividends while bond yields remain low.