With just three trading days left in the month, the S&P 500 is set to post its best month since October 2011. The Nasdaq's advance makes July so far the best month in a year and a half.

After declining for most of the day, the market regained ground shortly before the close, and all three major indexes finished in positive territory. At one point, the Dow was down as much as 150.45 points.

"I was surprised we ended up, but people have a sense of optimism about the market," said Matt McCormick, a money manager at Cincinnati-based Bahl & Gaynor Inc.

"The bad news from the morning was digested and discarded. Earnings haven't been as bad as people expected, and the political issues aren't in the forefront right now. That could change at any moment, but enjoy the summer rally while you can."

Five of the 10 S&P 500 industry sector indexes advanced, with the health sector leading the gains.

The Dow Jones Industrial Average <.DJI> rose 3.22 points, or 0.02 percent, to end at 15,558.83. The Standard & Poor's 500 Index <.SPX> gained 1.40 points, or 0.08 percent, to finish at 1,691.65. The Nasdaq Composite Index <.IXIC> advanced 7.98 points, or 0.22 percent, to close at 3,613.16.

Quincy Krosby, market strategist at Prudential Financial in Newark, New Jersey, said the market began to turn around on news that President Barack Obama had not reached a decision on who will succeed Federal Reserve Chairman Ben Bernanke and on optimism that the central bank will keep monetary policy accommodative.

"At least today, the market is expecting the Fed to offer no surprises, and if anything, to be maybe a bit more dovish," she said.

Speculation over who Bernanke's successor could be has caused anxiety in the market, with investors wondering how different the next chair's policies would be. The timing of the announcement is also tricky as the Fed simultaneously considers when to begin pulling back its $85 billion a month in bond purchases.

The prospect that former Treasury Secretary Lawrence Summers might get the nod, and not current Fed Vice Chair Janet Yellen, has concerned investors. Yellen is seen as more likely to provide a smooth transition after Bernanke's term ends, while Summers is viewed as more critical of the effectiveness of the central bank's stimulus.

Investors will scrutinize the Federal Open Market Committee policy statement next Wednesday for any additional clues about the Fed's intended timeline for scaling back its quantitative easing.

For the week, the S&P 500 finished essentially flat - down just 0.03 pct - the first week in five that it did not manage a gain. But the benchmark index is up 5.3 percent so far in July - its best month since October 2011.

The Dow rose 0.1 percent for the week, extending its string of weekly gains. For July, the Dow is up 4.4 percent.

The Nasdaq is up 6.2 percent in July so far, its best monthly gain in a year and half. For the week, the Nasdaq is up about 0.7 percent.

Expedia (>> Expedia Inc) ranked among the most active names traded on the Nasdaq. Shares of the online travel agency plunged 27.4 percent to $47.20 a day after it reported a quarterly profit far short of market estimates.

Starbucks (>> Starbucks Corporation) gave the biggest boost to the S&P 500 a day after the world's largest coffee chain reported a bigger-than-expected jump in quarterly profit. Starbucks' stock rose 7.6 percent to close at $73.36.

Halfway through earnings season, 67.6 percent of S&P 500 companies have beaten analysts' expectations - in line with the 67 percent average beat in the last four quarters.

About 56 percent of the companies have beaten revenue expectations, more than the 48 percent of revenue beats in the past four earnings seasons, but below the historical average.

Amazon (>> Amazon.com, Inc.) shares turned higher despite a forecast that disappointed on income and revenue. The stock rose 2.8 percent to $312.01, a rebound from a session low at $295.55. Earlier on Friday, Amazon's stock hit a 52-week high of $313.62.

Boeing Co (>> The Boeing Company) shares declined after the U.S. Federal Aviation Administration said it was proposing to fine the company's commercial airplane division $2.75 million for failing to take prompt action to fix a problem with fasteners on its model 777 airplanes. Shares of Boeing fell 1 percent to $105.60 and were the biggest drag on the Dow.

In addition to the FOMC meeting on Tuesday and Wednesday, next week's packed economic calendar includes the advance estimate, or second look, at gross domestic product for the second quarter, and the July nonfarm payrolls report.

About 5.4 billion shares changed hands on U.S. exchanges, below the daily average of 6.4 billion. Volume was relatively light for the week as a whole.

Decliners outnumbered advancers on the New York Stock Exchange by a ratio of 8 to 7. On the Nasdaq, about five stocks fell for every three that rose.

(Reporting by Alison Griswold; Additional reporting by Ryan Vlastelica; Editing by Jan Paschal)

By Alison Griswold