This tutorial aims to demystify ta.tr()
, exploring its syntax, functionality, and application in trading scripts.
Syntax and Arguments
ta.tr(handle_na) → series float
handle_na
(simple bool): This parameter dictates how NaN (Not a Number) values are managed. When set totrue
, if the previous day’s close is NaN, then the true range (tr
) is calculated as the current day’s high minus the low. Conversely, iffalse
,tr
would return NaN in such scenarios. It’s noteworthy thatta.atr()
, which calculates the Average True Range, utilizesta.tr(true)
.
Example
Let’s illustrate the use of ta.tr()
with a practical example. To ensure clarity, we’ll modify variable names slightly from the standard syntax.
//@version=5 indicator("My True Range Demo", overlay=true) // Calculating True Range with NaN handling myTrueRange = ta.tr(true) plot(myTrueRange, title="True Range", color=color.blue)
In this script:
- We declare an indicator named “My True Range Demo” that overlays on the price chart.
myTrueRange
calculates the true range of each bar/candle, withhandle_na
set totrue
to manage NaN values effectively.- The true range is then plotted on the chart in blue.
Detailed Walkthrough
- Indicator Declaration:
indicator("My True Range Demo", overlay=true)
specifies that this script is an indicator and will be drawn over the price chart. - True Range Calculation:
ta.tr(true)
calculates the true range. By passingtrue
forhandle_na
, the script ensures continuity in the true range calculation, even when the previous day’s close is missing. - Plotting:
plot(myTrueRange, title="True Range", color=color.blue)
visualizes the true range on the chart, providing a graphical representation of volatility.
Key Features and Takeaways
- Function Useability:
ta.tr()
is versatile, allowing users to handle NaN values efficiently, which is crucial for accurate volatility measurement. - Syntax: The function requires a boolean argument (
handle_na
) to specify NaN handling, enhancing flexibility in financial data analysis. - Application: Primarily used to assess market volatility, the true range is a foundational component in constructing more complex indicators like the Average True Range (ATR).
By understanding and utilizing ta.tr()
in Pine Script, traders and analysts can significantly enhance their technical analysis toolkit, especially in volatility assessment and strategy development.