The ta.supertrend function in Pine Script v5 is the cleanest way to plot the SuperTrend indicator on TradingView without recalculating the ATR by hand. The problem is that a lot of people copy code halfway and don’t understand what the function returns — which is why they get the signals wrong. Here you’ll find the correct syntax, what each return value means, and a ready-to-paste indicator with buy and sell signals. Trading is a risky activity; use this as a study tool, not as a promise of profit.
An indicator is only half the journey — trading on emotion blows up your account. See how to turn signals into automated execution and test it in a demo account.
See the Python automation →The function syntax
In Pine Script v5, the function has this signature:
It takes two arguments and returns two values at once (a tuple):
factor — the ATR multiplier (typical value: 3.0). The higher it is, the further the line sits from price and the fewer signals you get.
atrPeriod — the ATR period used in the calculation (typical value: 10).
supertrend — the value of the indicator line (for plotting).
direction — the trend direction. This is the field that generates the signals.
direction is negative (-1) when SuperTrend is below price (an uptrend) and positive (+1) when it’s above price (a downtrend). Many people flip this and get the signals wrong.
Detecting the flip (signals)
The signal comes from the change in direction, not from the value itself. Use the ta.change() function or compare against the previous bar:
Complete indicator (ready to paste)
Paste this code into TradingView’s Pine Editor (v5). It plots the colored line, paints the background, and marks buys/sells:
factor (e.g. 3.5–4.0) to reduce false signals. In slower markets, smaller values react faster — but generate more noise. Always backtest before trusting it.
Common mistakes
Flipping the direction: remember, an uptrend is direction < 0. If your signals look reversed, this is almost always why.
Using the value instead of the flip: a signal is a change in direction, not “price above the line”.
Repainting expectations: SuperTrend is ATR-based and confirms on the bar; don’t expect it to predict exact tops and bottoms.
Trading every signal: SuperTrend on its own generates many signals in a sideways market. Combine it with a trend or volume filter.
FAQ
Does ta.supertrend exist in Pine v4?
The built-in function is a v5 feature. In older versions you had to calculate the ATR and the line manually. Migrate to v5.
What are the best factor and atrPeriod values?
The classic default is 3.0 and 10, but the ideal setting depends on the asset and timeframe. Test and backtest.
Can the signals be automated?
Yes: alertcondition lets you create alerts that trigger webhooks. From there, an external script can execute the order — always tested in demo first.
Does SuperTrend work on any timeframe?
It works, but on very short timeframes the noise increases. Clearer trends tend to show up on higher timeframes.
Disclaimer: trading is a high-risk activity and most retail traders lose money. This content is educational and technical; it does not constitute an investment recommendation, an offer, or financial advice. Indicators do not predict the future and past results do not guarantee future results. Always test in a demo account before risking real capital, and never invest more than you can afford to lose.
