NEW YORK — US stocks closed at a record high Monday behind gains for chemical and machinery companies. Energy companies rose as the price of oil continued its recent recovery.
Makers of chemicals and mining companies made the biggest gains, and machinery companies and banks followed. Investors sold government bonds and utility and phone companies. Those stocks climbed earlier in the year as investors sought safety. Stocks have seesawed between small gains and losses for more than a week as investors consider mixed reports on the health of the economy and a decline in corporate earnings. That hasn’t stopped them from setting records, but it has kept investors wary.
‘‘The market has run up in anticipation of better earnings ahead,’’ said Brian Nick, chief investment strategist for TIAA Investments. ‘‘If those earnings don’t come, we have the Wile E. Coyote moment where we’re off the cliff . . . and we’re gonna fall.’’
The Dow Jones industrial average climbed 0.3 percent to 18,636.05. The Standard & Poor’s 500 index rose 0.3 percent to 2,190.15. The Nasdaq Composite added 0.6 percent to 5,262.02.
Second-quarter earnings are nearly all in, with this week’s releases from Home Depot, Walmart, and Target among the last. Corporate earnings are down again this quarter, and investors don’t expect much growth in the third quarter, but they are starting to expect improvement after that.
US crude oil jumped 2.8 percent to $45.74 a barrel in New York. Brent crude, a benchmark used to price international oils, rose 2.9 percent to $48.35 in London. After a steep slide for most of June and July, the price of US crude gained 6.4 percent last week.
Drilling rig operator Transocean added 5.4 percent to $10.43. National Oilwell Varco picked up 3.1 percent to $34.85, and ConocoPhillips rose 2 percent to $42.18.
The chemicals company LyondellBasell Industries rose 2.9 percent to $77.49; mining and energy company Freeport-McMoRan climbed 3 percent to $12.17. Alcoa gained 3.4 percent to $10.52.
Utility companies took the largest losses, as Southern Co. declined 1.6 percent to $51.49 and Consolidated Edison sank 2.4 percent to $76.24. Phone companies and household goods makers also slipped.
Real estate investment trust Mid-America Apartment Communities will buy competitor Post Properties for about $3.9 billion in stock. Both companies own large numbers of rental properties, and demand for rentals has boomed in recent years because many people are priced out of the housing market.
The deal values Post Properties at about $72.53 a share. based on Friday’s closing prices. The stock rose 9.4 percent to $68.08. Mid-America lost 4.9 percent to $97.15.
Water treatment company Xylem announced a $1.7 billion deal for Sensus, which provides smart meters, network technology, and analytics used by water, electric, and gas utilities. Xylem stock advanced 3.9 percent to $50.32.
A survey showed that US homebuilders are feeling more optimistic about the market as prices and sales of new homes rise. The National Association of Home Builders/Wells Fargo builder sentiment index rose from last month.
In June, sales of new homes grew by the fastest pace in eight years, aided by continuing job growth and low mortgage rates. Toll Brothers gained 3.2 percent to $28.86 and Lennar added 1.2 percent to $47.30.
Japan’s economy grew at a lower-than-forecast 0.2 percent pace in the April-June quarter, as private demand and exports remained weak. That could push the Bank of Japan to take additional steps to stimulate the national economy. The bank approved a new stimulus package earlier this month, but that wasn’t enough to please investors on Wall Street.
Germany’s DAX index was up 0.2 percent, and the CAC 40 of France dipped less than 0.1 percent. Britain’s FTSE 100 gained 0.4 percent. Japan’s Nikkei 225 edged up 0.3 percent, and Hong Kong’s Hang Seng index rose 0.7 percent.