Stocks extended gains for a second day on Tuesday as optimism grew that a solution to the Greek debt problem was near, boosting investor appetite for risky assets.

Buyers snapped up shares that had fallen sharply in the past week, mostly in commodities and technology.

The fact that we won't be seeing the worst-case scenario is the catalyst for the market that is still very oversold, said James Dailey, portfolio manager of TEAM Asset Strategy Fund in Harrisburg, Pennsylvania.

The S&P energy index <.GSPE> surged nearly 2.2 percent, the biggest gainer among S&P sectors. Halliburton Co gained 5.3 percent to $48.66, while Chevron Corp was up 1.2 percent at $100.09.

Brent crude futures rose $2.41 on Tuesday, pushing oil above $108 a barrel as the dollar weakened and optimism grew that Greece would adopt an austerity program.

The Dow Jones industrial average <.DJI> was up 118.64 points, or 0.99 percent, at 12,162.20. The Standard & Poor's 500 Index <.SPX> was up 13.05 points, or 1.02 percent, at 1,293.15. The Nasdaq Composite Index <.IXIC> was up 31.99 points, or 1.19 percent, at 2,720.27.

Dailey said about the S&P 500 stock index that 1,285 to 1,300 was a technical range being closely watched.

Greek lawmakers will vote Wednesday and Thursday on the measures, which must be passed to receive the next payment of

12 billion euros. If Greece doesn't get the funds, investors fear a Europe-wide crisis and potential credit market freeze could follow.

Also helping sentiment, progress was reported in talks to persuade European banks and insurers to voluntarily roll over maturing Greek debt.

VIX AND THE GREEK VOTE

While the market was generally optimistic about the vote, some options gauge suggested otherwise.

The CBOE Volatility Index <.VIX>, Wall Street's fear gauge, was at 19.62, a number considered relatively high.

While the most recent newswires are predicting that the Greek government has the minimum votes needed (151 out of 300) to pass the austerity plan, the relatively small declines in the VIX this week, given how sharply the US equity markets have rallied, seem to be building in at least some doubt that the measure may fail, said Randy Frederick, director of trading and derivatives at Schwab Center for Financial Research in Austin, Texas.

If the vote fails it could cause a rally in the dollar versus the euro and that would likely prompt a pullback in the domestic equity markets. Given a gain in the SPX of approximately 1.8 percent in the past two days, I would expect much more than the 6 percent decline we've seen in the VIX, especially given how high it is relative to the past 3 months or so.

Nike Inc surged 7.7 percent to $87.89 a day after reporting fourth-quarter earnings that beat expectations, while orders suggested robust strength for the future.

(Reporting by Angela Moon, Editing by Kenneth Barry)