Strategy · Pine Script v4 · On the chart

Zeguela Supertrend

by danielvm-git· code credited to zeguela

This strategy plots a Supertrend indicator directly on the price chart to generate trend-following trade signals.

View on GitHub

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code120
Settings13inputs
LicenceMIT
Last update2 months ago2 Aug 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

120 lines of Pine Script v4 from supertrend_indicator.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

supertrend_indicator.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This script is a commented version of @everge and @KivancOzbilgic SUPERTREND script. 
// You can find the reference code here: https://www.tradingview.com/script/r6dAP7yi/#tc3873161
// © zeguela
//@version=4
strategy("ZEGUELA SUPERTREND", commission_value=0.07, commission_type=strategy.commission.percent, initial_capital=100, default_qty_value=90, default_qty_type=strategy.percent_of_equity, overlay=true, process_orders_on_close=true)

// Step 1. Script settings

// Input options
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=false)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true)

// Step 2. Calculate indicator values
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

// Step 3. Determine indicator signals
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1

longCondition = buySignal
shortCondition = sellSignal

// Step 4. Output indicator data
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)

//Calculate P&L results
posColor = color.new(color.green, 75)
negColor = color.new(color.red, 75)
dftColor = color.new(color.blue, 75)
posProfit= (strategy.position_size > 0) ? (close * 100 / strategy.position_avg_price - 100) : strategy.position_size < 0 ? (strategy.position_avg_price * 100 / close - 100) : 0.0
posDir   = (strategy.position_size  > 0) ? "long" : strategy.position_size < 0 ? "short" : "flat"
posCol   = (posProfit > 0) ? posColor : (posProfit < 0) ? negColor : dftColor

var label lb = na
label.delete(lb)
lb := label.new(bar_index, max(high, highest(4)[1]), textcolor=color.white,
   color=color.gray,
   text="Open Pos: "+ posDir +
      "\nPnL: "+tostring(posProfit, "#.##")+"%" +
      "\nPreço Médio: "+tostring(strategy.position_avg_price, "#.####") +
      "\nClose: "+tostring(close, "#.####"))

//Closed trades boxes
if longCondition
    var label lL = na
    lL := label.new(bar_index, max(high, highest(4)[1]), textcolor=color.white, yloc=yloc.abovebar, style=label.style_label_down,
       color=posCol,
       text="Closed Pos: "+ posDir +
          "\nPnL: "+tostring(posProfit, "#.##")+"%" +
          "\nEntry Price: "+tostring(strategy.position_avg_price, "#.####") +
          "\nClose Price: "+tostring(close, "#.####"))

if shortCondition
    var label lL = na
    lL := label.new(bar_index, max(high, highest(4)[1]), textcolor=color.white, yloc=yloc.belowbar, style=label.style_label_up,
       color=posCol,
       text="Closed Pos: "+ posDir +
          "\nPnL: "+tostring(posProfit, "#.##")+"%" +
          "\nEntry Price: "+tostring(strategy.position_avg_price, "#.####") +
          "\nClose Price: "+tostring(close, "#.####"))

// Step 5. Strategy setup

// Make input options that configure backtest date range
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromMonth = input(defval = 7, title = "From Month", minval = 1, maxval = 12)
FromYear  = input(defval = 2019, title = "From Year", minval = 999)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 999)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)       
window()  => time >= start and time <= finish ? true : false

//Alert configuration 
alertMessageOpenLong="put you message here"

alertMessageOpenShort="put you message here"

// Step 7. Strategy Execution
if (longCondition)
    strategy.entry("LONG", strategy.long, when = window(), comment = "OPEN LONG", alert_message=alertMessageOpenLong)

if (shortCondition)
    strategy.entry("SELL", strategy.short, when = window(), comment = "OPEN SHORT", alert_message=alertMessageOpenShort)
buy1= barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

// Exit open market position when date range ends
if (not window())
    strategy.close_all()

Overview

Zeguela Supertrend calculates dynamic upper and lower bands using a chosen ATR period, multiplier, and calculation method. Entries trigger when the trend switches direction, issuing buy signals when price closes above the upper band and sell signals when price drops below the lower band. Users can adjust the ATR parameters, toggle signal labels, modify cloud highlighting, and set backtest date ranges. Position sizing defaults to ninety percent of equity. Notably, there are no stop loss or take profit rules.

Details

Published by

danielvm-git on GitHub

Code credit

zeguela, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Trend, Volatility & bands, Breakout

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
ATR Periodint10Periods
Sourcesourcehl2src
ATR Multiplierfloat3.0Multiplier
Change ATR Calculation Method ?booltruechangeATR
Show Buy/Sell Signals ?boolfalseshowsignals
Highlighter On/Off ?booltruehighlighting
Bar Coloring On/Off ?booltruebarcoloring
From Dayint1FromDay
From Monthint7FromMonth
From Yearint2019FromYear
To Monthint1ToMonth
To Dayint1ToDay
Show all 13 settings
SettingTypeDefaultVariable
To Yearint9999ToYear
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 strategy plots VWMA-based Bollinger bands on the 1-minute ES futures price chart for volatility analysis and potential breakout identification. It calculates dynamic long and short stop loss and take profit levels using percentage inputs relative to entry

Strategy v6 Volatility & bandsSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of V1.6 ES - Futures 1M No Repaint
106 stars

This strategy runs on the price chart and enters long when a fast simple moving average crosses above a slow one, and short on the opposite cross, while closing the other side. Entries also need ATR above a filter, a pullback check, a minimum candle-body chang

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 9:22 5 MIN 15 MIN BANKNIFTY

均线突破交易系统

by tangly1024
76 stars

This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the

Strategy v5 TrendMoving averages
Updated 4 months ago · None stated GitHub repository of 均线突破交易系统
16 stars

This trend-following strategy adapts classic Donchian breakout channels according to market volatility regimes. It calculates an ATR percentile rank across a rolling lookback period, shifting dynamically between tighter breakout channels with smaller stops dur

Strategy v6 TrendBreakout
Updated 11 months ago · None stated GitHub repository of Seykota Alt 1: Adaptive Volatility

This strategy executes breakout trades using two Donchian Channels alongside an optional 200-period EMA trend filter. Entries trigger when the price sets a new highest high or lowest low over the primary Donchian Channel period. Exits are managed via a shorter

Strategy v5 BreakoutTrend
Updated 1 year ago · None stated GitHub repository of Ruckard Walcrypto DoubleDonchian B002

Breakout Strategy

by JoelPasapera
11 stars

This strategy implements a channel breakout system directly on the main price chart. It calculates highest highs and lowest lows over configurable entry and exit periods using historical candles. A long entry triggers when the close breaks above the entry chan

Strategy v6 BreakoutTrend
Updated 5 months ago · Apache-2.0 GitHub repository of Breakout Strategy

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