Skip to main content

SuperTrend — Pine Script v5 strategy

TRENDSuperTrendATRPine Script v5

This Pine Script v5 strategy plots the ATR-based SuperTrend line and reads its direction value to trade. It opens a long position when the direction flips up and closes it when the direction flips down.

Full code (free to copy)

//@version=5
// SuperTrend (facteur 3, ATR 10) : long en tendance haussiere, plat sinon.
strategy("SuperTrend", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, process_orders_on_close=true, margin_long=0)
// direction vaut -1 en hausse, +1 en baisse.
[supertrend, direction] = ta.supertrend(3.0, 10)
longSignal = direction < 0
exitSignal = direction > 0
if longSignal
    strategy.entry("Long", strategy.long)
if exitSignal
    strategy.close("Long")

Judge this code on BTCUSDT 1D →Free, in your browser, on the same candles as the table below.

Online judge verdict on BTCUSDT (measured 29 November 2026)

TimeframeSealed periodTradesReturnBuy & holdVerdict
1d2024-12-17 → 2026-10-169-29.98 %-23.25 %Not enough sealed trades
4h2026-03-12 → 2026-10-17172.74 %15.83 %Inconclusive
1h2026-08-05 → 2026-10-172411.98 %27.14 %No evidence of an edge

Past data, 10 bps per fill, last 20% of the bars sealed. Windows: full history on 1d, last 3 years on 4h, last year on 1h. Research, not advice: no result is promised.

Compared with a real TradingView run on 31 October 2026 (BINANCE:BTCUSDT, this code): identical trades 39/39 on 1D, 72/72 on 4h, same prices on all matching trades.

Measured on 29 November 2026 with the online judge’s Pine v5 engine (sa-pine-vm 0.2.1), on Binance BTCUSDT candles up to 16/10/2026 on 1D and up to 17/10/2026 on 4h and 1h. Differences with TradingView’s Strategy Tester remain possible. Binance data, CC BY-NC-SA 4.0.

Open this code in the free judge (1 click) →

Replay each verdict yourself, in your browser: 1d · 4h · 1h. The script is filled in; nothing runs until you click “Judge”.

How the script works

The script declares a strategy on the chart with overlay set to true, so the SuperTrend line is drawn over the candles. Two inputs feed it: atrPeriod defaults to 10 and factor defaults to 3.0. The call ta.supertrend(factor, atrPeriod) returns two series, the SuperTrend line and the direction value, which the code stores in the variables supertrend and direction.

Entry logic is a single condition: if direction is below zero, the code calls strategy.entry with the id Long and strategy.long as the direction. In Pine Script v5, ta.supertrend returns a negative direction value while price sits above the line, so this condition fires when the trend flips to the upside. The order is filled on the next bar, not on the bar that produced the signal.

Exit logic is the mirror condition: if direction is greater than zero, strategy.close is called for the Long position. The script holds no short side and sets no stop loss or take profit, so the position stays open until the direction value turns positive again and that opposite flip closes it.

Parameters in the code

ParameterValueRole
atr period10Default of input.int(10), passed as the second argument of ta.supertrend(factor, atrPeriod).
factor3.0Default of input.float(3.0), the ATR multiplier passed as the first argument of ta.supertrend.
overlaytrueSet in the strategy() declaration so the SuperTrend line is drawn on the price chart.
strategy titleSuperTrendFirst argument of strategy(), the name the platform displays for this script.
entry conditiondirection < 0The only condition that triggers strategy.entry in the script.
exit conditiondirection > 0The only condition that triggers strategy.close in the script.
entry identifierLongString given to strategy.entry and reused by strategy.close.
entry directionstrategy.longSecond argument of strategy.entry, opens the position on the long side.
exit callstrategy.close("Long")Closes the whole Long position when direction becomes positive.
order size100% of equity (default_qty_value=100)strategy.entry receives no qty argument, so the default order size of the platform is used.

Known limits of this rule

Before you trust it: test it on data it never saw

A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: survives, no evidence of an edge or inconclusive.

Get the free Lab →

FAQ

Does it work on crypto?

The script has no market filter, so the same rule can be applied to any symbol your data feed provides. You can replay it on a period never seen in Strategy Arena Lab, which is free.

How do I add a stop loss?

The code only closes on a direction flip. You would add a strategy.exit call with a stop or loss parameter next to the existing entry.

Other free Pine Script v5 strategies

RSI Mean ReversionEMA Crossover 9/21EMA Ribbon 8/21/55Bollinger Band BounceMACD MomentumStochastic RSIATR BreakoutVWAP BounceRSI + EMA ComboFaber 10-Month TimingGolden Cross SMA 50/200Bollinger Squeeze BreakoutKeltner Channel Breakout 20/1.5Donchian Turtle 20/10ADX 25 & DMI 14 TrendParabolic SAR 0.02/0.2Hull Moving Average 55ALMA 20 Gaussian TrendCCI 20 (+100) BreakoutMoney Flow Index MFI 14 (20/80)ATR 14 Chandelier StopIchimoku Tenkan 9 / Kijun 26Linear Regression 20ROC 14 + EMA 200 FilterWilliams %R 14 PullbackNo-Leak Weekly EMA FilterPivot High/Low BreakoutAI-style ATR Breakout + EMA 200
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.