The ta.supertrend
function in Pine Script is a handy tool for traders, helping them track market trends. This blog post will offer an in-depth tutorial on how to use the function, its syntax, arguments, and an exclusive example that distinguishes it from its manual Pine Script counterpart. So, let’s dive straight in.
Syntax & Arguments
The primary purpose of the Supertrend indicator, as its name suggests, is to follow market trends. To achieve this, it uses the Average True Range (ATR) along with a user-defined factor.
Syntax
ta.supertrend(factor, atrPeriod) → [series float, series float]
Arguments
- factor (series int/float): This is the multiplier by which the ATR is multiplied. It determines the distance of the Supertrend line from the price.
- atrPeriod (simple int): This represents the length of the ATR. It’s the period over which the average true range is calculated.
Understanding The Example
Let’s dissect the given example to understand how the ta.supertrend
function works in Pine Script.
Using the ta.supertrend
Function
//@version=5 indicator("Pine Script™ Supertrend") [supertrend, direction] = ta.supertrend(3, 10) plot(direction < 0 ? supertrend : na, "Up direction", color = color.green, style=plot.style_linebr) plot(direction > 0 ? supertrend : na, "Down direction", color = color.red, style=plot.style_linebr)
indicator("Pine Script™ Supertrend")
: This initiates a new indicator on the TradingView chart with the specified name.- The
ta.supertrend(3, 10)
function is then called with a factor of 3 and an ATR period of 10. It returns two series – the supertrend line values and the direction of the trend. - The two subsequent
plot
functions visualize the up and down directions of the supertrend, represented in green and red, respectively.
Manually Replicating with Pine Script
pine_supertrend(factor, atrPeriod) => ... ... [pineSupertrend, pineDirection] = pine_supertrend(3, 10) plot(pineDirection < 0 ? pineSupertrend : na, "Up direction", color = color.green, style=plot.style_linebr) plot(pineDirection > 0 ? pineSupertrend : na, "Down direction", color = color.red, style=plot.style_linebr)
This segment manually creates the Supertrend function in Pine Script. The process involves calculating the upper and lower bands based on the ATR and factor, and then determining the trend direction based on these bands and the close price.
Unique Use Case Example
For our unique use-case, let’s say you want to generate alerts for potential trade entries. An alert can be triggered when the trend direction changes. We can achieve this using the following script:
//@version=5 indicator("Pine Script™ Supertrend Alerts") [supertrend, direction] = ta.supertrend(3, 10) alertcondition(direction[1] != direction, title="Trend Change", message="The supertrend direction has changed!") plot(direction < 0 ? supertrend : na, "Up direction", color = color.green, style=plot.style_linebr) plot(direction > 0 ? supertrend : na, "Down direction", color = color.red, style=plot.style_linebr)
Here, the alertcondition
function checks if the current trend direction differs from the previous one. If it does, an alert condition is set, allowing traders to create alerts in TradingView based on this condition.
Key Takeaway
The ta.supertrend
function in Pine Script is a powerful tool that provides traders with a clear visual representation of the market trend. It streamlines the process, eliminating the need for manual calculations. By understanding its workings and potential use cases, traders can incorporate it into their strategies for better decision-making.
Conclusion
Whether you use the built-in ta.supertrend
function or manually recreate it, Pine Script offers a flexible environment to cater to your trading needs. The key is to understand the underlying concepts, experiment with different settings, and always backtest any strategy before live trading. Happy coding!