Hull Moving Average in Pine Script
A moving average built from weighted averages of two lengths so that most of the lag is cancelled, which gives a line that turns almost with price at the cost of overshoot.
Definition
The Hull moving average (HMA) is a moving average designed to remove lag. It takes a weighted moving average of half the chosen length, doubles it, subtracts the weighted average of the full length, and smooths the difference over the square root of the length. The result hugs price far more closely than an SMA or EMA of the same length while staying smooth enough to read. It belongs to the moving averages family and is usually traded by its slope rather than by price crossing it.
The trick works because the difference between a short and a long weighted average is a rough estimate of where price is heading. Adding that difference to the short average pushes the line ahead of where a plain average would sit, which cancels most of the lag. The final smoothing over √n bars removes the noise that the subtraction adds. The line reacts to a turn within a few bars, so the slope of the HMA is a usable trend signal where the slope of an SMA would be a late one.
Alan Hull published the average in 2005. Common lengths are 9 to 21 for fast reads and 55 for a trend line. The library's implementation draws one HMA of 55 by default, coloured green while it is rising and red while it is falling. It marks Long on the confirmed bar the line turns up and Short on the bar it turns down, with an optional alert on each turn. Price crossing the line is deliberately not the trigger, because the line sits so close to price that crosses are constant.
On the chart the HMA looks like a smoothed version of price itself, with turns that arrive 1 or 2 bars after the swing. In a trend it runs beside the candles and its colour rarely changes. At a reversal it overshoots: the subtraction that removes lag also projects the last move a little too far, so the line bends past the turning point and back. In a range the colour flips every few bars, and each flip is a small loss for a trader who follows all of them.
Reading the chart
The HMA is read by its colour and its turns, so identification is about the quality of each turn rather than the level of the line.
- Choose the length for the job. 9 to 21 for a fast signal line on intraday charts, 55 for a trend line that filters most of the noise, longer for a regime line.
- Read the colour. Green means the line is rising and the trend is up on this length; red means it is falling. The library's fill switches on the first confirmed bar of each new slope.
- Judge each turn by what came before. A turn after a long run of one colour and a clear swing is a trend change; a turn after 3 bars of the other colour is range noise.
- Allow for overshoot at reversals. The line bends past the actual turning point, so the level of the HMA at a swing is not a precise support or resistance value.
- Confirm with structure. A turn up that coincides with a higher low, or a turn down with a lower high, carries more weight than a turn in the middle of nowhere.
- Pair it with a slower reference. A fast HMA turning in the direction of a rising 50 or 200 period average is a pullback entry; against it, a counter-trend fade with a quick exit.
The maths
The HMA is a weighted average of a lag-corrected series: twice the half-length weighted average minus the full-length one.
The half length and the square root are rounded to whole bars, so an HMA of 55 uses weighted averages of 27 and 55 and a final smoothing of 7 bars.
The raw series can move outside the range of recent closes because of the subtraction, which is the source of the overshoot at turns.
The library's script marks the change of slope, not price crossing the line: Long on the first confirmed bar the HMA is above its previous value, Short on the first bar it is below.
A flat bar, where the HMA equals its previous value, counts as not rising, so a stalled line reads as red until it rises again.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Length | 55 (9 to 21 for a signal line) | Shorter lengths turn within a bar or 2 of price and flip constantly in ranges; longer lengths turn later, overshoot less and hold their colour through pullbacks. |
| Source | close | Typical price (hlc3) tames the overshoot on wide-range bars a little; the close is standard. |
In practice
- As a slope trigger: the library's script marks Long when the HMA turns up and Short when it turns down. A filter keeps the turns that agree with a slower average and drops the ones against it.
- As a trend colour: keep a 55 period HMA on the chart as a fast regime line. Its colour changes days before an SMA of the same length would turn, which helps with early exits.
- As a trailing exit: the rule holds a trade while the line keeps its colour and exits on the first confirmed turn against the position. The exit is close to the swing because the lag is small.
- As a smoothed price: use the HMA in place of the close inside other indicators, for example as the source of an RSI, to cut noise without adding much lag.
- As a filter for faster tools: the filter passes oscillator triggers only in the direction of the HMA's colour, which removes most counter-trend triggers on intraday charts.
- As a pullback guide: in a strong trend, price touching a fast HMA that is still its trend colour is a pullback entry with a close stop past the last swing.
Where it goes wrong
- Trading every colour change. In a range the line flips every few bars; require agreement with a slower average or a structure signal before acting.
- Using the line as support or resistance. It overshoots at turns and hugs price in trends, so its level is not a place orders cluster; use it for slope only.
- Choosing a length too short for the timeframe. A 9 period HMA on a 1 minute chart is nearly the price itself and gives no smoothing at all.
- Expecting the HMA to lead price. It reduces lag to a bar or 2; it does not forecast, and the overshoot is a side effect of the projection, not a prediction.
Compared with
Moving Average. An SMA or EMA of the same length lags by roughly half its length and is read by price crossing it. The HMA removes most of that lag and is read by its slope, which is why it works as a signal line where the SMA is a level.
Moving Average Slope. The slope filter measures how steep an ordinary EMA is, in ATR units, and only calls a trend past a threshold. The HMA's colour flips on any change of direction, so it is faster and noisier; the two combine well.
SuperTrend. SuperTrend is a stepped stop line that only flips when price closes through it, so it stays one colour through pullbacks that would turn an HMA. The HMA reacts sooner, reverses more often and gives back less at the end of a trend.
Heikin-Ashi. Heikin-Ashi candles smooth price into runs of one colour, much as the HMA does, but they replace the candles themselves. The HMA leaves the real price on the chart and adds a line, which keeps stops and levels honest.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
What length should I use for the Hull moving average?
9 to 21 for a signal line on intraday charts, 55 for a trend line on any timeframe, and 100 or more for a regime line. Because the HMA lags so little, a length that would be slow as an SMA is fast as an HMA, so start longer than you would with a plain average.
Why does the HMA overshoot at turns?
Because it removes lag by projecting the difference between a short and a long weighted average. At a reversal that difference still points the old way for a bar or 2, so the line bends past the turning point before it comes back. The overshoot is the cost of the speed and is worst on sharp V reversals.
Is price crossing the HMA a useful trigger?
No. The line sits so close to price that crosses happen constantly and mean little. The slope is the better read, which is what the library's script marks: Long on the bar the line turns up, Short on the bar it turns down, and preferably only in the direction of a slower average.
Is the HMA better than an EMA?
It is faster, not better. The HMA turns within a bar or 2 of price where an EMA of the same length needs several, so it makes a better signal line and a better exit. The EMA is calmer, overshoots less and is more widely watched as a level. Many traders use both: HMA for timing, EMA for the level.
Does the Hull moving average repaint?
No. Every value is built from closed bars and the slope is checked on confirmed bars, so a colour change never disappears once printed. The current bar's value moves while the bar forms, which is the reason the library's script waits for the close.
Can I use the HMA as the source for other indicators?
Yes, and it is a good use. Feeding an HMA into an RSI, a MACD or a Bollinger Band cuts the bar-to-bar noise without adding the lag an SMA would add. Expect the derived indicator to be slightly smoother and to react a bar later than it does on the raw close.