Nov 14 (Reuters) - Bitcoin miners are making hay while the sun shines.

The business has been yanked out of the doldrums by the cryptocurrency's recent rally - and now mining companies are racing to lock in profits before bitcoin's "halving", when rewards for producing the tokens are cut in half.

The next halving is expected in April 2024, a process designed to slow the release of bitcoin, whose supply is capped at 21 million - of which 19 million have already been mined.

"You're seeing a lot of urgency to plug rigs in ahead of the halving," said Gregory Lewis, analyst at brokerage BTIG that covers the 13 biggest U.S.-listed bitcoin miners.

Bitcoin's hashrate - a measure of the computational power needed to mine a coin - has spiked to an all-time high, according to crypto platform Blockchain.com. That means miners are having to use more and more power and speed to crack the complex maths puzzles that earn them a bitcoin.

Analysts at J.P. Morgan estimate the hashrate has hit record highs for 11 consecutive months, including a historic surge in October.

PLAYING THE GAME

Bitcoin has risen about 37% in the past month to around $37,000 after months of listlessness, a rebound that's encouraged miners to hook up their powerful computers to crack the puzzles and sell newly minted coins.

The 30-day average of revenue earned by miners has improved steadily this year to hit a 18-month at $32.46 million on Nov. 11, blockchain.com data shows.

However, mining - a highly energy-intensive process - is still not as profitable as in its 2021 heyday.

A measure of miners' earnings from using 1 petahash per second of computing power in a day has risen to over $81 from $70 at the start of November but remains well below a peak of $127 in early May, according to mining data platform Hashrate Index.

With six months to go till miners' share of rewards is slashed, they are looking for ways to keep their margins from shrinking in the highly competitive environment.

"Every halving forces miners not playing that game at a high enough level to get washed out," said William Szamosszegi, CEO of mining company Sazmining.

HALVING OPPORTUNITY

Bitcoin prices have typically rallied in the past following halvings. Six months after the first halving in 2012, the price jumped to $126 from $12. After the second halving in 2016, it went to $1,000 from $654 within seven months and in 2020 it shot up to $18,040 from $8,570 in the same time period.

Bitcoin's third halving in 2020 brought down miner rewards to 6.25 bitcoin per block and the upcoming one is set to push it down to 3.125 in April. At current prices, mining each block reaps $231,250. Matteo Greco, analyst at digital asset investment company Fineqia International, said many mining companies were upgrading their equipment and boosting their hashrate power to stay competitive.

To conserve their profit margins, some players have resorted to moving their operations to Central American countries where energy prices are more affordable, and governments friendlier to cryptocurrencies.

"It's too early to say if all bitcoin miners are out of the wood," said Ludovic Thomas, portfolio manager at Swiss-based Criptonite Asset Management that invests in digital assets. "Profitability increase always leads to network hashrate and difficulty increase."

(Reporting by Medha Singh and Lisa Mattackal in Bengaluru Editing by Vidya Ranganathan and Pravin Char)