What it measures
Supertrend is a trend-following line that sits below price in an uptrend and above price in a downtrend. Its distance from price is set by volatility (the ATR), and it only moves in the trend's favour: up during an uptrend, down during a downtrend. When price closes through it, the line flips to the other side.
In effect it's an ATR trailing stop drawn as an indicator. That makes it popular both as a direction filter and as a ready-made stop level.
The formula in words
- Compute the ATR over N bars (10 is common) and multiply it by a factor (3 is common).
- Basic upper band = (high + low) ÷ 2 + factor × ATR. Basic lower band = (high + low) ÷ 2 − factor × ATR.
- The final lower band only rises: it takes the higher of today's basic lower band and yesterday's final lower band, unless the previous close was below yesterday's band, in which case it resets. The final upper band only falls, by the same logic.
- While the trend is up, Supertrend is the final lower band. When a close falls below it, the trend turns down and Supertrend switches to the final upper band, and vice versa.
Pine Script v6 has this built in as ta.supertrend(factor, atrPeriod), which returns the line and a direction value: negative for an uptrend, positive for a downtrend.
How traders read it
- Direction. Line below price: uptrend on this timeframe. Line above price: downtrend. Many traders only take trades that agree with Supertrend, using another tool for the actual entry.
- Flips as signals. A flip is a close through the trailing line. In trends, flips catch the bulk of a move; in ranges, they come in quick succession and each one costs a little.
- As a stop. Traders in a long position may trail their stop at the Supertrend line and exit on a flip. The ATR stop-loss calculator shows the same idea as a one-off level.
- Higher-timeframe bias. A daily Supertrend direction used as a filter on an hourly chart is a common combination; keep the request non-repainting (see multi-timeframe Pine Script without repainting).
Common settings
| ATR length | Factor | Character |
|---|---|---|
| 10 | 3 | The usual default. Moderate distance, moderate number of flips. |
| 10 | 2 | Tighter line, earlier flips, more whipsaws. |
| 14 to 22 | 3 to 4 | Wider line that stays with longer trends. |
The factor matters more than the length. Doubling the factor roughly halves the number of flips, and each loss on a flip gets larger.
Pine Script v6 code
The complete, self-contained script is below. Copy it into a new indicator in TradingView's Pine Editor, or download the .pine file.
//@version=6
indicator("Supertrend with flip signals", shorttitle = "Supertrend", overlay = true)
atrLen = input.int(10, "ATR length", minval = 1)
factor = input.float(3.0, "Factor", minval = 0.1, step = 0.1)
[st, dir] = ta.supertrend(factor, atrLen)
// dir < 0: uptrend (line below price). dir > 0: downtrend (line above price).
upTrend = dir < 0
flip = ta.change(dir)
flipUp = flip < 0
flipDown = flip > 0
bodyMid = plot((open + close) / 2, "Body middle", display = display.none)
upLine = plot(upTrend ? st : na, "Uptrend", color = color.new(color.teal, 0), linewidth = 2, style = plot.style_linebr)
downLine = plot(upTrend ? na : st, "Downtrend", color = color.new(color.red, 0), linewidth = 2, style = plot.style_linebr)
fill(bodyMid, upLine, color = color.new(color.teal, 88), fillgaps = false, title = "Uptrend fill")
fill(bodyMid, downLine, color = color.new(color.red, 88), fillgaps = false, title = "Downtrend fill")
plotshape(flipUp, "Turns up", shape.labelup, location.belowbar, color.new(color.teal, 0), text = "Up", textcolor = color.white, size = size.tiny)
plotshape(flipDown, "Turns down", shape.labeldown, location.abovebar, color.new(color.red, 0), text = "Down", textcolor = color.white, size = size.tiny)
alertcondition(flipUp, "Supertrend turns up", "Supertrend flipped up on {{ticker}} ({{interval}}) at {{close}}")
alertcondition(flipDown, "Supertrend turns down", "Supertrend flipped down on {{ticker}} ({{interval}}) at {{close}}")The script uses the built-in ta.supertrend() and draws the line in two colours, one for each direction, with a light fill between the line and the candle bodies. Labels mark each flip, and two alert conditions fire when the direction changes.
Flips are decided by the close, so a flip on the live bar is provisional until that bar closes. Set alerts to Once per bar close.
Add it to your chart
- Open any chart on TradingView and open the Pine Editor.
- Create a new indicator, delete the template code and paste the script.
- Click Save, then Add to chart. Change lengths and levels from the indicator's settings.
Stuck on an error? See how to add a Pine Script indicator to TradingView.
Pitfalls
- Ranges. In sideways markets Supertrend flips back and forth, and every flip is a small loss if you trade them. Pair it with a trend-strength filter such as ADX.
- Tight factors. A low factor feels responsive in hindsight and produces many more whipsaws in real time.
- Gaps. A stop at the Supertrend line is a level, not a guarantee. Price can gap past it over weekends or on news.
- Live-bar flips. The line can flip and flip back within one unfinished bar. Act on closed bars.
Related reading
- Pine Script strategy.exit(): stop loss and take profit that behave as you expect
- MQL5 trailing stop: points, ATR and break-even code for MT5 Expert Advisors
- Multi-timeframe Pine Script with request.security(), without repainting
- ATR Stop-Loss Calculator (free tool)
- Position Size Calculator (free tool)
The script is an educational example: test it on your own markets before relying on it. Indicators describe the past; they don't predict the future. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.