Indicator Reference

True Range

Introduction

This indicator computes the True Range (TR). The True Range is the greatest of the following values: value1 = distance from today's high to today's low. value2 = distance from yesterday's close to today's high. value3 = distance from yesterday's close to today's low.

Create Manual Indicators

You can manually create a TrueRange indicator, so it doesn’t automatically update. Manual indicators let you update their values with any data you choose. The following reference table describes the TrueRange constructor.

TrueRange()1/2

            TrueRange QuantConnect.Indicators.TrueRange (
   )
        

Initializes a new instance of the TrueRange class using the specified name.

TrueRange()2/2

            TrueRange QuantConnect.Indicators.TrueRange (
     string  name
   )
        

Initializes a new instance of the TrueRange class using the specified name.

Update Manual Indicators

You can update the indicator automatically or manually.

Automatic Update

To register a manual indicator for automatic updates with the security data, call the RegisterIndicator method.


private TrueRange _tr;

// In Initialize()
_tr = new TrueRange(name);
_tr.Updated += IndicatorUpdateMethod;

RegisterIndicator(symbol, _tr, Resolution.Daily);

// In IndicatorUpdateMethod()
if (_tr.IsReady)
{
    var indicatorValue = _tr.Current.Value;
}
    
# In Initialize()
self.tr = TrueRange(name)
self.tr.Updated += self.IndicatorUpdateMethod

self.RegisterIndicator(symbol, self.tr, Resolution.Daily)

# In IndicatorUpdateMethod()
if self.tr.IsReady:
    indicator_value = self.tr.Current.Value

To customize the data that automatically updates the indicator, see Custom Indicator Periods and Custom Indicator Values.

Manual Update

Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update method with a TradeBar, QuoteBar, or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

    
private TrueRange _tr;
private Symbol symbol;

// In Initialize()
_tr = new TrueRange();
symbol = AddEquity("SPY").Symbol;

// In OnData()
if (data.QuoteBars.ContainsKey(_symbol))
{
    _tr.Update(data.QuoteBars[symbol]);
}
if (_tr.IsReady)
{
    var indicatorValue = _tr.Current.Value;
}
    
# In Initialize()
self.tr = TrueRange()
self.symbol = self.AddEquity("SPY").Symbol

# In OnData()
if data.QuoteBars.ContainsKey(self.symbol):
    self.tr.Update(data.QuoteBars[self.symbol])
if self.tr.IsReady:
    indicator_value = self.tr.Current.Value

Create Automatic Indicators

The TR method creates an TrueRange indicator, sets up a consolidator to update the indicator, and then returns the indicator so you can use it in your algorithm.

The following reference table describes the TR method:

TR()1/1

            TrueRange QuantConnect.Algorithm.QCAlgorithm.TR (
    Symbol                                symbol,
    *Nullable<Resolution>           resolution,
    *Func<IBaseData, IBaseDataBar>  selector
   )
        

Creates a new TrueRange indicator.


If you don't provide a resolution, it defauls to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.

For more information about the selector argument, see Alternative Price Fields.

Get Indicator Values

To get the value of the indicator, use its Current.Value attribute.

private TrueRange _tr;

// In Initialize()
var symbol = AddEquity("SPY").Symbol;
_tr = TR(symbol);

// In OnData()
if (_tr.IsReady)
{
    var current = _tr.Current.Value;
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.tr = self.TR(symbol)

# In OnData()
if self.tr.IsReady:
    current = self.tr.Current.Value

Visualization

To plot indicator values, in the OnData event handler, call the Plot method.

private TrueRange _tr;

// In Initialize()
var symbol = AddEquity("SPY").Symbol;
_tr = TR(symbol);

// In OnData()
if (_tr.IsReady)
{
    Plot("My Indicators", "truerange", _tr.Current);
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.tr = self.TR(symbol)

# In OnData()
if self.tr.IsReady:
    self.Plot("My Indicators", "truerange", self.tr.Current)

For more information about plotting indicators, see Plotting Indicators.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: