S&P 500 ekes out first half gain amid Greece debt crisis

U.S. stocks closed higher on Tuesday in a slight recovery from the worst trading day of the year, as investors remained on edge amid the imminent Greece repayment deadline to the IMF.

The S&P 500 posted its smallest gain on record (going back to 1928) for the first half of the year, up just 0.20 percent. ( Tweet This ) The index ended the quarter 0.24 percent lower, ending 9 consecutive quarterly gains. That is the longest stretch since the 14-quarter streak that ended in the second quarter of 1998.

The Dow Jones industrial average closed about 1.1 percent lower for the first half of the year and posted a 0.89 percent loss for the second quarter.

The Nasdaq had the best performance, up 5.3 percent for the year so far and up 1.75 percent for the second quarter, boosted by a surge in the Nasdaq iShares biotechnology ETF (IBB) and gains in Apple.

"I think it's not that there's expectation of a resolution (on Greece) as much as it's not necessarily as dire as a year ago," said Clem Miller, investment analyst at Wilmington Trust Advisors. "It's still a negative situation but it's less negative than it was Monday morning."

Greece faces a 1.5 billion euro ($1.7 billion) payment due to the International Monetary Fund at midnight CEST (6:00 p.m. ET) Tuesday. A euro zone official said in a Reuters report that there's "no way" the Eurogroup will release funds for Greece to meet the deadline tonight.

David Kelly, chief global strategist at JPMorgan Funds, said investors were watching rather than reacting significantly to the Greek headlines. He noted stocks are little changed so far this year, with the S&P barely half a percent higher.

"We're waiting for what we think will be a good jobs number," he said. "Economies are improving and ought to support a higher stock market."

Data was mixed on Tuesday. The major economic report expected for the week is the nonfarm payrolls report on Thursday, which could shed light on the timing of a rate hike. Markets are closed Friday for the July 4 holiday.

"I think there's talk that Greece will get some sort of deal done, although I don't think that's the case," said Robert Pavlik, chief market strategist at Boston Private Wealth.

Eurogroup President Jeroen Dijsselbloem said in a Reuters report that it is too late for an extension of the Tuesday deadline and that the institutions will only consider the request for a new program after the referendum on July 5.

Read MoreLive blog: Greece asks for new bailout, Merkel hits the brakes

He added that the Eurogroup of finance ministers will meet again on Wednesday to discuss the new Greek proposal after holding a conference call earlier in the day.

The Greek government indicated in the call it could change its stance on the referendum it has called for Sunday if a request for a new loan could be agreed, euro zone sources told Reuters.

Separately, Finnish Finance Minister Alexander Stubb said the Eurogroup concluded that requests from Prime Minister Alexis Tsipras for an extension of Greece's bailout program or debt relief were not possible, Reuters reported.

The prime minister said the government will seek a viable solution until the end and aims to stay in the euro, Reuters reported. The Greek government also issued a statement saying it has prepared a two-year agreement with the euro zone bailout fund in order to cover its financing needs, Dow Jones reported.

German Chancellor Angela Merkel said on Tuesday that Germany would not negotiate on a new bailout agreement for Greece before its referendum which is planned for Sunday.

"There's still a downward trend in place. At this juncture markets are holding relatively steady, otherwise holding their breath until the weekend," said Eric Lascelles, chief economist at RBC Global Asset Management. "The market's default assumption is that Greece does miss that deadline."

The Dow and S&P both briefly turned negative in midday trade amid reports that S&P downgraded four of Greece's banks to "selective default." The indices turned higher after struggling to hold slight gains.

"The Greece situation continues to grind on. It's not encouraging. (But) you had a selloff and a pause. It suggests not panicking," said Bruce McCain, chief investment strategist at Key Private Bank.

"It's more the assessment that this is not the crisis with Lehman Brothers or other situations that lead to broader systemic problems," he said.

Analysts also noted some positioning as Tuesday marks the end of the second quarter and first half of the year.

"I think it's an oversold bounce. ... We also had a lot of nervousness (yesterday)," said Adam Sarhan, CEO of Sarhan Capital. "But from a market standpoint you really want to see the bulls step up and buy this dip. If they don't, then when will they?"

Stocks had their worst day for the year so far on Monday, with the Nasdaq plunging 2.4 percent, the Dow Jones industrial average closing below its 200-day moving average and the S&P 500 barely above its 200-day moving average.

"Next support is 2044-2054 based on both the 200-day moving average and the March low," BTIG Chief Technical Strategist Katie Stockton said in a note. "Our indicators were mostly constructive ahead of the pullback, but the negative reaction to the news out of Greece certainly heightens risk in the near term for the SPX."

Read MoreYellen may have just gotten her excuse: Ex-Fed Gov.

"Today's trading is showing no signs of a sustainable bounce from yesterday's tarnish," Peter Cardillo, chief market economist at Rockwell Global Capital, said in a Tuesday note. "This action suggests a test to the lower support areas is in sight."

European stocks extended Monday's selloff to end more than 1 percent lower as Greece was expected to default later that night.

The Greece stock exchange and banks remained closed on Tuesday.

"This temporary noise from Greece will blow over and investors can focus on (good) fundamentals," said Doug Cote, chief market strategist at Voya Investment Management. "The bond market is indicating that there's no real concern about Greece."

He expects Greece will reach an 11th-hour deal with its creditors.

Treasury yields held higher, with the 10-year near 2.34 percent and the 2-year higher at 0.64 percent. The German 10-year bund yield was slightly lower at 0.77 percent.

The dollar gained more than half a percent against major world currencies, with the euro below $1.12.

In economic news, the Chicago purchasing mangers' index missed expectations and came in below the key 50 level at 49.4.

"The market wants to see a number that is above the boom-bust line," said Mark Luschini, chief investment strategist at Janney Montgomery Scott. "A number like that is either an anomaly or completely anomalous top the other readings of other (data)."

The consumer confidence index topped expectations with a 101.4 read for June. Earlier, the S&P/Case-Shiller home price index showed a 4.9 percent rise in April.

Read MoreStock picks for the second half

"Bottom line, in an economy that is mostly driven by the consumer, it is certainly encouraging to see an optimistic consumer, helped by continued improvement in the labor market," Peter Boockvar, chief market analyst at The Lindsey Group, said in a note.

In addition to Greece, investors are eyeing Puerto Rico, where another potential debt crisis is brewing. The commonwealth's Governor Alejandro Garcia Padilla late Monday said the island needs a restructuring plan for its $72 billion in debt.

"People are probably underestimating the Puerto Rico impact on the market," said Peter Schiff, CEO of Euro Pacific Capital. "I think the market could be impacted by what's going on in Puerto Rico (more) than what's going on in Europe."

Read More20 funds with most exposure to Puerto Rican debt

Asian stocks rebounded on Tuesday, with Shanghai leaping 5.55 percent amid new supportive measures from the government.

"Now we're working headlong into Greece, Puerto Rico and rising rates. It's going to be a volatile three months," said Pete Benson, founding co-owner and partner of Beacon Capital Management.

Symbol
Name
Price
 
Change
%Change
DJIA
---
S&P 500
---
NASDAQ
---

The Dow Jones Industrial Average closed up 23.16 points,or 0.13 percent, at 17,619.51, with Walt Disney leading advancers and Wal-Mart the greatest decliner. The two stocks are the best and worst performers in the index year-to-date, respectively.

The S&P 500 closed up 5.48 points, or 0.27 percent, at 2,063.12, with energy leading seven sectors higher and telecommunications the greatest laggard.

Health care is the best sector year-to-date and utilities the worst.

The Nasdaq closed up 28.40 points, or 0.57 percent, at 4,986.87.

The CBOE Volatility Index (VIX), widely considered the best gauge of fear in the market, edged lower to trade just above 18.

About three stocks advanced for every two decliners on the New York Stock Exchange, with an exchange volume of nearly 1.2 billion and a composite volume of about 4.0 billion in the close.

Crude oil futures for August delivery settled up $1.14, or 2 percent at $59.47 a barrel on the New York Mercantile Exchange. Gold futures ended $7.20 lower at $1,178.00 an ounce.

In corporate news, Apple launched its music streaming service on Tuesday. The stock is up 13.63 percent year-to-date.

Towers Watson plunged 8.8 percent on news it will merge with British reinsurance firm Willis Group in an all-stock merger. Towers Watson shareholders will get about 2.65 Willis shares for each share they now hold, plus a one-time dividend of $4.87 per share in cash. The combined company will be called Willis Towers Watson.

Celgene edged 0.72 percent higher on a report it will invest about $1 billion in Juno. The two will form a partnership to develop treatments for cancer and autoimmune diseases. Juno surged 15.18 percent.

Read More Early movers: PNR, CAG, GE, FIT, JUNO, MSFT, GM & more

ConAgra edged out a 0.67 percent gain after reporting in-line earnings on revenue that missed expectations. The firm also said it was taking a new approach to increasing profit margins, and as part of its new strategy, it will pursue a divestiture of its private label business.

Reuters and CNBC's Peter Schacknow contributed to this report

On tap this week:

Tuesday

Deadline for Iran nuclear talks

6 p.m.: St . Louis Fed President James Bullard

Wednesday

Vehicle sales released by manufacturers

8:15 a.m.: ADP employment

9:45 a.m.: Manufacturing PMI

10 a.m.: ISM manufacturing, construction spending

10:30 a.m.: Oil and gasoline inventories

Thursday

8:30 a.m.: Initial claims, employment report

10 a.m.: Factory orders

10:30 a.m.: Natural gas inventories

Friday

Fourth of July holiday

Bond and stock market closed

More From CNBC.com: