Strategy · Pine Script v5 · On the chart

Heikin Ashi with Pivot Trendlines Strategy

by g-moe· code credited to goodgains

This strategy plots dual moving average clouds and a long-term trend filter directly on the price chart alongside Heikin Ashi data.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code151
Settings17inputs
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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

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

heikin-ashi-pivot-trendlines-strategy.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
strategy("Heikin Ashi with Pivot Trendlines Strategy", overlay=true)

// @keywords: heikin-ashi, pivot-trendlines, ma-cloud, ma-crossover, pivot-high, pivot-low, strategy-entry, strategy-exit, profit-target, stop-loss, swing-high, swing-low, long-trades, short-trades, filter-ma, three-bar-rule, entry-line, target-line, stop-line, ha-trend

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

var stop_price = 0.00
var profit_target = 0.00

//LONGS
if strategy.position_size > 0 
    
    if strategy.position_avg_price != strategy.position_avg_price[1] or na(strategy.position_avg_price[1])
        stop_price := last_pl
        profit_target := strategy.position_avg_price + math.abs(strategy.position_avg_price - stop_price) * target_mult
        
    else
        stop_price := stop_price[1]
        profit_target := profit_target[1]
        
    strategy.exit("Longs", limit = profit_target, stop = stop_price, comment = 'TP/SL')

//SHORTS
else if strategy.position_size < 0
    
    if strategy.position_avg_price != strategy.position_avg_price[1] or na(strategy.position_avg_price[1])
        stop_price := last_ph
        profit_target := strategy.position_avg_price - math.abs(strategy.position_avg_price - stop_price) * target_mult
        
    else
        stop_price := stop_price[1]
        profit_target := profit_target[1]
        
    strategy.exit("Shorts", limit = profit_target, stop = stop_price, comment = 'TP/SL')

else
    stop_price := na
    profit_target := na
    
entry_line = plot(strategy.position_avg_price, title = 'Entry', color = color.gray, style = plot.style_cross)
stop_line = plot(stop_price, title = 'Stop', color = bear_color, style = plot.style_cross)
target_line = plot(profit_target, title = 'Target', color = bull_color, style = plot.style_cross)

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 (buy_cond and long_trades)
    strategy.entry("LE", strategy.long)

if (sell_cond and short_trades)
    strategy.entry("SS_E", strategy.short)

Overview

Heikin Ashi with Pivot Trendlines Strategy triggers long and short entry signals using moving average alignments and pivot trendline levels, with optional filtering via a three-bar confirmation rule and a 200-period baseline average. Trades exit using a customizable profit target multiplier and swing stop levels. User settings control moving average lengths, calculation types, pivot detection lengths, and trade direction toggles.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages, Support & resistance, Candlestick patterns

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 17 settings
SettingTypeDefaultVariable
Long Tradesbooltruelong_trades
Short Tradesbooltrueshort_trades
Use 3 Barbooltrueuse_3_bar
Use Filterbooltrueuse_filter
Tgt Amountfloat1.50target_mult
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

Omar Edited WF

by Alorse
330 stars

Omar Edited WF is a trading strategy overlay built around Williams Fractals and multiple Exponential Moving Averages. It detects high and low fractal pivot points over configurable lookback periods alongside three adjustable EMA trendlines. Long entries are ma

Strategy v5 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of Omar Edited WF
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 6 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

This strategy calculates and plots smoothed Heikin-Ashi candles directly onto the main price chart using exponential moving averages of open, high, low, and close prices. It enters a long position after three consecutive bullish smoothed candles with sequentia

Strategy v4 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of Abdoli's Heikin Ashi Smoothed Buy & Sell Strategy Rev.4
104 stars

This strategy combines three Exponential Moving Averages with Williams Fractals to execute trend-following or mean-reverting trades. It analyzes price structure using short, medium, and long-term EMAs along with fractal highs and lows. Entries trigger when fra

Strategy v5 TrendMoving averages
Updated 3 years ago · MIT GitHub repository of 3MA and Fractals Strategy

This strategy executes trades based on pivot levels, an exponential moving average, and a multi-stage smoothing trend line. It identifies support and resistance points from pivot highs and lows, combining them with price relationship rules against a custom smo

Strategy v5 TrendMoving averages Repaints
Updated 2 years ago · None stated GitHub repository of Binancash - Candle Strategy Futures on Binance
13 stars

This strategy generates entry signals using pivot points and uses custom Curie moving average closing lines for exiting positions. It features risk management components including percentage-based stop loss, take profit, trailing stops, and break-even levels,

Strategy v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of BitLa PivotPoints A002

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers