Indicator · Pine Script v6 · On the chart

HA Moving Average

by rubbermetal· code credited to claygrace74

HA Moving Average calculates a moving average from Heikin Ashi prices fetched for the chart symbol, while the chart itself can stay on normal candles.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code57
Settings8inputs
LicenceMPL-2.0
Last update4 months ago10 Jun 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

57 lines of Pine Script v6 from HA Moving Average.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

HA Moving Average.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © claygrace74

//@version=6
indicator("HA Moving Average", shorttitle="HA MA", overlay=true)

// ==================== Settings ====================
var ma_group = "Moving Average"
ma_length = input.int(20, "MA Length", minval=1, group=ma_group)
ma_type   = input.string("EMA", "MA Type", options=["EMA", "SMA", "RMA", "WMA"], group=ma_group)
ma_src    = input.string("Close", "HA Source", options=["Close", "Open", "HL2", "HLC3", "OHLC4"], group=ma_group, tooltip="Which Heikin Ashi price to feed the MA")

var disp_group = "Display"
i_showFill    = input.bool(true, "Show Price/MA Fill", group=disp_group)
i_showSignals = input.bool(true, "Show Cross Signals", group=disp_group)
i_lineWidth   = input.int(3, "Line Width", minval=1, maxval=5, group=disp_group)
i_bullColor   = input.color(color.green, "Bullish Color", group=disp_group)
i_bearColor   = input.color(color.red, "Bearish Color", group=disp_group)

// ==================== Fetch Heikin Ashi Data ====================
ha_ticker = ticker.heikinashi(syminfo.tickerid)
ha_open   = request.security(ha_ticker, timeframe.period, open)
ha_high   = request.security(ha_ticker, timeframe.period, high)
ha_low    = request.security(ha_ticker, timeframe.period, low)
ha_close  = request.security(ha_ticker, timeframe.period, close)

// Select HA source
ha_src = ma_src == "Open" ? ha_open : ma_src == "HL2" ? (ha_high + ha_low) / 2 : ma_src == "HLC3" ? (ha_high + ha_low + ha_close) / 3 : ma_src == "OHLC4" ? (ha_open + ha_high + ha_low + ha_close) / 4 : ha_close

// ==================== Calculate MA from HA Data ====================
ha_ma = ma_type == "SMA" ? ta.sma(ha_src, ma_length) : ma_type == "RMA" ? ta.rma(ha_src, ma_length) : ma_type == "WMA" ? ta.wma(ha_src, ma_length) : ta.ema(ha_src, ma_length)

// Direction — MA slope
ma_rising = ha_ma > ha_ma[1]
ma_color  = ma_rising ? i_bullColor : i_bearColor

// Price relationship
priceAbove = close > ha_ma
crossUp    = ta.crossover(close, ha_ma)
crossDown  = ta.crossunder(close, ha_ma)

// ==================== Plots ====================
pMA    = plot(ha_ma, "HA MA", color=ma_color, linewidth=i_lineWidth)
pClose = plot(close, display=display.none, title="Close")

// Fill between price and MA
fill(pMA, pClose, color=i_showFill ? (priceAbove ? color.new(i_bullColor, 90) : color.new(i_bearColor, 90)) : color(na), title="Price/MA Fill")

// Cross signals
plotshape(i_showSignals and crossUp,   style=shape.triangleup,   location=location.belowbar, color=i_bullColor, text="HA Cross", textcolor=i_bullColor, size=size.tiny, title="Cross Above HA MA")
plotshape(i_showSignals and crossDown, style=shape.triangledown, location=location.abovebar, color=i_bearColor, text="HA Cross", textcolor=i_bearColor, size=size.tiny, title="Cross Below HA MA")

// ==================== Alert Conditions ====================
alertcondition(crossUp,   "Price Crossed Above HA MA", "Price crossed above Heikin Ashi moving average")
alertcondition(crossDown, "Price Crossed Below HA MA", "Price crossed below Heikin Ashi moving average")
alertcondition(ma_rising and not ma_rising[1], "HA MA Turned Bullish", "HA-based MA slope turned positive")
alertcondition(not ma_rising and ma_rising[1], "HA MA Turned Bearish", "HA-based MA slope turned negative")

Overview

Users choose the average type (EMA, SMA, RMA or WMA), the length and the Heikin Ashi source. The line changes color with its slope, a fill shades the gap to price, and markers show where price crosses the line. Alerts are included. It draws on the price chart.

Details

Published by

rubbermetal on GitHub

Code credit

claygrace74, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Moving averages

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.

Settings

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

SettingTypeDefaultVariable
MA Lengthint20ma_length
MA TypestringEMAma_type
HA SourcestringClosema_src
Show Price/MA Fillbooltruei_showFill
Show Cross Signalsbooltruei_showSignals
Line Widthint3i_lineWidth
Bullish Colorcolorcolor.greeni_bullColor
Bearish Colorcolorcolor.redi_bearColor
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

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 11 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 2 days ago · MIT GitHub repository of NVDA Trend Ribbon
257 stars

Donald SPX - Options Trading is an overlay indicator designed for intraday SPX analysis. It plots a 200-period Weighted Moving Average (WMA) on the price chart while evaluating multi-timeframe market structure across Daily, 15-minute, 5-minute, and 2-minute ba

Indicator v5 Moving averagesTrend Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald SPX - Options Trading

60K均線

by TaichiS
219 stars

This multi-timeframe indicator calculates and plots 20-period and 60-period simple moving averages from the 60-minute timeframe directly onto the price chart. The 20-period average displays as a green line, while the 60-period average appears in blue. It serve

Indicator v4 Moving averagesTrend Repaints
Updated 5 years ago · None stated GitHub repository of 60K均線

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers