Pine Script Hull Moving Average (HMA)

Master this remarkably smooth and low-lagging moving average in TradingView's Pine Script for precise trend identification and reversal signals.

Subscribe now @ $99/month

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):

  1. 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`).
  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`).
  3. 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)

Key Advantage: HMA minimizes lag by giving more weight to recent prices and smoothing the result, making it highly effective for trend identification.

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.

//@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:

Smoothness vs. Lag: HMA is a balance. While it reduces lag significantly, it's not entirely lag-free. Always be aware of the trade-off, especially on very short timeframes.

Common HMA Pitfalls

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