Skip to content
All library documents

Consolidating Futures Quote Data into Five-Minute Bars

Article Strategy library · Author: QuantConnect

Summary

This example demonstrates how to combine incoming futures quote data into larger time-based bars for use in an algorithm. It subscribes to an S&P 500 E-mini futures chain, filters contracts by expiry, and creates a five-minute quote-bar consolidator for each security as it is added. A callback logs each completed consolidated bar. When a security leaves the active set, the example removes its consolidator and disconnects the callback.

The document is an implementation example rather than a trading strategy: it does not define entry or exit rules, place orders, or report backtest results. Its useful lesson is the lifecycle of consolidators for dynamically changing futures contracts, including adding them when securities appear and cleaning them up when they are removed. The sample uses a short historical date range and is explicitly presented as a demonstration of data handling.

Key ideas

  • The example aggregates futures quote data into five-minute quote bars.
  • It applies an expiry filter to the S&P 500 E-mini futures chain.
  • A consolidator and event callback are registered for each newly added security.
  • Consolidators and callbacks are removed when their associated securities leave the active set.
  • The example demonstrates data handling and reports no trading logic or performance results.

Tags

Full text
# BasicTemplateFuturesConsolidationAlgorithm


# BasicTemplateFuturesConsolidationAlgorithm









A demonstration of consolidating futures data into larger bars for your algorithm.

## Source (Apache-2.0)

```python
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *

### <summary>
### A demonstration of consolidating futures data into larger bars for your algorithm.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="benchmarks" />
### <meta name="tag" content="consolidating data" />
### <meta name="tag" content="futures" />
class BasicTemplateFuturesConsolidationAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2013, 10, 7)
        self.set_end_date(2013, 10, 11)
        self.set_cash(1000000)

        # Subscribe and set our expiry filter for the futures chain
        futureSP500 = self.add_future(Futures.Indices.SP_500_E_MINI)
        # set our expiry filter for this future chain
        # SetFilter method accepts timedelta objects or integer for days.
        # The following statements yield the same filtering criteria
        futureSP500.set_filter(0, 182)
        # future.set_filter(timedelta(0), timedelta(182))

        self.consolidators = dict()

    def on_data(self,slice):
        pass

    def on_data_consolidated(self, sender, quote_bar):
        self.log("OnDataConsolidated called on " + str(self.time))
        self.log(str(quote_bar))
        
    def on_securities_changed(self, changes):
        for security in changes.added_securities:
            consolidator = QuoteBarConsolidator(timedelta(minutes=5))
            consolidator.data_consolidated += self.on_data_consolidated
            self.subscription_manager.add_consolidator(security.symbol, consolidator)
            self.consolidators[security.symbol] = consolidator
            
        for security in changes.removed_securities:
            consolidator = self.consolidators.pop(security.symbol)
            self.subscription_manager.remove_consolidator(security.symbol, consolidator)
            consolidator.data_consolidated -= self.on_data_consolidated

```

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.