Indicator · Pine Script v5 · On the chart

Heikin Ashi with Pivot Trendlines

by g-moe· code credited to goodgains

This indicator overlays moving average clouds, swing pivots, and trade signals directly on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code175
Settings20inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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

175 lines of Pine Script v5 from Tradingview/heikin-ashi-pivot-trendlines.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

heikin-ashi-pivot-trendlines.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("Heikin Ashi with Pivot Trendlines", overlay=true)

// @keywords: heikin-ashi-trend, dual-ma-cloud, filter-ma-cloud, ma-crossover-entries, three-bar-ha-filter, pivot-swing-circles, entry-target-stop-lines, long-short-signals, risk-reward-multiple, pivot-stop-placement, alert-conditions, trade-state-machine, ha-barcolor, configurable-ma-types, swing-pivot-length, target-fill-zones, entry-exit-markers, trend-filter-ma, delayed-cross-entry, pivot-based-stops


ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        
ma1_length = input.int(9, title = 'MA 1', minval = 1, group = 'Indicator Settings', inline = '1')
ma1_type = input.string(title = "", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Indicator Settings", inline = '1')

ma2_length = input.int(20, title = 'MA 2', minval = 1, group = 'Indicator Settings', inline = '2')
ma2_type = input.string(title = "", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Indicator Settings", inline = '2')

long_ma_length = input.int(200, title = 'Filter MA', group = 'Indicator Settings', inline = '3')
long_type = input.string(title = "", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Indicator Settings", inline = '3')

bull_color = input.color(color.green, title = 'Bull Color', group = 'Visual Settings', inline = '1')
bear_color = input.color(color.red, title = 'Bear Color', group = 'Visual Settings', inline = '1')

ma1_color = input.color(color.gray, title = 'MA 1 Color', group = 'Visual Settings', inline = '2')
ma2_color = input.color(color.gray, title = 'MA 2 Color', group = 'Visual Settings', inline = '2')

pivot_length = input.int(3, minval = 2, maxval = 5, title = 'Pivot Length', group="Indicator Settings", inline = '4')
pivot_debug = input.bool(false, title = 'Debug', group="Indicator Settings", inline = '4')

long_trades = input.bool(true, title = 'Long Trades', group="Trade Settings", inline = '1')
short_trades = input.bool(true, title = 'Short Trades', group="Trade Settings", inline = '2')
use_3_bar = input.bool(true, title = 'Use 3 Bar', group="Trade Settings", inline = '3')
use_filter = input.bool(true, title = 'Use Filter', group="Trade Settings", inline = '4')
target_mult = input.float(1.50, minval = 0.01, title = 'Tgt Amount', group="Trade Settings", inline = '5')

ma1_value = ma(close, ma1_length, ma1_type)
ma1_plot = plot(ma1_value, title = 'MA 1', color = ma1_color)
ma2_value = ma(close, ma2_length, ma2_type)
ma2_plot = plot(ma2_value, title = 'MA 2', color = ma2_color)
cloud_color = ma1_value >= ma2_value ? bull_color : bear_color
fill(ma1_plot, ma2_plot, title = 'MA Cloud', color = color.new(cloud_color,80))

longma_value = ma(close, long_ma_length, long_type)
long_color = close >= longma_value ? bull_color : bear_color
longma_plot = plot(longma_value, title = 'MA 1', color = color.new(long_color,50))
mid_lineplot = plot(close >= longma_value ? math.min(close,open) : math.max(close,open), title = 'Long Cloud Mid', color = color.new(#000000,100), display = display.none)
fill(mid_lineplot, longma_plot, color = color.new(long_color,90), title = 'Long MA Cloud')

heikinashi_close = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
heikinashi_open = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)

ha_trend = heikinashi_close > heikinashi_open ? 1 : -1
ha_color = ha_trend == 1 ? bull_color : bear_color
barcolor(ha_color)

var ha_bull = 0
ha_bull := heikinashi_close > heikinashi_open ? 1 : 0
var ha_bear = 0
ha_bear := heikinashi_close > heikinashi_open ? 0 : 1

buy_cross = ta.crossover(ma1_value, ma2_value)
sell_cross = ta.crossunder(ma1_value, ma2_value)

var buy_cond = false
var sell_cond = false

if use_3_bar and use_filter
    buy_cond := ma1_value >= ma2_value and close >= longma_value and buy_cross[2] and math.sum(ha_bull, 3) == 3
    sell_cond := ma1_value < ma2_value and close < longma_value and sell_cross[2] and math.sum(ha_bear, 3) == 3
    
else if use_3_bar and not use_filter
    buy_cond := ma1_value >= ma2_value and buy_cross[2] and math.sum(ha_bull, 3) == 3
    sell_cond := ma1_value < ma2_value and sell_cross[2] and math.sum(ha_bear, 3) == 3
    
else if not use_3_bar and use_filter
    buy_cond := ma1_value >= ma2_value and close >= longma_value and buy_cross
    sell_cond := ma1_value < ma2_value and close < longma_value and sell_cross

else if not use_3_bar and not use_filter
    buy_cond := ma1_value >= ma2_value and buy_cross
    sell_cond := ma1_value < ma2_value and sell_cross
    
else
    buy_cond := false
    sell_cond := false

ph = ta.pivothigh(pivot_length, pivot_length)
pl = ta.pivotlow(pivot_length, pivot_length)

real_offset = pivot_debug ? 0 : -pivot_length

plotshape(ph, title = 'Swing High', location = location.absolute, color = color.white, offset = real_offset, size = size.auto, style = shape.circle)
plotshape(pl, title = 'Swing Low', location = location.absolute, color = color.white, offset = real_offset, size = size.auto, style = shape.circle)

var last_ph = 0.00
var last_pl = 0.00

last_ph := na(ph) ? last_ph[1] : ph
last_pl := na(pl) ? last_pl[1] : pl

//STRATEGY LOGIC
var in_trade = false
var trade_side = 0

var target = 99999999.99
var stop = 0.000000
var entry_price = 0.00

var long_entry = false
var short_entry = false

var tgt_hit = false
var stop_hit = false

var long_exit = false
var short_exit = false

long_entry := long_trades and buy_cond and trade_side[1] != 1
short_entry := short_trades and sell_cond and trade_side[1] != -1

entry_price := long_entry[1] or short_entry[1] ? open :  in_trade == true ? entry_price[1] : na
trade_side := long_entry[1] ? 1 : short_entry[1] ? -1 : in_trade == false ? 0 : trade_side[1]

stop := trade_side == 1 and trade_side[1] != 1 ? last_pl : trade_side == -1 and trade_side[1] != -1 ? last_ph : stop[1]
target := trade_side == 1 and trade_side[1] != 1 ? entry_price + math.abs(entry_price - stop) * target_mult : trade_side == -1 and trade_side[1] != -1 ? 
     entry_price - math.abs(entry_price - stop) * target_mult : target[1]
     
tgt_hit := trade_side == 1 and high >= target or trade_side == -1 and low <= target
stop_hit := trade_side == 1 and low <= stop or trade_side == -1 and high >= stop

long_exit := (tgt_hit or stop_hit) and in_trade[1] == true and trade_side[1] == 1
short_exit := (tgt_hit or stop_hit) and in_trade[1] == true and trade_side[1] == -1

in_trade := long_entry[1] or short_entry[1] ? true : long_exit or short_exit ?  false : in_trade[1]
    
alertcondition(long_entry, title = 'Long Entry', message = 'Long Entry')
alertcondition(short_entry, title = 'Short Entry', message = 'Short Entry')
alertcondition(long_exit, title = 'Long Exit', message = 'Long Exit')
alertcondition(short_exit, title = 'Short Exit', message = 'Short Exit')


//PLOTS
show_signals = true
show_lines = true
entry_color = input.color(color.blue, title = 'Entry Color', group = 'Visual Settings', inline = '3')
tgt_color = input.color(color.green, title = 'Tgt Color', group = 'Visual Settings', inline = '3')
stop_color = input.color(color.red, title = 'Stop Color', group = 'Visual Settings', inline = '3')

plotshape(show_signals and long_entry[1] , style = shape.triangleup, location = location.belowbar, color=bull_color, size=size.small, title = 'Long Entry')
plotshape(show_signals and short_entry[1] , style = shape.triangledown, location = location.abovebar, color=bear_color, size=size.small, title = 'SS Entry')

plotshape(show_signals and short_exit, style = shape.triangleup, location = location.belowbar, color=color.white, size=size.small, title = 'SS Exit')
plotshape(show_signals and long_exit, style = shape.triangledown, location = location.abovebar, color=color.white, size=size.small, title = 'Long Exit')



entry_line = plot(show_lines ? entry_price : na, title = 'Entry Line', color = entry_color, style = plot.style_circles, editable = false)
target_line = plot(show_lines ? target : na, title = 'Target Line', color = tgt_color, style = plot.style_circles, editable = false)
stop_line = plot(show_lines ? stop : na, title = 'Stop Line', color = stop_color, style = plot.style_circles, editable = false)

fill(target_line, entry_line, color = color.new(bull_color,95), title = 'Entry To Target')
fill(stop_line, entry_line, color = color.new(bear_color,95), title = 'Entry To Stop')



if in_trade == false
    target := na
    stop := na

Overview

Heikin Ashi with Pivot Trendlines combines a fast and slow moving average cloud with a long-term trend filter moving average. Trade signals are triggered by moving average crossovers and optional three-bar confirmation filters, using pivot points for stop-loss placement and a configurable risk-to-reward target multiplier. Key settings include moving average types and lengths, pivot detection periods, and trade direction toggles.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages, Support & resistance

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 1int9ma1_length
ma1_typestringEMAma1_type
MA 2int20ma2_length
ma2_typestringEMAma2_type
Filter MAint200long_ma_length
long_typestringEMAlong_type
Bull Colorcolorcolor.greenbull_color
Bear Colorcolorcolor.redbear_color
MA 1 Colorcolorcolor.grayma1_color
MA 2 Colorcolorcolor.grayma2_color
Pivot Lengthint3pivot_length
Debugboolfalsepivot_debug
Show all 20 settings
SettingTypeDefaultVariable
Long Tradesbooltruelong_trades
Short Tradesbooltrueshort_trades
Use 3 Barbooltrueuse_3_bar
Use Filterbooltrueuse_filter
Tgt Amountfloat1.50target_mult
Entry Colorcolorcolor.blueentry_color
Tgt Colorcolorcolor.greentgt_color
Stop Colorcolorcolor.redstop_color
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 6 weeks ago · None stated GitHub repository of SPX 30M - Trading Chart

Ichimoku Cloud

by harryguiacorn
219 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 4 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 2 months ago · None stated GitHub repository of No-Lag MA Trend Levels

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