Stocks eke out gains as Fed vows flexibility on stimulus

Stocks squeezed out modest gains in choppy trading Wednesday, following comments from Fed Chairman Ben Bernanke that monetary policy supporting the markets will remain in place even while the central bank could start to scale back its bond buying later this year.

The Dow Jones Industrial Average finished 18 points higher. Bank of America led the gainers and Caterpillar slumped to lead the Dow laggards.

The S&P 500 and the Nasdaq also closed modestly higher. Both indexes had finished lower for the first time in nine sessions on Tuesday. The CBOE Volatility Index (VIX), widely considered the best gauge of fear in the market, traded near 14.

(Read More: Thanks to QE, bubbleof 2000 looks like 'day at beach')

The markets had initially risen sharply on Wednesday after Bernanke reiterated the central bank's plan to start paring back its bond-purchase program later this year, but said that could change if the economic outlook shifted.

"Our asset purchases depend on economic and financial developments, but they are by no means on a preset course," Bernanke said in prepared remarks to the U.S. House of Representatives Financial Services Committee.

The Federal Reserve chief said the pace of asset purchases could be reduced "somewhat more quickly" if economic conditions were to improve faster than expected. On the other hand, the current $85 billion monthly pace "could be maintained for longer" if the labor market outlook darkened, or inflation did not look like it was rising back toward the Fed's 2 percent goal.

Meanwhile, the U.S. economy continued to grow at a modest to moderate pace in June and early July, with manufacturing expanding in most areas of the country, according to the Federal Reserve's Beige Book report.

(Read more:Fed's Bernanke Gets What He Wants: A Big Yawn)

The Fed's stimulus policies have buoyed the markets and Bernanke's words on Wednesday appeared to cheer investors.

"U.S. equities have recouped their spring swoon as a litany of Fed governors' pronouncements have on balance calmed fears QE tapering might begin before the U.S. recovery is strong enough to handle it," said Alec Young, global equity strategist at S&P Capital IQ.

This more dovish Fed perception, coupled with a recent softening in economic news flow, has hit bond yields and the greenback, Young noted. "Turning back to the U.S., second-quarter beats will now need to take the lead in fueling stocks, as we think a more dovish Fed is now priced into a very overbought market."

Most key S&P sectors remained in positive territory, led by materials, while utilities turned lower.

On the economic front, housing starts tumbled 9.9 percent in June to a seasonally adjusted annual rate of 836,000 units, according to the Commerce Department, the lowest level since last August. Economists polled by Reuters had expected groundbreaking to rise to a 959,000-unit rate last month.

Mortgage applications fell last week, due to a decline in demand for refinancing loans as mortgage interest rates remained at a two-year high, according to the Mortgage Bankers Association.

Among earnings, Bank of America climbed after the financial giant posted a 70 percent jump in earnings, thanks to lower operating expenses.

(Read More:Enjoy it while it lasts: Bank earnings face trouble)

Analysts expect S&P 500 companies' second-quarter earnings to have grown 3 percent from a year earlier, with revenue up 1.5 percent, according to the latest data from Thomson Reuters.