Consolidating Data

Updating Indicators

Introduction

You can use consolidators to automatically update indicators in your algorithms. The consolidators can update your indicators at each time step or with aggregated bars. By default, LEAN updates data point indicators with the close price of the consolidated bars, but you can change it to a custom data field.

Standard Indicator Periods

If your algorithm has a static universe, you can create automatic indicators in just one line of code. When you create an automatic indicator, LEAN creates a consolidator, hooks it up for automatic updates, and then updates the indicator with the consolidated bars.

// Consolidate minute SPY data into 14-bar daily indicators
var ema = EMA("SPY", 14, Resolution.Daily);
var sma = SMA("SPY", 14, Resolution.Daily);
 # Consolidate minute SPY data into 14-bar daily indicators
ema = self.EMA("SPY", 14, Resolution.Daily)
sma = self.SMA("SPY", 14, Resolution.Daily)

If your algorithm has a dynamic universe, create a manual indicator and then create a time period consolidator that updates the indicator at each time step. Keep a reference to the consolidator so you can remove it when your algorithm removes the security from the universe.

_ema = new ExponentialMovingAverage(14);
_consolidator = new TradeBarConsolidator(1);
_consolidator.DataConsolidated += (_, bar) => ema.Update(bar.EndTime, bar.Close);
SubscriptionManager.AddConsolidator(_symbol, _consolidator);
self.ema = ExponentialMovingAverage(14)
self.consolidator = TradeBarConsolidator(1)
self.consolidator.DataConsolidated += lambda _, bar: self.ema.Update(bar.EndTime, bar.Close)
self.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)

Custom Indicator Periods

It's common to update indicators with price data that spans a normal time period like one minute or one day. It's less common to update an indicator with exotic time periods (for example, a 7-minute consolidated bar), so these types of indicators may provide more opportunities for alpha. To update indicators with exotic data, create a manual indicator and then call the RegisterIndicator method. The RegisterIndicator method wires up the indicator for automatic updates at the time interval you provide.

// Calculate the SMA with 10 7-minute bars
var symbol = AddEquity("SPY", Resolution.Minute).Symbol;
var indicator = new SimpleMovingAverage(10);
RegisterIndicator(symbol, sma, TimeSpan.FromMinutes(7));
# Calculate the SMA with 10 7-minute bars
self.symbol = self.AddEquity("SPY", Resolution.Minute).Symbol
self.indicator = SimpleMovingAverage(10)
self.RegisterIndicator(self.symbol, self.sma, timedelta(minutes=7))

The RegisterIndicator method can accept a timedeltaTimeSpan, Resolution, or an unregistered consolidator. If you apply the indicator to a security in a dynamic universe, provide a consolidator so that you can remove it when your algorithm removes the security from the universe.

// TimeSpan
RegisterIndicator(symbol, indicator, TimeSpan.FromMinutes(7));

// Resolution
RegisterIndicator(symbol, indicator, Resolution.Hour);

// Consolidator
_consolidator = new TradeBarConsolidator(35);
RegisterIndicator(symbol, indicator, _consolidator);
# timedelta
self.RegisterIndicator(self.symbol, self.indicator, timedelta(minutes=7))

# Resolution
self.RegisterIndicator(self.symbol, self.indicator, Resolution.Hour)

# Consolidator
self.consolidator = TradeBarConsolidator(35)
self.RegisterIndicator(self.symbol, self.indicator, self.consolidator)

Custom Indicator Values

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 RegisterIndicator method.

// Define a 10-period RSI with indicator the constructor
_rsi = new RelativeStrengthIndex(10, MovingAverageType.Simple);

// Register the daily High price data to automatically update the indicator
RegisterIndicator(symbol, _rsi, Resolution.Daily, Field.High);
# Define a 10-period RSI with indicator constructor
self.rsi = RelativeStrengthIndex(10, MovingAverageType.Simple)

# Register the daily High price data to automatically update the indicator
self.RegisterIndicator(self.symbol, self.rsi, Resolution.Daily, Field.High)

The RegisterIndicator method can accept a timedeltaTimeSpan, Resolution, or an unregistered consolidator. If you apply the indicator to a security in a dynamic universe, provide a consolidator so that you can remove it when your algorithm removes the security from the universe.

The Field class has the following selector properties:

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

RegisterIndicator(_symbol, _rsi,  Resolution.Daily, x =>
{
    var bar = x as IBaseDataBar;
    return (bar.Low + bar.High) / 2;
});

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: