ta.trTrue Range: Variable and Function
ta.tr is a series variable with no parameters. The function ta.tr(handle_na) takes a boolean: true uses high - low when close[1] is missing; false returns na in that case, like the variable. With a valid previous close, both forms return the same true range.
Pine Script v6 reference verified: · TradingView reference
Syntax
// Variable: no parentheses or arguments
ta.tr → series float
// Function: choose how to handle a missing previous close
ta.tr(handle_na) → series floatArguments
| Parameter | Type | Description |
|---|---|---|
| handle_na | simple bool | Function form only. Pass true to use high - low if close[1] is na, or false to return na when close[1] is na. This option does not choose a lookback length or replace missing high and low values. |
Returns
A series float in price units. With valid high, low, and previous close, TR is max(high - low, abs(high - close[1]), abs(low - close[1])). A missing previous close produces na unless handle_na is true.
Remarks
ta.tr is equivalent to ta.tr(false). On the first bar of the dataset, close[1] is unavailable. A normal overnight or weekend break does not itself make close[1] missing: it still refers to the previous available chart bar. ATR smooths true range with Wilder smoothing (RMA): ta.rma(ta.tr(true), length). It needs length valid true-range observations to initialize; changing the first value to high - low does not make a 14-period ATR available on bar 0. RMA ignores na source observations, so a missing first value does not permanently poison the built-in smoother.
True range: worked price examples
Hypothetical, independent bars. Compute the three candidates and take their maximum. The current close is not an input to this calculation.
| Case | Previous close | High / low | H - L | |H - previous close| | |L - previous close| | ta.tr / ta.tr(false) | ta.tr(true) |
|---|---|---|---|---|---|---|---|
| First dataset bar | na | 150 / 145 | 5 | na | na | na | 5 |
| Previous close inside range | 148 | 152 / 147 | 5 | 4 | 1 | 5 | 5 |
| Range above previous close | 151 | 160 / 155 | 5 | 9 | 4 | 9 | 9 |
| Range below previous close | 158 | 148 / 142 | 6 | 10 | 16 | 16 | 16 |
| Wide bar spanning prior close | 144 | 156 / 138 | 18 | 12 | 6 | 18 | 18 |
| Missing previous close later in data | na | 165 / 160 | 5 | na | na | na | 5 |
Check the first bar before debugging ATR
The first example plots all three forms and exposes the manual formula difference in the Data Window. The second compares ATR with two RMA inputs. On consecutive valid OHLC bars, a length of 14 gives the first ATR at bar index 13 using ta.tr(true); RMA of the variable needs one more bar because its first input is missing. These examples are diagnostic scripts, not backtested strategies.
Review TradingView's variable and function distinction, ta.tr function reference, and true-range formula and ATR smoothing. The history-referencing operator explains why [1] means the previous bar in the series.
For help revising a larger volatility calculation, include the Pine version, length, and first mismatching bar when using the Pine Script AI Coding Agent. Review the resulting code and compare its values on your chart. Browse more TradingView coding guides.
Code Examples
//@version=6
indicator("True range forms", overlay=false)
float trVariable = ta.tr
float trFalse = ta.tr(false)
float trTrue = ta.tr(true)
float trFormula = na(close[1]) ? high - low : math.max(high - low, math.abs(high - close[1]), math.abs(low - close[1]))
plot(trVariable, "ta.tr", color=color.blue, linewidth=2)
plot(trFalse, "ta.tr(false)", color=color.purple, style=plot.style_circles)
plot(trTrue, "ta.tr(true)", color=color.orange, style=plot.style_cross)
plot(trFormula - trTrue, "Formula minus ta.tr(true)", display=display.data_window)
// Marks a missing previous close; it does not mark every session opening.
bgcolor(na(close[1]) ? color.new(color.orange, 85) : na)//@version=6
indicator("ATR and RMA of true range", overlay=false)
int length = input.int(14, "ATR length", minval=1)
float atrBuiltin = ta.atr(length)
float atrFromTr = ta.rma(ta.tr(true), length)
// This version has no valid TR on the first dataset bar.
float atrFromVariable = ta.rma(ta.tr, length)
plot(atrBuiltin, "Built-in ATR", color=color.blue, linewidth=2)
plot(atrFromTr, "RMA of ta.tr(true)", color=color.orange, style=plot.style_circles)
plot(atrFromVariable, "RMA of ta.tr", color=color.purple)
plot(atrBuiltin - atrFromTr, "ATR difference", display=display.data_window)Trading Applications
Inspect a single bar's range including its distance from the previous close.
Compare a custom volatility calculation with ta.atr using the same data and length.
Choose whether missing previous closes should remain missing or use the current bar range.
Frequently Asked Questions
Generate ta.tr Code with AI
Draft Pine Script using ta.tr with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.
Related Pine Script Functions
ta.tr() - True Range Function
Learn how to use ta.tr() function in Pine Script. Syntax, parameters including handle_na, code examples for True Range.
ta.atr() - Average True Range
Learn how to use ta.atr() in Pine Script. Syntax, parameters, code examples for the Average True Range volatility indicator.
ta.rma() - Relative Moving Average
Learn how to use ta.rma() in Pine Script. Syntax, parameters, code examples for Wilder's smoothing method used in RSI calculation.
ta.bb() - Bollinger Bands
Learn how to use ta.bb() in Pine Script. Syntax, parameters, code examples for Bollinger Bands with upper, middle, and lower bands.
ta.kc() - Keltner Channels
Learn how to use ta.kc() in Pine Script. Syntax, parameters, code examples for Keltner Channel volatility bands.