SuperTrend — Pine Script v5 strategy
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)
| Timeframe | Sealed period | Trades | Return | Buy & hold | Verdict |
|---|---|---|---|---|---|
| 1d | 2024-12-17 → 2026-10-16 | 9 | -29.98 % | -23.25 % | Not enough sealed trades |
| 4h | 2026-03-12 → 2026-10-17 | 17 | 2.74 % | 15.83 % | Inconclusive |
| 1h | 2026-08-05 → 2026-10-17 | 24 | 11.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
| Parameter | Value | Role |
|---|---|---|
atr period | 10 | Default of input.int(10), passed as the second argument of ta.supertrend(factor, atrPeriod). |
factor | 3.0 | Default of input.float(3.0), the ATR multiplier passed as the first argument of ta.supertrend. |
overlay | true | Set in the strategy() declaration so the SuperTrend line is drawn on the price chart. |
strategy title | SuperTrend | First argument of strategy(), the name the platform displays for this script. |
entry condition | direction < 0 | The only condition that triggers strategy.entry in the script. |
exit condition | direction > 0 | The only condition that triggers strategy.close in the script. |
entry identifier | Long | String given to strategy.entry and reused by strategy.close. |
entry direction | strategy.long | Second argument of strategy.entry, opens the position on the long side. |
exit call | strategy.close("Long") | Closes the whole Long position when direction becomes positive. |
order size | 100% 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
- SuperTrend is a trend-following rule: in a sideways range the direction value flips repeatedly and each flip opens or closes the position, which produces whipsaw trades.
- The direction value is computed from past bars and the fill happens on the next bar, so entries and exits arrive after the move has already started, and a late flip can reverse a position near the end of a move.
- The script sets no commission, no slippage and no stop loss, so the chart result ignores trading costs and carries each trade without a defined risk limit.
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.
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
More TradingView paths
Educational code on past data. No investment advice; no result is promised.