Skip to content

True Range (ta.true_range)

View Markdown

Take the maximum of the high-low range and gap ranges relative to the previous close. With handle_na=True, a missing previous close falls back to H-L and the current Close may be missing; non-finite or inverted H/L is invalid. The default retains the previous output.

Indicator catalog · How to use indicators

Run this example

This complete script uses bundled data. It observes the selected output without placing trades.

import numpy as np
from openbacktesting import Backtest, Strategy
from openbacktesting.test import GOOG

data = GOOG.copy()

class Example(Strategy):
    def init(self):
        self.value = self.ta.true_range()
        self.latest_value = None

    def next(self):
        channel = self.value
        if channel.valid[-1]:
            self.latest_value = float(channel[-1])

result = Backtest(data, Example).run()
print(result["_strategy"].latest_value)

Call and parameters

self.ta.true_range(*, drift=1, handle_na=False, inputs=None, backend=None, required_outputs=None)

Register in init(). Read the returned channel in next() after checking valid[-1].

Parameter Meaning Type Default Allowed values
drift Bar lag for differences. int 1 Minimum: 1
handle_na Use High - Low when previous Close is missing; otherwise mark that observation invalid. bool False No additional constraints

source/named source arguments accept a column name or same-run single-channel handle. Alternatively, inputs maps each input below to a column or handle. backend selects a supported implementation; required_outputs selects channels for readiness diagnostics. See shared arguments and handles.

Inputs and outputs

Required inputs: high, low, close.

Channel Meaning
true_range True Range value

Reading values safely

Insufficient history or missing inputs can make a channel invalid. Check that channel's valid mask; ready is diagnostic and never suppresses next(). Multi-output results may have different valid channels on the same bar.

Indicator catalog · Custom calculations