Indicators

Automatic Indicators

Introduction

Automatic indicators are indicators that automatically update from the underlying security data. Automatic indicators do everything that manual indicators do, but with automatic updates registered for you. If you have a dynamic universe, you shouldn't use automatic indicators because you can't remove the consolidator that updates the indicator. Since you can't remove the consolidator, the consolidators build up over time and slow down your algorithm. If your algorithm has a dynamic universe, use manual indicators instead.

Naming Convention

The method to create an automatic indicator is usually named after the acronym of the indicator name. For example, to create an automatic simple moving average indicator, use the SMA method.

Create Automatic Indicators

To create automatic indicators, call the indicator helper method from the QCAlgorithm class. The indicator helper methods create an indicator object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. To view the helper method for each indicator, see the Indicator Reference. In most cases, you should call the helper method in the Initialize method.

The indicator resolution must be greater than or equal to the resolution of the security subscription. For instance, if your security subscription is for minute resolution data, the indicator resolution should be minute, hour, or daily resolution.

private SimpleMovingAverage _sma;
private TwoCrows _twoCrows;

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

//  - Create an indicator
_sma = SMA(symbol, 20, Resolution.Daily);

// - Create a candlestick pattern
var patterns = new CandlestickPatterns(this);
_twoCrows = patterns.TwoCrows(_symbol);
# In Initialize()
symbol = self.AddEquity("SPY").Symbol

#  - Create an indicator
self.sma = self.SMA(symbol, 20, Resolution.Daily)

#  - Create a candlestick pattern
patterns = CandlestickPatterns(self)
self.two_crows = patterns.TwoCrows(self.symbol)

When you create an indicator with a helper method, the indicator is given a default name. You can track indicators by their name. The default name for the SimpleMovingAverage indicator is "SMA(period,ticker_resolution)". For example, in the preceding code snippet, the SimpleMovingAverage indicator is named "SMA(20,SPY_day)". To get the name of an indicator, inspect its Name property.

Log(_sma.Name);
Log(_twoCrows.Name);
self.Log(self.sma.Name)
self.Log(self.two_crows.Name)

Alternative Price Fields

Data point indicators use only a single price data in their calculations. By default, those indicators use the closing price. For assets with TradeBar data, that price is the TradeBar close price. For assets with QuoteBar data, that price is the mid-price of the bid closing price and the ask closing price. To create an indicator with the other fields like the Open, High, Low, or Close, provide a selector argument to the indicator helper method.

# Define a 10-period daily RSI indicator with shortcut helper method
# Select the Open price to update the indicator
self.rsi = self.RSI("SPY", 10,  MovingAverageType.Simple, Resolution.Daily, Field.Open)
// Define a 10-period daily RSI indicator with shortcut helper method
// Select the Open price to update the indicator
_rsi = RSI("SPY", 10,  MovingAverageType.Simple, Resolution.Daily, Field.Open);

The Field class has the following selector properties:

To create a custom selector, define a function that calculates the value.

_rsi = RSI("SPY", 10,  MovingAverageType.Simple, Resolution.Daily, x =>
{
    var bar = x as IBaseDataBar;
    return (bar.Low + bar.High) / 2;
});

Warm Up Indicators

Indicators compute their value based on historical data. Before you start trading with an indicator, warm it up. There are several ways to warm-up automatic indicators.

Algorithm Warm-up

You can set an algorithm warm-up period to warm up the indicators. When you set an algorithm warm-up period, the engine pumps data in and automatically updates all the indicators from before the start date. To ensure that all the indicators are ready after the algorithm warm-up period, choose a look-back period that contains sufficient data.

private SimpleMovingAverage _sma;

// In Initialize
var symbol = AddEquity("SPY").Symbol;
_sma = SMA(symbol, 20, Resolution.Daily);
SetWarmUp(20, Resolution.Daily);

// In OnData
if (IsWarmingUp)
{ return; }
# In Initialize
symbol = self.AddEquity("SPY").Symbol
self.sma = self.SMA(symbol, 20, Resolution.Daily)
self.SetWarmUp(20, Resolution.Daily)

# In OnData
if self.IsWarmingUp:
    return

Manual Indicator Warm-up

You can manually warm up indicators with a history request.

private SimpleMovingAverage _sma;

// In Initialize
var symbol = AddEquity("SPY").Symbol;
_sma = SMA(symbol, 20, Resolution.Daily);

var history = History(symbol, 20, Resolution.Daily);
foreach (var bar in history)
{
    sma.Update(bar.Time, bar.Close);
}
# In Initialize
symbol = self.AddEquity("SPY").Symbol
self.sma = self.SMA(symbol, 20, Resolution.Daily)

history = self.History(symbol, 20, Resolution.Daily)
if not history.empty:
    for time, row in history.loc[symbol].iterrows():
        self.sma.Update(time, row.close)

Automatic Indicator Warm-up

You can set the EnableAutomaticIndicatorWarmUp property to true before you create indicators to automatically warm them up.

private SimpleMovingAverage _sma;

// In Initialize
var symbol = AddEquity("SPY").Symbol;
EnableAutomaticIndicatorWarmUp = true;
_sma = SMA(symbol, 20, Resolution.Daily);
# In Initialize
symbol = self.AddEquity("SPY").Symbol
self.EnableAutomaticIndicatorWarmUp = True
self.sma = self.SMA(symbol, 20, Resolution.Daily)

Warm-up Helper Method

If an indicator inherits the IIndicatorWarmUpPeriodProvider class, you can warm it up with the WarmUpIndicator method.

_sma = SMA(_symbol, 20);
WarmUpIndicator(_symbol, _sma);
self.sma = self.SMA(self.symbol, 20)
self.WarmUpIndicator(self.symbol, self.sma)

To warm up the indicator with a resolution that's different from the security resolution, pass a resolution or TimeSpantimedelta argument to the WarmUpIndicator method. The resolution you provide should be greater than or equal to the security resolution. For example, if the security has minute resolution data, you should warm up the indicator with data that spans at least one minute.

// Warm up with daily bars
WarmUpIndicator(_symbol, _sma, Resolution.Daily);

// Warm up with 3-day bars
WarmUpIndicator(_symbol, _sma, TimeSpan.FromDays(3));
# Warm up with daily bars
self.WarmUpIndicator(self.symbol, self.sma, Resolution.Daily)

# Warm up with 3-day bars
self.WarmUpIndicator(self.symbol, self.sma, timedelta(days=3))

The WarmUpIndicator method uses the default Value of the historical data to warm up the indicator. In most cases, this is the closing price. To warm up the indicator with a different data field, pass a Field argument to the method.

WarmUpIndicator(_symbol, _sma, Resolution.Daily, Field.High);
self.WarmUpIndicator(self.symbol, self.sma, Resolution.Daily, Field.High)

Common Mistakes

Avoid the following common mistakes when you use automatic indicators.

Creating Automatic Indicators in a Dynamic Universe

You can't currently remove the consolidator that LEAN creates to update automatic indicators. If you add consolidators to a dynamic universe, the consolidators build up over time and slow down your algorithm. To avoid issues, if you algorithm has a dynamic universe, use manual indicators.

Using Indicator Values Before the Indicator Is Warmed Up

Indicators can provide inaccurate output values before they are warmed up. To avoid issues, always check the IsReady flag before you use indicator output values.

if (_indicator.IsReady)
{
    var value = _indicator.Current.Value;
}
if self.indicator.IsReady:
    value = self.indicator.Current.Value

Manually Registering an Automatic Indicator for Updates

If you create an automatic indicator and then register it for automatics updates or call the Update method, the indicator receives multiple input values during each update cycle. To avoid issues, create automatic indicators but don't register them for automatic updates or call the Update method.

// Create automatic indicators
_indicator = SMA(_symbol, 5);

// Don't do this:
RegisterIndicator(_symbol, _indicator, Resolution.Daily)
# Create automatic indicators
self.indicator = self.SMA(self.symbol, 5)

# Don't do this:
self.RegisterIndicator(self.symbol, self.indicator, Resolution.Daily)

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: