Stocks rose on Tuesday, with the Dow briefly topping 13,000 for the first time since May 2008 after Greece secured a bailout to avoid a March default, but gains were limited as investors had priced in expectations of a deal.

Signs of improvement in the economy and stabilization of Europe's debt crisis have driven the Dow more than 20 percent since late last year, while the S&P has climbed more than 8 percent so far this year.

Euro zone finance ministers agreed on a 130-billion-euro ($172 billion) rescue for Greece to avert an imminent chaotic default after forcing Athens to commit to unpopular cuts and private bondholders to take bigger losses.

This is the most solid agreement Greece has had, with actual money behind it, and that makes the market optimistic, said Phil Flynn, senior market analyst at PFG Best in Chicago.

Even with the bailout, Greece faces a long road to economic recovery. European Union officials said the Greek economy will only return to growth in 2014 after a recession that will shrink output by 17 percent.

If you say this is definitely the end of the story, then that shows you're not familiar with the history of the issue, Flynn said. We're cautious optimistic, but we're not likely to move significantly higher from this point since we've rallied going into it.

The Dow Jones industrial average <.DJI> was up 47.72 points, or 0.37 percent, at 12,997.59. The Standard & Poor's 500 Index <.SPX> added 5.46 points, or 0.40 percent, at 1,366.69. The Nasdaq Composite Index <.IXIC> rose 10.60 points, or 0.36 percent, at 2,962.38.

Wal-Mart Stores Inc lost 3.8 percent to $60.07 and was the top drag on both the Dow and S&P after its quarterly profit came in short of expectations.

Home Depot Inc climbed 1.6 percent to $47.44 after the home improvement chain's quarterly profit beat estimates.

Macy's Inc climbed 4.7 percent to $37.98 as the department store group posted higher profit and forecast more sales gains this year.

The Morgan Stanley retail index <.MVR> rose 0.2 percent. The S&P retail index <.RLX>, which excludes Wal-Mart, gained 0.8 percent.

Kraft Foods Inc advanced 1.7 percent to $38.66 after the foodmaker forecast earnings growth of at least 9 percent this year even as it prunes its portfolio of North American brands. Kraft, Wal-Mart and Home Depot are all Dow components.

Apple Inc was up 2.3 percent to $513.52 after the U.S. International Trade Commission ruled the iPhone maker did not infringe patented technology owned by Android phonemaker HTC Corp <2498.TW>.

Dell Inc and Intuit Inc were due to report results later Tuesday.

Earnings season continued to wind down this week, with 59 companies scheduled to report. According to Thomson Reuters data through Tuesday morning, of the 418 companies in the S&P 500 that have reported earnings, 64 percent have topped analyst expectations.

(Reporting By Ryan Vlastelica; editing by Jeffrey Benkoe)