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

- 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.
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
- Compute the Average True Range (ATR) over the chosen period (10 bars here).
- Upper band (long stop) = source (hl2) minus multiplier x ATR. While the previous close stays above it, the band can only move up.
- Lower band (short stop) = source plus multiplier x ATR. While the previous close stays below it, the band can only move down.
- 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
- Open a chart in TradingView and click Pine Editor at the bottom of the screen.
- Create a new indicator, delete the template code and paste the v6 code above.
- Click Save, then Add to chart.
- Open the indicator settings to change the ATR Period, Source and ATR Multiplier.
- To get notified, create an alert and choose the SuperTrend Buy or SuperTrend Sell condition.
Supertrend Trading Tips
- Supertrend works best in trending markets; in narrow ranges expect frequent flips. An EMA filter helps, as shown in Supertrend v4.0 with EMA filter in Pine Script.
- Use the green or red line as a trailing stop rather than a fixed target.
- Test settings before trading them live. My VectorBT Supertrend grid optimisation tutorial shows how to scan many ATR and multiplier combinations in Python.
- Amibroker users can use Supertrend V4.0 AFL code for the same logic.
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.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.


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
We are still in Beta. You can expect the feature once we are going live with our own feeds
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
Its designed for Tradingview charts for Amibroker use the Supertrend AFL code link here
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.
you can get it at http://www.marketcallls.in/eodchart
Can you please provide “tradescript” code for Supertrend indicator ?
What is the timeframes to use with this indicator?
Thank you
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.
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
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.
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
Initally TrendUp sets with null values automatically. So TrendUp[0] is initialized to null and rest other calculation does in recursive mode.
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?
supertrend v4 is not codeable as of now in Trading View due to pinescript feature. May be in the later days.
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?
how to code close> supertrend?