Supertrend Indicator Guide
Master this indicator with Pine Script code and automated strategies.
What is Supertrend Indicator?
Supertrend is a trend-following indicator based on ATR (Average True Range). It plots a line that follows price and flips color/position when the trend changes. Green below price = uptrend, red above price = downtrend.
How to Use This Strategy
Pro Tip: Buy when price closes above the Supertrend line (line turns green). Sell when price closes below (line turns red). Use the line as a trailing stop-loss level.
Pine Script Code
Copy and paste this code into your TradingView Pine Editor.
PINE SCRIPT V5
//@version=5
indicator("Supertrend", overlay=true)
// Inputs
atrPeriod = input.int(10, "ATR Period")
multiplier = input.float(3.0, "ATR Multiplier")
// Calculate ATR
atr = ta.atr(atrPeriod)
// Calculate Supertrend
upperBand = hl2 + multiplier * atr
lowerBand = hl2 - multiplier * atr
var float supertrend = na
var int direction = 1
prevSupertrend = supertrend[1]
prevUpperBand = upperBand[1]
prevLowerBand = lowerBand[1]
if na(prevSupertrend)
supertrend := upperBand
else
if prevSupertrend == prevUpperBand
supertrend := close > upperBand ? lowerBand : math.min(upperBand, prevUpperBand)
else
supertrend := close < lowerBand ? upperBand : math.max(lowerBand, prevLowerBand)
direction := supertrend < close ? 1 : -1
// Plot
plot(supertrend, "Supertrend", color=direction == 1 ? color.green : color.red, linewidth=2)
// Signals
buySignal = direction == 1 and direction[1] == -1
sellSignal = direction == -1 and direction[1] == 1
plotshape(buySignal, "Buy", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(sellSignal, "Sell", shape.triangledown, location.abovebar, color.red, size=size.small)