U.S. stocks advanced on investor optimism over the economy, driving the Dow Jones above 13,000 for the first time in nearly four years on Tuesday, and the euro steadied after earlier gaining on news of a long-awaited bailout for Greece.

The euro rose, retreated and then traded near break-even against the dollar after Europe finally sealed a rescue deal for Greece, prompting investors to pare positions against the single currency despite doubts about the deal's implementation.

The size of the Greek economy, however, pales when compared with that of the United States, the world's largest, which continues to surprise on the upside and bolster sentiment.

As it's become more apparent that the U.S. economy is recovering, people have reduced their concerns about Europe and focused much more on prospects here in the U.S., said Rick Meckler, president of hedge fund LibertyView Capital Management in Jersey City, New Jersey.

Like any crisis that goes on and on investors start to live with it. Greece is not dominating the news anymore, he said.

The broad S&P 500 index is up about 8.6 percent so far this year and the Dow is up about 6.4 percent, with the two indexes up more than 25 percent since their lows in early October.

Fourth-quarter earnings continue to beat expectations, albeit at a lower rate than recent quarters. Of the 418 companies in the S&P 500 that have reported, 64 percent have topped analyst expectations, according to Thomson Reuters data.

The Dow Jones industrial average <.DJI> was up 48.33 points, or 0.37 percent, at 12,998.20. The Standard & Poor's 500 Index <.SPX> was up 5.27 points, or 0.39 percent, at 1,366.50. The Nasdaq Composite Index <.IXIC> was up 5.01 points, or 0.17 percent, at 2,956.79.

Wal-Mart Stores Inc , the world's largest retailer, was the biggest drag on both the Dow and the S&P after reporting quarterly earnings that fell short of expectations. Its shares slumped 4.0 percent to $59.97.

On the positive side, department store operator Macy's Inc rose 2 percent to $36.98 after reporting a higher profit and forecasting more sales gains this year.

European shares ended lower in thin volume, with investors cashing in on recent highs, after the bailout failed to soothe concerns about the future of the euro zone's most troubled country.

While the agreement averted the danger of a disorderly Greek default, the country faces further political and economic hurdles.

The FTSEurofirst 300 index <.FTEU3> of pan-European shares fell 0.5 percent to 1,085.38 points, retreating after a two-day winning streak fuelled by expectations the Greek bailout deal was imminent.

The euro rose against the dollar after investors pared their bets against the single currency. The euro traded up 0.1 percent at $1.3257. Earlier, it hit a session high of $1.3292 in the overnight session after the Greek bailout deal was reached.

Being short the euro is a stale position right now, said Douglas Borthwick, managing director and head of trading at Faros Trading in Stamford, Connecticut. Many had questioned whether or not Greece would stay in the EUR, but last night's decisions were a resounding vote of yes.

After 13 hours of talks, euro zone ministers agreed on the Greek deal early Tuesday by forcing Athens to commit to unpopular budget cutbacks and getting private bondholders to accept deeper losses on their holdings.

The MSCI world equity index <.MIWD00000PUS> slipped by 0.1 percent after the Greek bailout but is still more than 10 percent higher for the year to date.

U.S. Treasury prices fell after the Greek bailout dented appetite for safe-haven assets. However, concern over how Athens implements the painful austerity measures moderated losses.

The benchmark 10-year U.S. Treasury note was down 22/32 in price to yield 2.08 percent.

While the bailout for Greece averts a disorderly default next month, which potentially could have disrupted financial markets worldwide, it left major doubts over the prospects for implementation given looming elections in April and rising social unrest on the streets of Athens.

Greece is increasingly trapped in a vicious circle where ever more austerity comes with an ever higher price tag on growth. Consequently, implementation risk will remain high, analysts at French bank Societe Generale said in a note.

Oil held close to $120 a barrel as Iran's top customers in Asia moved to cut back on supplies due to tighter sanctions by the West over Iran's disputed nuclear program, which the West says is aimed at building bombs, something that Iran denies.

Cuts in Asia are in addition to voluntary cutbacks by Iran's clients in Europe ahead of a July 1 European Union ban on Tehran's oil. As a result, Iran could be forced to re-route up to 500,000 barrels per day from March, trading sources say.

Benchmark Brent was up $1.44 cents to $121.49 a barrel. U.S. crude was at $105.87, up $2.63.

Gold has drifted between $1,700 and $1,750 in the past two weeks, following the ups and downs in Greece's struggle to secure its bailout package.

(Additional reporting by Julie Haviv; Editing by Leslie Adler)