VolatilityPine Script v6Built-in Variable

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

Syntax
// Variable: no parentheses or arguments
ta.tr → series float

// Function: choose how to handle a missing previous close
ta.tr(handle_na) → series float

Arguments

ParameterTypeDescription
handle_nasimple boolFunction 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.

CasePrevious closeHigh / lowH - L|H - previous close||L - previous close|ta.tr / ta.tr(false)ta.tr(true)
First dataset barna150 / 1455nanana5
Previous close inside range148152 / 14754155
Range above previous close151160 / 15559499
Range below previous close158148 / 142610161616
Wide bar spanning prior close144156 / 138181261818
Missing previous close later in datana165 / 1605nanana5
For the upward gap example, max(5, 9, 4) = 9. For the downward gap, max(6, 10, 16) = 16. A gap in opening price can be filled within the bar, so TR greater than high - low is not a complete detector of opening gaps. The missing-data row assumes close[1] is actually na; a session break alone is insufficient.

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

Pine Script v6: compare the variable, function, and formula
//@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)
Pine Script v6: compare ATR with RMA of true range
//@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.

Related Functions

Frequently Asked Questions

The variable ta.tr has no parameters. The function ta.tr(handle_na) accepts one simple bool argument. Use ta.tr(true) for a high - low fallback when close[1] is missing, or ta.tr(false) to preserve na in that case. Neither form takes a length parameter.

The true-range formula uses close[1], and the first dataset bar has no previous bar. The variable and ta.tr(false) therefore return na. With valid high and low, ta.tr(true) returns high - low instead.

No. The history offset [1] refers to the previous available chart bar. An overnight price gap changes the distance between that close and the new high or low. It does not automatically insert a missing previous close.

True range describes one bar. ATR applies Wilder smoothing across true-range observations, and can be expressed as ta.rma(ta.tr(true), length). RMA initializes from length non-na observations. Using ta.tr instead removes the first fallback value and can change initialization and subsequent values.

No. It handles a missing previous close by using high - low. That fallback still needs valid current high and low values, and any downstream indicator can have its own warmup requirements.

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.