NEW YORK (MarketWatch) — U.S. stocks rallied on Friday, sending the S&P 500 and Dow Jones Industrial Average to record levels on news that eurozone ministers agreed to a four-month extension of Greece’s bailout.

The main indexes booked weekly gains for the third consecutive week.

The antiausterity Greek government sought an extension to its loan agreement and on Friday after hours of meetings eurozone ministers reached an agreement. Athens has to submit a list of reform proposals by Monday.

Read: Greek bailout extended by 4 months

The S&P 500 SPX, -1.11% climbed 12.85 points, or 0.6% to 2,110.30, with nine of its 10 main sectors finishing higher. The benchmark index gained 0.6% over the week.

The Dow Jones Industrial Average DJIA, -0.87% swung from a triple-digit loss to a triple-digit gain, adding 154.67 points, or 0.9%, to 18,140.44. The blue-chip index closed at a record high for the first time this year and booked a 0.7% gain over the week.

The Nasdaq Composite COMP, -1.07% rose for the eighth consecutive session, adding 31.27 points, or 0.6%, to 4,952 and gaining 1.3% over the week. The tech-heavy index outperformed other benchmarks so far this year, gaining 4.6%, compared with a gain of 2.5% for the S&P 500.

The stock market had been trending higher, but news of an accord between Greece and its creditors helped fuel a rally.

Jeffrey Saut, chief investment strategist at Raymond James, dismissed daily moves, but noted that there are probably many traders with short positions who wouldn't want to go into the weekend holding those positions in the wake of positive Greek news.

John Manley, chief equity strategist at Wells Fargo Advantage Funds, pointed to the Greek reports as another example the market failed to fail, implying that equity values are growing lofty.

“Investors are building up strong confidence, as we see how every dip in this market is bought. Bull markets end when investors get excited and stay excited. The trip to that excitement has begun, but it may last anywhere from 6 months or 18 months,” Manley said.

Channing Smith, portfolio manager at Capital Advisors, taking a slightly longer view of the market, said it is hard to explain investor enthusiasm that had led to the rally so far this month.

“Investor optimism has been fueled the relative strength of the U.S. economy, unbridled faith in central banks across the globe, which have broadly adopted easy-money policies amid deflation fears, and lack of alternatives, Smith said.

Data: Friday’s sole piece of U.S. economic data showed that manufacturing activity picked up in February, with the Markit flash purchasing managers index climbing to 54.3, above forecasts by economists polled by MarketWatch. However, details were much weaker and indicated the U.S. economy has entered a slower growth phase.

PMI readings out of the eurozone showed business activity in the currency union grew at a faster pace in February than expected, to reach a seven-month high.

Earnings: Nordstrom Inc. JWN, -3.07% turned in fourth-quarter earnings of $255 million, or $1.32 a share, missing Wall Street’s consensus estimate of $1.35 a share. Sales rose to $3.94 billion from $3.61 billion. Shares dropped 4.4%.

Intuit Inc. INTU, -2.31% shares jumped 6.2% after its chief financial officer said Thursday that the company hasn’t found any evidence that its systems were hacked into in connection with a number of fraudulent tax-return filings this month.

Fannie Mae FNM, shares dropped 7.5% after the company offered a dour earnings outlook as it reported quarterly profits that sank by about 80%.

For more on notable movers, read our Movers & Shakers column.

Other markets:Oil CLH25, settled 1.5% lower at $50.34 a barrel, and booked their first weekly loss in a month.

Gold prices US:GCJ5 settled slightly lower at $1,204.90 an ounce and lost 1.8% over the week.