Open-Source Pine Script v5 Guide

Supertrend Indicator

Institutional execution rules, audit-clean Pine Script v5 source code, and automated multi-ticker scanning.

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.

Execution Rules & Setup Playbook

Institutional Playbook: 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 v5 Source Code

TradingView Pine Editor → Paste → Add to Chart
PINE SCRIPT V5 · UNLOCKED SOURCE
//@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)

Related Pine Script v5 Guides

Scan Without Coding

Skip single-chart limits — Tradestie monitors this setup across 5,000+ US stocks automatically.

Server-Side Advantage Our Trendline Scanner uses similar trend-following logic to identify stocks breaking above key resistance levels with momentum confirmation.
  • Real-time multi-ticker signal scanning
  • Institutional options & GEX confirmation
  • Automated email alerts on triggers
Launch Live Scanner
Check Any Stock Right Now