TrendPine Script v6

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

Syntax
ta.supertrend(factor, atrPeriod) → [series float, series float]

Arguments

ParameterTypeDescription
factorseries int/floatATR 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.
atrPeriodsimple intLookback 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.

ConditionMeaningTypical mistake
direction < 0Current uptrend state (-1)Assuming negative means bearish
direction > 0Current downtrend state (+1)Assuming positive means bullish
ta.crossunder(direction, 0)Switch from +1 to -1: uptrend beginsUsing crossover for a bullish direction switch
ta.crossover(direction, 0)Switch from -1 to +1: downtrend beginsTreating it as an uptrend entry
A state can remain true for many bars. A transition compares this bar with the preceding bar, and can change before the current bar closes.

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

Supertrend bands with confirmed direction alerts
//@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")
Long-only strategy: enter on uptrend, close on downtrend
//@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.

Frequently Asked Questions

That is the built-in function's sign convention: -1 identifies the uptrend state and +1 identifies the downtrend state. The negative sign does not mean a negative return, a falling price or a short order. TradingView's reference example plots negative direction as the uptrend band. The documentation specifies the convention without giving a design rationale for the sign.

A switch to uptrend goes from +1 to -1, so use ta.crossunder(direction, 0). A switch to downtrend goes from -1 to +1, so use ta.crossover(direction, 0). Checking direction < 0 alone is a state filter and can remain true for many consecutive bars. Evaluate the cross functions on every bar.

ta.supertrend(factor, atrPeriod) returns [series float, series float]. Destructure it as [supertrendValue, direction]. The first result is the price line; the second has direction values -1 and +1. factor accepts series int/float and may vary per bar. atrPeriod requires simple int, such as a fixed integer or input.int(), and cannot be a series length.

Yes. An indicator recalculates as realtime prices update, so a direction switch may appear and disappear before the bar closes. The indicator example gates alert conditions with barstate.isconfirmed. When creating its TradingView alert, choose Once Per Bar Close. This confirms the chart bar; higher-timeframe requests require a separate confirmed-data design.

A single plot connects adjacent values even when the active band switches sides. Plot the uptrend and downtrend segments separately, return na for the inactive segment, and use plot.style_linebr to avoid joining across those gaps.

They are example inputs, not an optimum established here. ATR period changes the volatility calculation and factor scales the basic band distance. Evaluate alternatives with the same symbol, timeframe, costs and risk rules, then check a separate time period. No performance comparison has been run for this page.

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.