I’m learning Pine Script and I want to create a Supertrend indicator from scratch instead of using the built-in function.
Can someone explain how the Supertrend indicator works and provide a simple Pine Script example? It would also be helpful if the code is well commented so it’s easy to understand and customize later.
Yes, you can create a Supertrend indicator in Pine Script very easily using the built-in ta.supertrend() function.
The code below creates a Supertrend with an ATR Multiplier of 3 and an ATR Length of 10. It returns two values:
–supertrend – The Supertrend line.
–direction – The current trend direction.
The plot() functions then display the Supertrend on the chart. When the trend is bullish, the line is plotted in green, and when the trend is bearish, it is plotted in red.
The script also includes a custom function named pine_supertrend(). This function manually calculates the Supertrend using ATR, upper band, lower band, and trend direction. It produces the same result as the built-in ta.supertrend() function and is useful if you want to understand or customize the Supertrend calculation.
You can simply copy the code below into the Pine Script Editor and click Add to Chart to see the Supertrend indicator in action.
Also, if you want the code of tradingview provided for the supertrend then just visit the refrance manual and search for the supertrend in that example you will find the code of the supertrend.
I hope this helps! If you have any questions about the code or want to customize the Supertrend settings, feel free to ask in the comments.
Verified Solution
Done now I can be able to create super trend indicator with example and with your code. Thanks for the help.– Radhesh JoshiJul 28