Oil and gas companies were the only bright spot in a dismal first half of the year for the U.S. stock market as the energy sector benefits from soaring commodity prices fueled by the war in Ukraine.

The S&P 500 energy sub-index, comprising 21 big oil and gas groups, jumped by almost a third in the first six months of the year, bucking a trend in which the wider market recorded its worst half in more than 50 years.

The 29% rise added more than $300 billion in market capitalization to the sector, while the broader index shed more than $8 trillion, or 21%.

“That is a massive, massive outperformance,” said Pavel Molchanov, an analyst at Raymond James. “To state the obvious—energy is the best performing sector of the equity market year to date.”

Hart Energy July 2022 - Financial Times US Energy Stocks Massive Outperformance - SP 500 Sectors First Half 2022 Graph

The trajectory of oil and gas stocks closely mirrored an increase in commodity prices, which were already marching higher coming into 2022 as supply lagged behind resurgent demand as economies recovered from coronavirus pandemic hits. But Russian President Vladimir Putin’s February decision to send troops into Ukraine has sent prices soaring as western nations impose sanctions and scramble to find alternatives to Russian imports.

WTI, the U.S. crude marker, has gained more than 40% this year to trade around $106 at the end of June. Benchmark Henry Hub U.S. gas added about 60% to trade at $5.70 per million Btu.

That has left U.S. oil and gas groups, from drillers to refiners, benefiting from a cash bonanza that has prompted political outrage as consumers pay record prices at the pump. President Joe Biden said recently that Exxon Mobil, America’s biggest producer by value, “made more money than God this year.”

Still, it has not all been positive for the industry. Energy stocks were at the sharp end of a broad sell-off last month, driven by growing fears that rapid interest rate rises would push the U.S. into recession. Oil and gas was the worst performer on the S&P in June, shedding 17% as oil and gas prices slid.

Fred Fromm, who runs a natural resources investment fund at Franklin Templeton, said it was “not surprising” to see some pullback after the earlier surge, but said the longer-term pressures that had been pushing stocks higher had not changed.

“The U.S. has not been the main driver of growth for oil demand for a decade or more … We believe even during a period of slower economic growth or a mild contraction, other demand factors like China reopening largely offsets that.”

Demand worries caused by coronavirus lockdowns in China—the world’s largest oil importer—had been a counterpressure on prices as they were rising earlier in the year.

Hart Energy July 2022 - Financial Times US Energy Stocks Massive Outperformance - US Oil Gas Stocks First Half 2022 Graph

The strong performance of oil and gas stocks in the first half of 2022 marked a stark change in fortunes for a sector that has been on the ropes for years. Energy was the worst performing index on the S&P 500 over the past decade as debt-fuelled drilling sprees led to steep losses, prompting investors to abandon the sector in droves.

But the industry says it has changed its ways and is now laser-focused on capital discipline and shareholder returns. Capex at the world’s top 50 producers is set to be just over $300 billion this year, according to Raymond James, down by almost half from a record $600 billion in 2013.

Despite the weaker June performance, analysts and investors reckon the oil and gas resurgence is set to continue into the second half of the year as the conflict in Ukraine continues to cause supply ructions.

“As long as the war continues, oil prices are likely to stay above $100 a barrel, which means that the profitability of just about everybody in the oil value chain will be at or near record highs,” said Molchanov.

“There are a lot of unknowns, more than I’ve ever seen before,” added Fromm, who said he expected energy stocks could remain volatile in the next few months. “But any weakness that causes we’re looking at as a potential buying opportunity.”