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