How to use consolidators in Quantconnect

Quantconnect provides several ways for the user to consolidate data into various timeframes. We have put together a simple explanation of how to use consolidators and the key process to streamline building them into the algo.

What is a data consolidator?

Consolidators are used to combine or “consolidate” data from higher resolutions down into lower resolutions. For example, you might use a consolidator to turn minute data into hourly data or daily data into monthly data.

Why use a data consolidator?

As an example, if you want to use hourly data, you can call the hour data when you need it or call the minute data and consolidate it into the hourly timeframe. The benefit of consolidating the data is that you have greater flexibility in using the data in multiple timeframes in the same model.

Without consolidators, you would have to subscribe to the minute and hourly data for each security which 1) is not efficient and 2) potentially introduces necessary complexity to the code.

The concept of the consolidator is then streamlining the requirements above by subscribing to just minute data; a consolidator combines the minute data information into the hourly data timeframe with the same open, close, high, low parameters etc.

Building a Consolidator Process

There are a variety of ways to implement the ideas, and below are the key ideas in this space.

1. Consolidator Handler: In QuantConnect, consolidators are typically set up with event handlers that are triggered each time the consolidator generates consolidated data. This is achieved using the DataConsolidated event. You assign a function to this event, and that function gets called every time the consolidator has new data.

# Define the consolidator
consolidator = TradeBarConsolidator(timedelta(minutes=60))

# Define the function to be called
def OnDataConsolidated(sender, bar):
    self.Debug(str(bar))

# Add the function to the DataConsolidated event
consolidator.DataConsolidated += OnDataConsolidated

# Add the consolidator for a particular symbol
self.SubscriptionManager.AddConsolidator("SPY", consolidator)

2. Automatic Update: Once you’ve registered a consolidator with a symbol using the SubscriptionManager.AddConsolidator() method, QuantConnect will automatically update the consolidator with data for that symbol. This means you don’t have to pass data into the consolidator manually; it’s all handled automatically by the LEAN engine.

3. Consolidator Helper Method: QuantConnect also provides helper methods to make the creation of consolidators easier. For instance, you can use the self.Consolidate() method to create a consolidator and attach an event handler in a single line. Here is an example:

self.Consolidate("SPY", timedelta(minutes=60), self.OnDataConsolidated)

In this example, self.OnDataConsolidated is a method that takes two arguments (the consolidator that fired the event and the consolidated data) and will be called whenever the consolidator for “SPY” has new data.