U.S. stocks closed sharply higher as investors were relieved that Greece and its creditors reached an agreement for a third bailout program, averting a potential ‘Grexit’.

The main indexes recorded their third consecutive session of gains, while Dow industrials advanced more than 200 points for the second session in a row.

The S&P 500 SPX, -1.15% closed 22.98 points, or 1.1%, higher at 2,099.60, with all 10 main sectors finishing with gains. Technology and consumer discretionary stocks led the gains.

The Dow Jones Industrial Average DJIA, -1.84% jumped 217.27 points, or 1.2%, to 17,977.68, with all but one of its member closing higher. The Nasdaq Composite COMP, -0.13% climbed 73.82 points, or 1.5% to 5,071.51.

U.S. equities followed a rally in European equity markets, spurred by news that a Greek exit from the eurozone had been avoided. After a 17-hour meeting, eurozone leaders agreed early Monday on a package of reform measures in principle for the debt-laden country, paving the way for formal negotiations on financial aid through the eurozone’s bailout fund, the European Stability Mechanism, or ESM.

The deal must now be approved by national parliaments, including the Greek Parliament, before the formal negotiations can begin. The formal pact is expected by the end of the week.

“Investors are optimistic that Greece will take the bailout deal and remain in the eurozone. For now, the risk of Grexit has been removed,” said Kate Warne, investment strategist at Edward Jones.

“We think investors are looking ahead at earnings and so far there aren’t a lot of signs that companies will disappoint. Once companies beat expectations, that will support prices further,” Warne said.

Read: ‘Bitter pill to swallow’ — analysts assess the Greek deal

Eurozone leaders reach a deal on Greece

Read: Here’s what Greece agreed to do to secure bailout funds

Not everyone is buying into the Greece-fueled optimism.

James Abate, chief investment strategist at Centre Funds Management, said it was perplexing to see markets rally on Greece news.

“It is perplexing to see that people are cheering news that amounts to a can-kicking exercise. Until there is a substantial hair-cut by the IMF, Greece will be back in the news in 12 months or 2 years — unable to service its debt,” Abate said.

“The bigger picture that a lot of leaders and investors are overlooking is the shrinking populations and falling productivity in Greece and other financially stressed eurozone countries and that they have no capability to grow their economies,” he added.

Follow the latest on the Greek debt crisis here

Movers & shakers:Fitbit Inc. FIT, -0.94% shares jumped 3.3% on Monday after several banks initiated coverage of the stock with buy ratings.

MarkWest Energy Partners LP US:MWE soared 14% after it said it was being acquired by MPLX LP for about $15.8 billion, creating a master-limited partnership giant with natural-gas and crude-oil presences. MPLX LP is a midstream master limited partnership (MLP) sponsored by Marathon Petroleum Corp. MPC, -3.19% Marathon shares jumped 7.8%.

Microsoft Corp MSFT, +1.07% shares rose 2.1% after the tech company announced its plans to launch Windows 10 operating system on July 29.

Netflix Inc. NFLX, +3.70% jumped 4%. The stock more than doubled since the start of the year and set an intraday high at $716.16 on Monday. The company reports earnings on Wednesday and will split its stock 7-for-1 the same day.

For more on today’s notable movers read Movers & Shakers column.

Other markets:Chinese stocks led Asian markets higher, although shares of many firms were still halted for trade on the back of the sharp selloff in recent weeks.

Oil prices CLQ25, were under pressure from potential fallout from an Iranian deal, stronger dollar and rising production from OPEC. Oil futures settled 1% lower at $51.20 a barrel