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
strategy("SuperTrend", overlay=true)
atrPeriod = input.int(10)
factor = input.float(3.0)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
if (direction < 0)
    strategy.entry("Long", strategy.long)
if (direction > 0)
    strategy.close("Long")

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 sizenot set (Pine default)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: holds, mirage 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 Combo
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.