What is the Hull Moving Average (HMA)?
The Hull Moving Average (HMA), created by Alan Hull, is a unique and powerful moving average designed to be extremely smooth while simultaneously having very little lag. Traditional moving averages often suffer from either being too laggy (smoothing out noise but delaying signals) or too noisy (responsive but prone to whipsaws). The HMA aims to address this dilemma by using a sophisticated weighting method to achieve both responsiveness and smoothness.
In Pine Script, the HMA stands out as a superior tool for discerning trend direction early and filtering out market noise effectively, making it a favorite among traders looking for cleaner signals.
Components and Calculation
The HMA is calculated in three steps, involving Weighted Moving Averages (WMAs):
- Step 1: Calculate a WMA with period `n/2`: First, a Weighted Moving Average of the input price (typically `close`) is calculated over half the HMA's specified length (`n/2`).
- Step 2: Calculate a WMA with period `n`: Next, another Weighted Moving Average of the input price is calculated over the full HMA length (`n`).
- Step 3: Combine and smooth: Subtract the WMA from Step 2 from twice the WMA from Step 1. Then, calculate a WMA of this result, using a period equal to the square root of `n` (`sqrt(n)`). This final WMA is the Hull Moving Average.
This triple-smoothing process, particularly the square root part, is what gives HMA its unique low-lag and smoothing characteristics.
Basic HMA Implementation in Pine Script
Pine Script v5 provides a convenient built-in function `ta.hma()` that greatly simplifies its implementation.
//@version=5
indicator("My Hull Moving Average", overlay=true)
// Input for HMA length
length = input.int(20, title="HMA Length", minval=1)
// Calculate HMA using the built-in function
hmaValue = ta.hma(close, length)
// Plot the HMA line
plot(hmaValue, title="HMA", color=color.blue, linewidth=2)
Practical HMA Strategies
1. HMA as a Trend Direction Filter (Color Change)
HMA's smoothness makes it excellent for identifying the prevailing trend direction. A popular visual strategy involves coloring the HMA based on its slope or its relation to the previous bar's value.
- Uptrend: HMA is rising (current HMA > previous HMA).
- Downtrend: HMA is falling (current HMA < previous HMA).
//@version=5
strategy("HMA Trend Color Strategy", overlay=true)
// Input for HMA length
length = input.int(20, title="HMA Length", minval=1)
// Calculate HMA
hmaValue = ta.hma(close, length)
// Determine HMA color based on its direction
hmaColor = hmaValue > hmaValue[1] ? color.green : color.red
// Plot the HMA line with dynamic color
plot(hmaValue, title="HMA", color=hmaColor, linewidth=2)
// Example entry logic: buy when HMA turns green, sell when HMA turns red
longCondition = hmaValue > hmaValue[1] and hmaValue[1] <= hmaValue[2] // HMA turns up
shortCondition = hmaValue < hmaValue[1] and hmaValue[1] >= hmaValue[2] // HMA turns down
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
2. HMA Crossover Strategy (with Price or another HMA)
Crossovers between price and HMA, or between two HMAs of different lengths, can generate powerful trading signals.
//@version=5
strategy("HMA Crossover Strategy", overlay=true)
// Inputs for HMA lengths
fastHmaLength = input.int(10, title="Fast HMA Length", minval=1)
slowHmaLength = input.int(30, title="Slow HMA Length", minval=1)
// Calculate HMAs
fastHma = ta.hma(close, fastHmaLength)
slowHma = ta.hma(close, slowHmaLength)
// Plot the HMAs
plot(fastHma, title="Fast HMA", color=color.blue, linewidth=2)
plot(slowHma, title="Slow HMA", color=color.orange, linewidth=2)
// Crossover conditions (Fast HMA crossing Slow HMA)
longCondition = ta.crossover(fastHma, slowHma)
shortCondition = ta.crossunder(fastHma, slowHma)
// Strategy entries/exits
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
3. HMA for Support and Resistance
Due to its smooth nature, HMA can often act as dynamic support or resistance. Price bouncing off a rising HMA indicates support, while rejection from a falling HMA indicates resistance.
//@version=5
indicator("HMA Support/Resistance", overlay=true)
length = input.int(20, title="HMA Length", minval=1)
hmaValue = ta.hma(close, length)
plot(hmaValue, title="HMA", color=color.blue, linewidth=2)
// Highlight potential support/resistance touches (conceptual - adjust thresholds)
isSupportTouch = hmaValue > close and hmaValue[1] < close[1] and close > hmaValue * 0.995 and close < hmaValue * 1.005 // Price nearing HMA from below and bouncing
isResistanceTouch = hmaValue < close and hmaValue[1] > close[1] and close < hmaValue * 1.005 and close > hmaValue * 0.995 // Price nearing HMA from above and bouncing
plotshape(isSupportTouch, title="Potential Support", location=location.belowbar, color=color.green, style=shape.circle, size=size.tiny)
plotshape(isResistanceTouch, title="Potential Resistance", location=location.abovebar, color=color.red, style=shape.circle, size=size.tiny)
Optimizing HMA Performance
To get the most from the Hull Moving Average in Pine Script:
- Parameter Tuning: The `length` parameter is the most critical. Experiment with different lengths (e.g., 9, 20, 50, 200) to find what works best for your chosen asset and timeframe. Shorter lengths are more responsive, while longer lengths provide more smoothing for longer-term trends.
- Multi-Timeframe Analysis: Use HMA on higher timeframes to confirm the overall trend and on lower timeframes for precise entries and exits. For example, a 200-period HMA on a daily chart for long-term bias, and a 20-period HMA on an hourly chart for trade timing.
- Combine with Other Indicators: While HMA is designed to reduce noise, it's not a standalone indicator. Pair it with volume indicators, oscillators (like RSI for overbought/oversold, or MACD for momentum confirmation), or price action analysis.
- Avoid Choppy Markets: Despite its smoothing, HMA can still produce false signals in prolonged sideways or non-trending markets. Use a trend strength indicator (e.g., ADX) to confirm a trend is present before relying heavily on HMA signals.
Common HMA Pitfalls
- Whipsaws in Extreme Choppiness: In extremely flat or highly volatile, non-trending markets, HMA can still generate false signals or become less reliable.
- Not a Substitute for Price Action: While HMA can show trend direction, it doesn't provide insight into the underlying supply and demand dynamics that candlestick patterns or chart structures reveal.
- Over-Optimization: Tuning HMA parameters too precisely to historical data can lead to curve-fitting and poor performance in live trading.
- Lack of Overbought/Oversold Information: HMA is a trend-following indicator and does not inherently provide information on overbought or oversold market conditions, unlike oscillators.
Conclusion
The Hull Moving Average (HMA) is a remarkable advancement in the world of technical indicators in Pine Script for TradingView. Its innovative calculation method provides a uniquely smooth line with minimal lag, making it an excellent tool for accurate trend identification and dynamic signal generation. By understanding its construction, applying it thoughtfully within comprehensive trading strategies, and combining it with other analytical tools, you can leverage the HMA to gain a clearer perspective on market direction and improve your trading decisions.
Enhance Your Trading
Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.
This strategy runs in live mode on TradingView, helping you identify potential opportunities.
Subscribe now @ $99/month