Home » Technical Analysis Functions » Understanding ta.tr() Function in Pine Script

Understanding ta.tr() Function in Pine Script

Photo of author
Published on

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 to true, 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, if false, tr would return NaN in such scenarios. It’s noteworthy that ta.atr(), which calculates the Average True Range, utilizes ta.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)
Example

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, with handle_na set to true to manage NaN values effectively.
  • The true range is then plotted on the chart in blue.

Detailed Walkthrough

  1. Indicator Declaration: indicator("My True Range Demo", overlay=true) specifies that this script is an indicator and will be drawn over the price chart.
  2. True Range Calculation: ta.tr(true) calculates the true range. By passing true for handle_na, the script ensures continuity in the true range calculation, even when the previous day’s close is missing.
  3. 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.

Leave a Comment