Stocks edged higher on Tuesday, reversing losses, on a turnaround by shares of Goldman Sachs and gains in the energy and materials sectors.

Stocks had fallen as some corporate revenues disappointed and housing data signaled the recovery was losing momentum.

Goldman's stock, which opened lower, rose 1.7 percent to $148.16 despite posting lower second-quarter earnings and revenue as investors snapped up the beaten-down shares.

Earnings weren't very strong ... but (Goldman's) numbers are hard to estimate. I think it's an attractive investment at current levels, said Charles Lieberman, chief investment officer of Advisors Capital Management, LLC in Paramus, New Jersey.

An index of energy shares also rose along with the price of oil, helping to underpin the market. The S&P energy index <.GSPE> was up 0.6 percent. Oil gained 54 cents to $76.16.

The Dow Jones industrial average <.DJI> gained 37.16 points, or 0.37 percent, to 10,191.59. The Standard & Poor's 500 Index <.SPX> added 6.45 points, or 0.60 percent, to 1,077.70. The Nasdaq Composite Index <.IXIC> advanced 6.35 points, or 0.29 percent, to 2,204.58.

Housing starts fell more than expected in June, the government said, while applications for building permits topped forecasts.

The fact that the market didn't fall apart on weaker than expected results from key market participants this morning may suggest that the market is trying to find a bottom, said

Michael Sheldon, chief market strategist at RDM Financial in Westport, Connecticut.

All three indexes were down more than 1 percent shortly after the opening, with the Dow slipping nearly to 10,000.

International Business Machines Corp fell 3.9 percent to $124.73 a day after reporting quarterly revenues missed estimates as new technology services contracts declined. IBM, the world's biggest technology services company, was the top drag on the Dow.

Texas Instruments Inc also missed revenue expectations due to weaker-than-expected orders from one mobile phone customer, identified by analysts as Nokia. Shares of the chipmaker dropped 3.4 percent to $24.69.

The Dow Jones home construction index <.DJUSHB> added 1.8 percent.

Apple shares rose 1.2 percent to $248.52 before its earnings after the closing bell.

(Reporting by Caroline Valetkevitch; Editing by Kenneth Barry)