Hull Moving Average (HMA)
Summary
Hull Moving Average, published by Alan Hull in 2005: a moving average built to track price with far less lag than an SMA, WMA or EMA of the same length while staying smooth. It first removes lag by doubling a half-period WMA and subtracting the full-period one — extrapolating the average toward current price — then smooths that de-lagged series with a final WMA over the square root of the period.
HMA is also selectable as a moving-average type (TA_MAType_HMA) wherever an optInMAType parameter is accepted (MA, BBANDS, STOCH, MACDEXT, ...).
Chart
Chart metadata i
Outputs
outReal:line
Formula
HMA(n) = WMA( 2 * WMA(price, Integer(n/2)) - WMA(price, n), Integer(SquareRoot(n)) )
All three averages are the standard linearly-weighted moving average (TA-Lib's WMA). Every output is a closed-form weighted sum of the input window: there is no seeding, no recursion, hence no unstable period.
Notes
- The two derived periods
n/2andsqrt(n)are truncated to integers, exactly as in Alan Hull's own statement of the formula (Integer()); Tulip Indicators and pandas-ta do the same. Some other published descriptions round to nearest instead, which changes both the values and, for the square root, the lookback — a visibly different line, not a tolerance-level difference. TA-Lib follows the author. - The default period of 20 is Alan Hull's own default. It is also a period on which the truncate and round-to-nearest conventions coincide (20/2 is exact; sqrt(20) = 4.47 truncates and rounds to 4), so at the default a charting platform using the other convention still lands on TA-Lib's values.
- A period of 1 performs no smoothing: the output is a copy of the input.
Inputs
inReal— Source price series, close by convention
Outputs
outReal— Hull moving average of the input
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 20 | 1–100000 | Number of bars in the full-period WMA; the half and square-root periods derive from it |
Properties
Numerical Stability: Start-Independent
| ✅ Overlap Input i |
| ☐ Independent Y-Axis |
| ☐ Candlestick |
| ☐ Display Shift |
| ☐ Can Output NaN or ±Inf |
| ✅ Identity at Period 1 i |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_HMA( 0, n - 1, series, 20, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d: [%.4f, ..., %.4f]\n", outNBElement, outBegIdx, out[0], out[outNBElement - 1] );use ta_lib::Core;
let series = vec![0.0; 252]; // your data, oldest first
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.hma(0, series.len() - 1, &series, 20, &mut out)?;
println!("{} values from bar {}: [{:.4}, ..., {:.4}]", range.count, range.beg_idx, out[0], out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.OutRange;
import java.util.Locale;
double[] series = new double[252]; // your data, oldest first
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.hma(0, series.length - 1, series, 20, out);
System.out.printf(Locale.ROOT, "%d values from bar %d: [%.4f, ..., %.4f]%n", r.count(), r.begIdx(), out[0], out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Hma(0, series.Length - 1, series, 20, outReal);
Console.WriteLine(FormattableString.Invariant($"{r.Count} values from bar {r.BegIdx}: [{outReal[0]:F4}, ..., {outReal[r.Count - 1]:F4}]"));import { hma } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const { begIdx, values } = hma(series, { timePeriod: 20 });
console.log(values.length + ' values from bar ' + begIdx + ': [' + values[0].toFixed(4) + ', ..., ' + values[values.length - 1].toFixed(4) + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
series := make([]float64, 252) // your data, oldest first
out := make([]float64, 252)
core := talib.NewCore()
r, err := core.HMA(0, len(series)-1, series, 20, out)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d: [%.4f, ..., %.4f]\n", r.NBElement, r.BegIdx, out[0], out[r.NBElement-1])Implementation
Added in 0.8.1 (ta-lib-python 0.8.1).
TA-Lib Definition: hma.c · hma.yaml
| Native | File |
|---|---|
| C | ta_HMA.c |
| Rust | hma.rs |
| Java | Core_HMA.java |
| C# | Core_HMA.cs |
| TypeScript | hma.ts |
| Go | hma.go |
Python: talib.HMA (ta-lib-python 0.8.1 or later), see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Hull Moving Average
See Also
References
- Alan Hull, How to reduce lag in a moving average — the original definition, including the
Integer()truncation of both derived periods: alanhull.com/hull-moving-average