The Hull Moving Average (HMA) is another type of moving average used in technical analysis, designed to reduce lag and provide a more accurate representation of current price movements compared to traditional moving averages. It achieves this by incorporating weighted averages and by adjusting its calculation method based on the market volatility.
The formula for calculating the Hull Moving Average is a three-step process:
- Calculate the Weighted Moving Average (WMA) of a given period.
- Calculate the WMA of half the period.
- Subtract the result from step 2 from twice the result of step 1.
The formula for the Hull Moving Average can be written as:
HMA=WMA(2×WMA(n2)−WMA(n)),HMA = WMA(2 \times WMA(\frac{n}{2}) - WMA(n)),
Where:
- HMAHMA is the Hull Moving Average.
- WMAWMA is the Weighted Moving Average.
- nn is the period over which the Hull Moving Average is calculated.
Here's a Python function to calculate the Hull Moving Average:
def hull_moving_average(data, n):
weighted_sum = 0
weighted_divisor = 0
for i in range(n):
weight = 2 * (i + 1)
weighted_sum += data[-n + i] * weight
weighted_divisor += weight
wma_n = weighted_sum / weighted_divisor
weighted_sum = 0
weighted_divisor = 0
for i in range(n // 2):
weight = (i + 1)
weighted_sum += data[-n // 2 + i] * weight
weighted_divisor += weight
wma_half_n = weighted_sum / weighted_divisor
hma = wma(2 * wma_half_n - wma_n)
return hma
In this function:
data
is a list of historical prices.n
is the period over which the Hull Moving Average is calculated.
You can then use the hull_moving_average
function to calculate the Hull Moving Average for a given dataset and period.