(AP) -- Financial and energy companies led a modest increase in U.S. stocks Wednesday, giving the stock market its third gain in a row.
The market got a boost from a pickup in the price of oil, which climbed about 4 percent after an early slide. When oil prices rise it tends to favor battered energy stocks and financial companies such as banks, which have been in the doldrums due to investor concerns that loans to struggling oil companies could go bad.
After several weeks of moving in different directions, the stock market appears to be getting more closely tied to the fluctuations in oil prices.
"Oil is what's been driving the market lately," said Chris Gaffney, president of EverBank World Markets.
Utilities and consumer staples stocks were among the biggest decliners.
The Dow Jones industrial average rose 42.67 points, or 0.2 percent, to 18,096.27. The Standard & Poor's 500 index added 1.60 points, or 0.1 percent, to 2,102.40. The Nasdaq composite index gained 7.80 points, or 0.2 percent, to 4,948.13.
The Dow is now up almost 4 percent for the year, while the S&P 500 is up about 3 percent. The Nasdaq narrowed its loss to 1.2 percent.
Trading got off to a flat start, with the major stock indexes moving sideways. They perked up by midmorning, as oil prices turned higher, but the rally lost some steam by the end of the day.
U.S. crude rose $1.55, or 3.8 percent, to close at $42.63 a barrel in New York. Brent crude, the international benchmark, climbed $1.77, or 4 percent, at $45.80 a barrel in London. Heating oil jumped 5.5 percent after adding 7 cents to close at $1.33 a gallon.
That helped lift shares in several energy companies. Chesapeake Energy gained 30 cents, or 4.9 percent, to $6.42, while Williams Cos. rose 82 cents, or 4.6 percent, at $18.83.
The market got some encouraging data on housing, with the National Association of Realtors reporting that sales of previously occupied U.S. homes bounced back in March after a February slump as the spring home-selling season kicked off.
Investors also had their eye on the latest batch of company earnings.
Corporate profits for companies in the S&P 500 are expected to be down 8.1 percent, according to S&P Global Market Intelligence. Even excluding energy companies, which have been hammered by falling oil prices, earnings growth for the S&P 500 companies is projected to be down 3.3 percent.
Even so, while only about 15 percent of companies having reported results at this point, many have turned in better-than-expected results.
"So far, we're actually seeing companies surprising to the upside," said Jason Pride, director of investment strategy at Glenmede.
Discover Financial Services led all the gainers in the S&P 500 after the credit card issuer and lender reported better-than-anticipated quarterly profit and sales as loan volume improved. The stock climbed $4.29, or 8.2 percent, to $56.84.
VMWare also delivered strong results, which vaulted the cloud-computing company $7.07, or nearly 14 percent, to $58.53.
Some companies failed to turn in encouraging results.
Coca-Cola slid 4.8 percent after the world's biggest beverage maker reported a lower profit for the first quarter. The company was squeezed by a strong dollar and charges related to the transformation of its North American operations. Coca-Cola was the biggest decliner in the S&P 500. It lost $2.23 to $44.37.
Beyond earnings, investors welcomed news that printer maker Lexmark International agreed to be bought by a group that includes Apex Technology and PAG Asia Capital for about $2.51 billion. The stock climbed $3.24, or 9.3 percent, to $37.90.
Stock markets in Europe also closed higher.
Germany's DAX rose 0.7 percent, while the CAC-40 of France gained 0.6 percent. The FTSE 100 of leading British shares added 0.1 percent. Earlier in Asia, most markets closed lower. South Korea's Kospi fell 0.3 percent and Hong Kong's Hang Seng slid 0.9 percent. Japan's benchmark Nikkei 225 edged up 0.2 percent, while Australia's S&P/ASX 200 added 0.5 percent.
In other energy trading, wholesale gasoline rose about 3 cents, or 1.8 percent, to close at $1.51 a gallon. Natural gas fell 2 cents to close at $2.069 per 1,000 cubic feet.
Precious and industrial metals futures inched higher. Gold rose 10 cents to $1,254.40 an ounce, silver gained 16 cents, or 1 percent, to $17.14 an ounce and copper rose 1.5 cents to $2.24 a pound.
Bond prices fell. The yield on the 10-year Treasury note rose to 1.85 from 1.78 late Tuesday. In currency markets, the euro fell to $1.1302 from $1.1377, while the dollar rose to 109.80 yen from 109.13.