Trend FollowingBeginnerSupertrendATR

Supertrend Strategy

Supertrend is a trend-following indicator built on ATR that flips direction when price crosses its value. It produces clean long/short signals with only two parameters (ATR period and factor), making it ideal for systematic optimization. This implementation uses the built-in ta.supertrend() function and includes ATR-based trailing stops.

Best for: Trending assets — crypto, commodities, indicesTimeframes: 4H, Daily, Weekly

Pine Script v6 — Complete Strategy Code

//@version=6
supertrend-pine-script.pine
//@version=6
strategy("Supertrend Strategy", overlay=true,
     default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// ── Inputs ──
int   atrPeriod = input.int(defval=10,  title="ATR Period", minval=1, group="Supertrend")
float factor    = input.float(defval=3.0, title="Factor",  minval=0.1, step=0.1, group="Supertrend")

// ── Calculations ──
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

bool longEntry  = direction == -1 and direction[1] == 1   // flipped bullish
bool shortEntry = direction == 1  and direction[1] == -1  // flipped bearish

// ── Visuals ──
float stPlot = direction == -1 ? supertrend : na
float sbPlot = direction == 1  ? supertrend : na
plot(stPlot, "Supertrend Up",   color.new(color.green, 0), linewidth=2)
plot(sbPlot, "Supertrend Down", color.new(color.red,   0), linewidth=2)
plotshape(longEntry,  style=shape.triangleup,   location=location.belowbar, color=color.new(color.green, 0), size=size.small)
plotshape(shortEntry, style=shape.triangledown, location=location.abovebar, color=color.new(color.red,   0), size=size.small)

// ── Orders ──
if longEntry
    strategy.close("Short")
    strategy.entry("Long", strategy.long)
if shortEntry
    strategy.close("Long")
    strategy.entry("Short", strategy.short)

// ── Alerts ──
alertcondition(longEntry,  title="Supertrend Long",  message="Supertrend bullish flip — {{ticker}}")
alertcondition(shortEntry, title="Supertrend Short", message="Supertrend bearish flip — {{ticker}}")

Copy and paste into TradingView Pine Editor, or generate a custom variation with AI.

How it works

Strategy logic, step by step

  1. 1

    Supertrend is calculated using ta.supertrend(factor, atrPeriod).

  2. 2

    Long entry: Supertrend flips from bearish to bullish (direction changes from 1 to -1).

  3. 3

    Short entry: Supertrend flips from bullish to bearish (direction changes from -1 to 1).

  4. 4

    Stop loss: previous Supertrend level (dynamic, moves with the trend).

  5. 5

    The strategy is always in the market — each new signal closes the previous position and opens the new one.

Parameters

Configurable inputs

ParameterDefault
ATR Period10
Factor3.0

Ask the TradePilot copilot to backtest this and sweep the inputs to find the best combination for your asset and timeframe.

FAQ

Supertrend Strategy questions

What is the best Supertrend setting?

There is no universal best setting — it depends on the asset and timeframe. ATR 10, Factor 3 is the most common starting point. Use TradePilot's Strategy Optimizer to find the best Factor (1.5–5.0) and ATR Period (7–21) for your specific market.

Does Supertrend repaint?

The built-in ta.supertrend() does not repaint on confirmed bars. Signals on the current (unconfirmed) bar may change until the bar closes, which is normal for all Pine Script indicators.

Can I add a take profit to Supertrend?

Yes — tell TradePilot "add a 3% take profit to the Supertrend strategy" and the inline editor will add strategy.exit() with a limit price.

More strategies

Other Pine Script strategies

Want a custom version of this strategy?

Describe your modifications in plain English — different indicators, different risk parameters, long-only, multi-timeframe filter — and TradePilot generates the full Pine Script v6 code in seconds.