Logo
OFFLINEPIXEL
/ pinescript-strategies / pine-script-hma

$ Pinescript Hull Moving Average (HMA)

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

500+ Clients Helped
100% Satisfaction
Live Trading Ready
⚠️
Trading financial markets carries risk. All content (PineScript code, indicators, strategies) on this website is for educational purposes only. Past performance is not indicative of future results. By using any code or information from this site, you agree that you are solely responsible for your trading decisions. The author disclaims all liability for any losses incurred. To gain from experts experiences, You can always try our Invite Only Scripts
01

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 Pinescript, 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.

02

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.
03

Basic HMA Implementation in Pinescript

pine-script@terminal
//@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)
$ ✓ Compiled successfully
04

Key Advantage

Key Advantage

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

05

Practical HMA Strategies

06

1. HMA as a Trend Direction Filter (Color Change)

pine-script@terminal
//@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)
$ ✓ Compiled successfully
07

2. HMA Crossover Strategy (with Price or another HMA)

pine-script@terminal
//@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)
$ ✓ Compiled successfully
08

3. HMA for Support and Resistance

pine-script@terminal
//@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.new(color.green, 0), style=shape.circle, size=size.tiny)
plotshape(isResistanceTouch, title="Potential Resistance", location=location.abovebar, color=color.new(color.red, 0), style=shape.circle, size=size.tiny)
$ ✓ Compiled successfully
09

Optimizing HMA Performance

To get the most from the Hull Moving Average in Pinescript:

  • 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.
10

Smoothness vs. Lag

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.

11

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.
12

Conclusion

Conclusion

The Hull Moving Average (HMA) is a remarkable advancement in the world of technical indicators in Pinescript 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 Pinescript 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.

Get Pinescript Strategy