Indicator · Pine Script v6 · On the chart

HEMA Trend Levels [AlgoAlpha]

by theonetrade· code credited to AlgoAlpha

HEMA Trend Levels by AlgoAlpha calculates fast and slow Hull Exponential Moving Averages (HEMA) to identify market trend direction.

View on GitHub

GitHub stars34on the repository
Pine versionv6current
Lines of code83
Settings4inputs
LicenceMIT
Last update2 weeks ago8 Nov 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

83 lines of Pine Script v6 from assets/indicator/bit-exact/hema_trend_levels_algoalpha_WClHCU5Q.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

hema_trend_levels_algoalpha_WClHCU5Q.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoAlpha
//@version=6
indicator("HEMA Trend Levels [AlgoAlpha]", "AlgoAlpha - HEMA Trend ", overlay=true, max_boxes_count = 500, explicit_plot_zorder = true)

// Inputs
hemaLength1 = input.int(20, title="HEMA Length Fast", group="HEMA Settings", tooltip="First HEMA period length (20 by default)")
hemaLength2 = input.int(40, title="HEMA Length Slow", group="HEMA Settings", tooltip="Second HEMA period length (40 by default)")

bullColor = input.color(#00ffbb, title="Bullish Color", tooltip="Color used for bullishness", group="Appearance")
bearColor = input.color(#ff1100, title="Bearish Color", tooltip="Color used for bearishness", group="Appearance")

// HEMA Function
f_hema(_src, _length) =>
    halfLength = math.max(1, math.round(_length / 2))
    sqrtLength = math.max(1, math.round(math.sqrt(_length)))
    ema_half = ta.ema(_src, halfLength)
    ema_full = ta.ema(_src, _length)
    diff = 2 * ema_half - ema_full
    ta.ema(diff, sqrtLength)

// Calculate HEMA values
hema1 = f_hema(close, hemaLength1)
hema2 = f_hema(close, hemaLength2)

// Create hidden plots for gradient fill
hemaTop = plot(hema1 > hema2 ? hema1 : hema2, "Top HEMA", display=display.none)
hemaBottom = plot(hema1 > hema2 ? hema2 : hema1, "Bottom HEMA", display=display.none)

// Determine fill color based on trend direction
fillColor = hema1 > hema2 ? bullColor : bearColor

fill(hemaTop, hemaBottom, hema1, hema2, color.new(chart.bg_color, 100), color.new(fillColor, 70), "HEMA Gradient")

// Plotting with dynamic colors
plot(hema1, title="HEMA 20", color=color.new(fillColor, 70))
plot(hema2, title="HEMA 40", color=color.new(fillColor, 70))

// Define conditions
bullishCondition = close > hema1 and hema1 > hema2
bearishCondition = close < hema1 and hema1 < hema2
grayCondition = (close < hema1 and hema1 > hema2) or (close > hema1 and hema1 < hema2)

// Apply bar coloring
barcolor(bullishCondition ? color.new(bullColor, 50) : bearishCondition ? color.new(bearColor, 50) : grayCondition ? color.new(color.gray, 50) : na)

vola = ta.atr(14)/2

var bull_box = array.new_box()
var bear_box = array.new_box()

if ta.crossover(hema1, hema2)
    bull_box.unshift(box.new(bar_index, low + vola, bar_index+5, low, bgcolor = color.new(bullColor, 70), border_color = color.new(color.black, 100)))
else if ta.crossunder(hema1, hema2)
    bear_box.unshift(box.new(bar_index, high - vola, bar_index+5, high, bgcolor = color.new(bearColor, 70), border_color = color.new(color.black, 100)))

low_test = 0.0
high_test = 0.0

if bull_box.size() > 0
    Q = bull_box.first()
    if low < Q.get_top() and high > Q.get_top() and open > Q.get_top() and close > Q.get_top()
        low_test := Q.get_bottom()
    Q.set_right(bar_index)

if bear_box.size() > 0
    Q = bear_box.first()
    if high > Q.get_top() and low < Q.get_top() and open < Q.get_top() and close < Q.get_top()
        high_test := Q.get_bottom()
    Q.set_right(bar_index)

plotchar(low_test != 0.0 ? low_test - vola : na, "low_test", '▲', location.absolute, bullColor, size = size.tiny)
plotchar(high_test != 0.0 ? high_test + vola : na, "high_test", '▼', location.absolute, bearColor, size = size.tiny)
plotshape(ta.crossover(hema1, hema2) ? low - vola * 3 : na, "Bullish Trend", shape.labelup, location.absolute, color.new(bullColor, 50), text = "▲", textcolor = chart.fg_color, size = size.small)
plotshape(ta.crossunder(hema1, hema2) ? high + vola * 3 : na, "Bearish Trend", shape.labeldown, location.absolute, color.new(bearColor, 50), text = "▼", textcolor = chart.fg_color, size = size.small)

// Alert conditions for trend changes
alertcondition(ta.crossover(hema1, hema2), title="Bullish Crossover", message="HEMA 20 crossed above HEMA 40 - Bullish Trend")
alertcondition(ta.crossunder(hema1, hema2), title="Bearish Crossunder", message="HEMA 20 crossed below HEMA 40 - Bearish Trend")

// Alert conditions for price tests
alertcondition(low_test != 0.0, title="Bullish Test", message="Price tested bullish box support")
alertcondition(high_test != 0.0, title="Bearish Test", message="Price tested bearish box resistance")

Overview

HEMA Trend Levels [AlgoAlpha] plots both HEMA lines directly on the price chart with dynamic color fills between them. The indicator recolors price bars based on trend alignment and generates dynamic support and resistance boxes on HEMA crossovers. Additionally, it highlights level retest interactions by plotting signal shapes and arrows using ATR-based volatility offsets.

Details

Published by

theonetrade on GitHub

Code credit

AlgoAlpha, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
HEMA Length Fastint20hemaLength1
HEMA Length Slowint40hemaLength2
Bullish Colorcolor#00ffbbbullColor
Bearish Colorcolor#ff1100bearColor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

257 stars

This indicator analyzes short-term price action on index futures and options charts by applying Rational Quadratic kernel smoothing to detect sideways consolidation ranges and potential breakouts. It processes pivot highs and lows over customizable loopback pe

Indicator v5 Support & resistanceTrend Repaints
Updated 2 months ago · None stated GitHub repository of SPX 30M - Trading Chart

Ichimoku Cloud

by harryguiacorn
220 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud

MA Envelope (MAE)

by mihakralj
99 stars

The MA Envelope indicator calculates upper and lower channel bands shifted by a user-defined percentage around a baseline moving average. It supports Simple Moving Average, Exponential Moving Average, and Weighted Moving Average calculation types. The script p

Indicator v6 Moving averagesSupport & resistance
Updated 8 weeks ago · Apache-2.0 GitHub repository of MA Envelope (MAE)
56 stars

This indicator calculates an Exponential Moving Average and a Zero-Lag Moving Average to identify market trends on the price chart. It plots both moving averages and fills the space between them with color to highlight trend direction. Upon a crossover or cros

Indicator v5 TrendMoving averages
Updated 3 months ago · None stated GitHub repository of No-Lag MA Trend Levels

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers