ta.supertrend()Supertrend Direction: Negative Means Uptrend
ta.supertrend() returns [supertrendValue, direction]. A negative direction (-1) means uptrend; a positive direction (+1) means downtrend. Use ta.crossunder(direction, 0) for a switch to uptrend and ta.crossover(direction, 0) for a switch to downtrend. These describe indicator states, not guaranteed profitable trades.
Pine Script v6 reference verified: · TradingView reference
Syntax
ta.supertrend(factor, atrPeriod) → [series float, series float]Arguments
| Parameter | Type | Description |
|---|---|---|
| factor | series int/float | ATR multiplier used in the basic bands hl2 + factor * ATR and hl2 - factor * ATR. It accepts a series value, so it can vary per bar. A larger positive factor widens these basic bands; the final bands also depend on their previous values. |
| atrPeriod | simple int | Lookback period for Average True Range (ATR) calculation. Measures market volatility across the specified number of bars. |
Returns
A two-element tuple with signature [series float, series float]: the active Supertrend price level, then direction. Direction takes values -1 for uptrend and +1 for downtrend. The price line is an indicator value; plotting it does not place a stop order.
Remarks
The final bands carry forward prior band values according to price and band conditions, so Supertrend is not simply hl2 plus or minus ATR on each bar. factor accepts series int/float; atrPeriod requires simple int. On an open realtime bar, high, low, close, ATR and direction can change. A close-confirmed chart-timeframe signal uses barstate.isconfirmed together with the direction transition. This does not confirm higher-timeframe data requested with request.security().
Direction state versus direction change
Evaluate the state when filtering a setup. Evaluate a transition when you want one event at the start of a new direction.
| Condition | Meaning | Typical mistake |
|---|---|---|
| direction < 0 | Current uptrend state (-1) | Assuming negative means bearish |
| direction > 0 | Current downtrend state (+1) | Assuming positive means bullish |
| ta.crossunder(direction, 0) | Switch from +1 to -1: uptrend begins | Using crossover for a bullish direction switch |
| ta.crossover(direction, 0) | Switch from -1 to +1: downtrend begins | Treating it as an uptrend entry |
Check the sign before the order
Read the second tuple value for direction. The first value is a price, so testing whether it is positive does not identify an uptrend. Compare the direction value with the labeled chart bands, then distinguish a persistent state from a one-bar transition. Correct sign logic alone does not establish a profitable strategy.
For more debugging topics, browse the TradingView coding guides. To review a script with AI assistance, use the Pine Script AI Coding Agent and check the generated conditions in Pine Editor.
Official sources
Code Examples
//@version=6
indicator("Supertrend Direction & Band Plotting", overlay = true)
factor = input.float(3.0, "ATR Factor", minval = 0.5, step = 0.5)
atrPeriod = input.int(10, "ATR Period", minval = 1)
[supertrendValue, direction] = ta.supertrend(factor, atrPeriod)
// Direction convention: -1 = Uptrend, 1 = Downtrend
bool isUptrend = direction == -1
bool isDowntrend = direction == 1
// Plot Supertrend bands with line breaks across trend shifts
plot(isUptrend ? supertrendValue : na, "Bullish Support Band", color = color.green, linewidth = 2, style = plot.style_linebr)
plot(isDowntrend ? supertrendValue : na, "Bearish Resistance Band", color = color.red, linewidth = 2, style = plot.style_linebr)
bool turnsUp = ta.crossunder(direction, 0)
bool turnsDown = ta.crossover(direction, 0)
alertcondition(barstate.isconfirmed and turnsUp, "Confirmed uptrend", "Supertrend switched to uptrend at bar close")
alertcondition(barstate.isconfirmed and turnsDown, "Confirmed downtrend", "Supertrend switched to downtrend at bar close")//@version=6
strategy("Supertrend Direction Example", overlay = true)
factor = input.float(3.0, "ATR Factor", minval = 0.1)
atrPeriod = input.int(10, "ATR Period", minval = 1)
[supertrendValue, direction] = ta.supertrend(factor, atrPeriod)
bool turnsUp = ta.crossunder(direction, 0)
bool turnsDown = ta.crossover(direction, 0)
if barstate.isconfirmed and turnsUp
strategy.entry("Long", strategy.long)
if barstate.isconfirmed and turnsDown
strategy.close("Long")
plot(direction < 0 ? supertrendValue : na, "Uptrend", color.green, style = plot.style_linebr)
plot(direction > 0 ? supertrendValue : na, "Downtrend", color.red, style = plot.style_linebr)Trading Applications
Filter an independently defined setup with direction < 0 or direction > 0; a trend state can persist across many bars.
Inspect direction switches alongside the two plotted bands. Compare several closed bars before interpreting a trading rule.
Use the price line as an input to an explicitly coded stop order, if your strategy requires one. The examples here do not place stop orders.
The long-only example closes a long position on a downtrend switch; it does not open a short. With default strategy settings, a market order normally fills on the next available tick, usually the next bar open in a historical backtest.
Examples demonstrate syntax and signal logic, not investment advice or tested performance. Set costs, position size and risk rules before evaluating a strategy.
Related Functions
Frequently Asked Questions
Generate ta.supertrend() Code with AI
Draft Pine Script using ta.supertrend() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.
Related Pine Script Functions
ta.atr() - Average True Range
Learn how to use ta.atr() in Pine Script. Syntax, parameters, code examples for the Average True Range volatility indicator.
ta.sar() - Parabolic SAR
Learn how to use ta.sar() in Pine Script. Syntax, parameters, code examples for the Parabolic Stop and Reverse indicator.
ta.macd() - MACD
Learn how to use ta.macd() in Pine Script. Syntax, parameters, code examples for MACD line, signal line, and histogram.
ta.dmi() - Directional Movement Index
Learn how to use ta.dmi() in Pine Script. Syntax, parameters, code examples for +DI, -DI, and ADX.
ta.ema() - Exponential Moving Average
Learn how to use ta.ema() in Pine Script. Syntax, parameters, code examples, and trading applications for the Exponential Moving Average function.