Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

Supertrend Indicator – PineScript – Tradingview Charts

5 min read

This Supertrend Pine Script code plots the Supertrend indicator on TradingView charts with an ATR period of 10 and a multiplier of 3, marks Buy Mode and Sell Mode labels on trend flips, colours the bars and fires alerts. Below you will find the original Pine Script v4 code I wrote for Nifty futures, an updated Pine Script v6 version for 2026, and how to add it to your chart.

Supertrend: Pine Script Indicator with Nifty Future charts

Key takeaways
  • Supertrend is an ATR based trailing stop that flips between an up trend line (green) and a down trend line (red).
  • Default settings in this script: ATR period 10, multiplier 3, source hl2.
  • The original code is Pine Script v4; an updated v6 port with the same logic is included below.
  • TradingView also has a built-in Supertrend indicator and a ta.supertrend() function in Pine v5 and v6.

About This Supertrend Indicator

This is my first indicator from a series of Pinescript Indicators. And of course, supertrend is one of my favorite indicators. So love to take this opportunity to code it in Pinescript supported by Tradingview charts with a huge community following.

Pine Script V6 AI Masterclass

TRADINGVIEW

Pine Script V6 AI Masterclass

Write, debug and backtest TradingView indicators and strategies faster with AI as your coding partner.

  • Pine Script V6 from the ground up
  • Indicators, strategies and alerts
  • AI-assisted coding workflow

Already Supertrend is enough spoken here. Those who are very new to Supertrend can follow this article here 10 things to know about Supertrend v2.0 AFL code

Tradingview users can use the Supertrend Indicator from the Public Library

How the Supertrend Indicator Is Calculated

  1. Compute the Average True Range (ATR) over the chosen period (10 bars here).
  2. Upper band (long stop) = source (hl2) minus multiplier x ATR. While the previous close stays above it, the band can only move up.
  3. Lower band (short stop) = source plus multiplier x ATR. While the previous close stays below it, the band can only move down.
  4. The trend flips to +1 (buy) when the close crosses above the short stop, and to -1 (sell) when it closes below the long stop.

A larger multiplier or ATR period gives fewer, slower signals; a smaller one reacts faster but whipsaws more in sideways markets.

Scale Settings to Apply Supertrend V1.0 in Tradingview

Do the scale settings as shown in the image below.

Note: this scale screenshot dates from 2014 when the first version of this script was published. Current TradingView versions handle overlay scaling automatically for indicators declared with overlay = true, so you normally do not need to change any scale settings.

Supertrend Pine Script Code (Pine Script v4)

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © marketcalls_in
// Rajandran - Founder - Marketcalls  / Co-Founder - Algomojo
//@version=4
study("Supertrend - Buy or Sell Signal", overlay = true)



//inputs
Periods = input(title="ATR Period", type=input.integer, defval=10)
Source = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)



//Compute ATR Levels
atr=  atr(Periods) 


//Creating Upper Channel

up=Source-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up

//Creating Down Channel
dn=Source+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn


//Compute the Trend Stream +1/-1
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

//Create Stoploss for Longs
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)

//Buy Signal
Buy = trend == 1 and trend[1] == -1

plotshape(Buy ? up : na, title="Go Long", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(Buy ? up : na, title="Buy", text="Buy Mode", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)

//Sell Signal
Sell = trend == -1 and trend[1] == 1

plotshape(Sell ? dn : na, title="Go Short", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(Sell ? dn : na, title="Sell", text="Sell Mode", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

iPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

longFillColor =  trend == 1 ? color.green : color.white
shortFillColor = trend == -1 ? color.red : color.white

fill(iPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(iPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)



//Alerts
alertcondition(Buy, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(Sell, title="SuperTrend Sell", message="SuperTrend Sell!")


buycontiue = barssince(Sell) > barssince(Buy)
sellcontinue = barssince(Buy) > barssince(Sell)

color = buycontiue[1] ? color.green : sellcontinue ? color.red : na
barcolor(color)

Updated Version (2026): Supertrend Pine Script v6

TradingView released Pine Script v6 in December 2024. Scripts written in earlier versions keep working, but new features arrive only in v6. Here is the same logic ported to v6: study() becomes indicator(), inputs use input.int(), input.float() and input.source(), built-ins move to the ta. and math. namespaces, the removed transp argument is replaced by color.new(), and the trend variable is declared with var. The variable named color is renamed to barColor because it clashes with the built-in type name.

//@version=6
// Updated version (2026) of the Supertrend - Buy or Sell Signal indicator
// Same logic as the v4 code above, ported to Pine Script v6
indicator("Supertrend - Buy or Sell Signal", overlay = true)

//inputs
Periods    = input.int(10, title = "ATR Period")
Source     = input.source(hl2, title = "Source")
Multiplier = input.float(3.0, title = "ATR Multiplier", step = 0.1)

//Compute ATR Levels
atrValue = ta.atr(Periods)

//Creating Upper Channel
up = Source - Multiplier * atrValue
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up

//Creating Down Channel
dn = Source + Multiplier * atrValue
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn

//Compute the Trend Stream +1/-1
var int trend = 1
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

//Create Stoploss for Longs
upPlot = plot(trend == 1 ? up : na, title = "Up Trend", style = plot.style_linebr, linewidth = 2, color = color.green)

//Buy Signal
Buy = trend == 1 and trend[1] == -1
plotshape(Buy ? up : na, title = "Go Long", location = location.absolute, style = shape.circle, size = size.tiny, color = color.green)
plotshape(Buy ? up : na, title = "Buy", text = "Buy Mode", location = location.absolute, style = shape.labelup, size = size.tiny, color = color.green, textcolor = color.white)

dnPlot = plot(trend == 1 ? na : dn, title = "Down Trend", style = plot.style_linebr, linewidth = 2, color = color.red)

//Sell Signal
Sell = trend == -1 and trend[1] == 1
plotshape(Sell ? dn : na, title = "Go Short", location = location.absolute, style = shape.circle, size = size.tiny, color = color.red)
plotshape(Sell ? dn : na, title = "Sell", text = "Sell Mode", location = location.absolute, style = shape.labeldown, size = size.tiny, color = color.red, textcolor = color.white)

//Invisible mid-price plot used only as the fill anchor
iPlot = plot(ohlc4, title = "Mid Price", color = na)

longFillColor  = trend == 1 ? color.new(color.green, 90) : na
shortFillColor = trend == -1 ? color.new(color.red, 90) : na

fill(iPlot, upPlot, title = "UpTrend Highlighter", color = longFillColor)
fill(iPlot, dnPlot, title = "DownTrend Highlighter", color = shortFillColor)

//Alerts
alertcondition(Buy, title = "SuperTrend Buy", message = "SuperTrend Buy!")
alertcondition(Sell, title = "SuperTrend Sell", message = "SuperTrend Sell!")

buycontinue  = ta.barssince(Sell) > ta.barssince(Buy)
sellcontinue = ta.barssince(Buy) > ta.barssince(Sell)

barColor = buycontinue[1] ? color.green : sellcontinue ? color.red : na
barcolor(barColor)

If you only need the Supertrend line, Pine v5 and v6 also provide a built-in function: [supertrend, direction] = ta.supertrend(factor, atrPeriod), where a negative direction means an up trend. The custom code above is kept because it reproduces the exact signals, labels and bar colours of the original script.

How to Add the Supertrend Script to TradingView

  1. Open a chart in TradingView and click Pine Editor at the bottom of the screen.
  2. Create a new indicator, delete the template code and paste the v6 code above.
  3. Click Save, then Add to chart.
  4. Open the indicator settings to change the ATR Period, Source and ATR Multiplier.
  5. To get notified, create an alert and choose the SuperTrend Buy or SuperTrend Sell condition.

Supertrend Trading Tips

Frequently Asked Questions

What are the best Supertrend settings for TradingView?

The common default is ATR period 10 and multiplier 3, which this script uses. Intraday traders sometimes use a smaller multiplier for faster signals, but test any change on your own instrument and timeframe.

Is Supertrend available as a built-in indicator in TradingView?

Yes. TradingView has a built-in Supertrend indicator, and Pine Script v5 and v6 provide the ta.supertrend() function. Custom scripts like this one add labels, fills, bar colours and alerts.

Does the Supertrend indicator repaint?

The signal can change on the current, still-forming bar as price moves, but once a bar closes the Supertrend value and any flip on that bar are fixed.

How do I convert old Pine Script code to v6?

Open the script in the Pine Editor and use the Convert code to v6 option, then fix any remaining errors, such as removed arguments like transp or variable names that clash with built-in types.

Marketcalls Elite Traders Club

MEMBERSHIP

Marketcalls Elite Traders Club

Every Marketcalls course and live mentorship in one membership: 127 courses and 2,500+ hours of learning.

  • Market Profile, Orderflow, AFL, Pine Script and Python
  • Live mentorship and TradEdge trading room
  • Premium tools and the Elite Slack community

Learn by watching: free videos every week

Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

17 Replies to “Supertrend Indicator – PineScript – Tradingview Charts”

  1. Dear Rajandran

    Can you pls make the symbol search easier in your EOD charts of Tradingview. Like Google when you type the first two or three letters we can choose the symbol. This will be helpful

    Regards,

    Veer

  2. Dear Rajendaran

    I am unable to apply supertrend indicator in Amibroker.. When I try to edit the formula it shows error??

    Please help me

    Regards

    Dhananjay

  3. Last week i was seeing EOD charts with RSI and stochastic RSI signals but now i have lost site where i can see these charts now? kindly inform on my mail if possible.

  4. Dear sir
    i m using trading view for trading in nasdaq. i have also subscribe for trading view live data for cme. i m using ur super trend indicator. pls help me to convert super trend indicator code base to strategy code so i can back test and foreword test in pine script.

  5. Hello Rajandran

    I have just come across your SuperTrend V1.0 script and it’s awesome. What’s the current version and can I have it?

    Thanks

    Jo

  6. Dear Rajandran

    Can you tell me if the Supertrend V1.0 – Buy or Sell Signal indicator is available for MT4?

    If so I would like to carry it over to my broker platform for ease of execution.

    With regards.

  7. Hello Rajandran,

    I found your script about Supertrend indicator very interesting.

    However, the is something in it I do not understand. Perhaps you could leave me some lines.

    The calculation of variable TrendUp uses TrendUp[1]. But this previous TrendUp seems to me that is never calculated. For example, there is no “once” statement.

    How is TrendUp getting its first value in the script? Once I understand where this first value comes from, I will fully understand.

    Thank you in advance

    1. Initally TrendUp sets with null values automatically. So TrendUp[0] is initialized to null and rest other calculation does in recursive mode.

  8. Dear Rajendran,

    Wonderful work, can you please share latest script v4 for TradingView? I can see only v1 in your page also in Trading view?

  9. Hello Rajendran,
    I am using your Supertend script v1.0 in the free version of Tradingview. I wanted also the default MACD indicator alolng with your Supertrend. I treid to modify your script, but getting some error.
    Can you please help me?

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More