> "Alan Hull looked at the lag in moving averages and said, 'I can fix that.' And he did, by making the math do gymnastics."
HMA (Hull Moving Average) is a solution to the eternal struggle between smoothness and lag. Most indicators force you to choose one; HMA gives you both. It achieves this by using weighted moving averages (WMAs) in a clever configuration that cancels out lag while maintaining the smoothing properties of the WMA.
## Historical Context
Developed by Alan Hull in 2005, the HMA was designed to be "responsive, accurate, and smooth." Hull realized that lag is essentially a function of the period, and by combining averages of different periods (specifically, a full period and a half period), he could mathematically offset the lag.
## Architecture & Physics
The HMA is built from three Weighted Moving Averages (WMAs):
1.**WMA(n/2)**: A fast WMA of half the period.
2.**WMA(n)**: A slow WMA of the full period.
3.**WMA(sqrt(n))**: A smoothing WMA applied to the difference.
The core logic is: $2 \times \text{WMA}(n/2) - \text{WMA}(n)$.
This operation "over-weights" the recent data, pushing the average forward to align with the current price. The final WMA smooths out the resulting noise.
This results in different effective periods for $N=14$ ($\sqrt{14} \approx 3.74 \to 3$ vs $4$) and others where the fractional part $\ge 0.5$. Validation tests match exactly for periods where rounding logic aligns (e.g., $N=9, 20, 50$).
## C# Implementation Considerations
### Compositional Architecture
HMA composes three independent `Wma` instances rather than implementing custom logic:
```csharp
_wmaFull=newWma(period);
_wmaHalf=newWma(halfPeriod);
_wmaSqrt=newWma(_sqrtPeriod);
```
This leverages WMA's optimized O(1) implementation for each component, maintaining the zero-allocation property.
### Streaming Update Pipeline
The hot path chains the three WMA updates with minimal intermediate allocation:
For HMA(100), total memory is approximately 1.7 KB per instance (three WMA instances combined).
### Common Pitfalls
1.**Overshoot**: Like DEMA, HMA can overshoot price turns because of the lag correction.
2.**Period Sensitivity**: The $\sqrt{N}$ smoothing is hardcoded into the definition. You can't easily tweak the smoothing independently of the lag correction without breaking the "Hull" definition.
3.**Integer Math**: The periods $N/2$ and $\sqrt{N}$ are rounded to integers. This can cause slight discrepancies between implementations depending on rounding rules. Standard integer truncation is used in QuanTAlib.