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.

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 SMAsma 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 Supported Indicators. In most cases, you should call the helper method in the Initializeinitialize 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.add_equity("SPY").symbol

#  - Create an indicator
self._sma = self.sma(symbol, 20, Resolution.DAILY)

#  - Create a candlestick pattern
patterns = CandlestickPatterns(self)
self.two_crows = patterns.two_crows(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.add_equity("SPY").symbol
self._sma = self.sma(symbol, 20, Resolution.DAILY)
self.set_warm_up(20, Resolution.DAILY)

# In OnData
if self.is_warming_up:
    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.add_equity("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 most indicators to automatically warm them up. This technique doesn't work for all indicators.

private SimpleMovingAverage _sma;

// In Initialize
var symbol = AddEquity("SPY").Symbol;
EnableAutomaticIndicatorWarmUp = true;
_sma = SMA(symbol, 20, Resolution.Daily);
# In Initialize
symbol = self.add_equity("SPY").symbol
self.enable_automatic_indicator_warm_up = 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.warm_up_indicator(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. If the indicator resolution is different from the security resolution, provide the indicator resolution as the argument to properly warm up the indicator.

// 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.warm_up_indicator(self.symbol, self.sma, Resolution.DAILY)

# Warm up with 3-day bars
self.warm_up_indicator(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.warm_up_indicator(self.symbol, self.sma, Resolution.DAILY, Field.HIGH)

Timing Considerations

In some cases, you might create and warm up the indicator during its sampling period. For example, say the security resolution is minute, the indicator resolution is daily, and you create and warm-up the indicator at noon without using the SetWarmUpset_warm_up method. In this example, the history request that gathers data to warm up the indicator won't contain any data from the current day and the consolidator that updates the indicator also won't aggregate any data from before noon. It doesn't cause issues if the indicator only uses the close price to calculate the indicator value (like the simple moving average indicator) because the first consolidated bar that updates the indicator will have the correct close price. However, if the indicator uses more than just the close price to calculate its value (like the True Range indicator), the open, high, and low values of the first consolidated bar may be incorrect, causing the initial indicator values to be incorrect.

Deregister Indicators

To stop the automatic updates of an indicator, pass the indicator to the DeregisterIndicator method.

DeregisterIndicator(_sma);
// Alias:
// UnregisterIndicator(_sma);
self.deregister_indicator(self.sma)
# Alias:
# self.unregister_indicator(self.sma)

Common Mistakes

Avoid the following common mistakes when you use automatic 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.is_ready:
    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 Updateupdate 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 Updateupdate 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.register_indicator(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: