Consolidating Options and Equity Data into Five-Minute Bars
Summary
This example demonstrates how an algorithm can convert incoming security data into five-minute bars. It subscribes to SPY options with a strike and expiration filter, then listens for changes to the option chain. When securities are added, it attaches a trade-bar consolidator to equity securities and a quote-bar consolidator to other securities, with callbacks that log each completed bar.
When securities leave the active set, the algorithm removes the matching consolidator and detaches its callback. The example illustrates the lifecycle needed to manage consolidators as an options universe changes, and distinguishes trade data for equities from quote data for option contracts. It is an implementation demonstration rather than a trading strategy: it contains no entry or exit rules, performance evidence, or discussion of execution quality. Its value is in showing bar construction and subscription cleanup in an algorithmic research environment.
Key ideas
- The example aggregates incoming data into five-minute bars.
- It filters the SPY options chain by strike and expiration.
- Equity securities receive trade-bar consolidators, while other securities receive quote-bar consolidators.
- Consolidators are attached when securities enter the universe and removed when they leave.
- The example demonstrates data handling and does not define a trading signal or report strategy results.
Tags
Full text
# BasicTemplateOptionsConsolidationAlgorithm
# BasicTemplateOptionsConsolidationAlgorithm
A demonstration of consolidating options 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 options 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="options" />
class BasicTemplateOptionsConsolidationAlgorithm(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 filter for the options chain
option = self.add_option('SPY')
# set our strike/expiry filter for this option chain
# SetFilter method accepts timedelta objects or integer for days.
# The following statements yield the same filtering criteria
option.set_filter(-2, +2, 0, 180)
# option.set_filter(-2, +2, timedelta(0), timedelta(180))
self.consolidators = dict()
def on_quote_bar_consolidated(self, sender, quote_bar):
self.log("OnQuoteBarConsolidated called on " + str(self.time))
self.log(str(quote_bar))
def on_trade_bar_consolidated(self, sender, trade_bar):
self.log("OnTradeBarConsolidated called on " + str(self.time))
self.log(str(trade_bar))
def on_securities_changed(self, changes):
for security in changes.added_securities:
if security.type == SecurityType.EQUITY:
trade_bar_consolidator = TradeBarConsolidator(timedelta(minutes=5))
trade_bar_consolidator.data_consolidated += self.on_trade_bar_consolidated
self.subscription_manager.add_consolidator(security.symbol, trade_bar_consolidator)
self.consolidators[security.symbol] = trade_bar_consolidator
else:
quote_bar_consolidator = QuoteBarConsolidator(timedelta(minutes=5))
quote_bar_consolidator.data_consolidated += self.on_quote_bar_consolidated
self.subscription_manager.add_consolidator(security.symbol, quote_bar_consolidator)
self.consolidators[security.symbol] = quote_bar_consolidator
for security in changes.removed_securities:
consolidator = self.consolidators.pop(security.symbol)
self.subscription_manager.remove_consolidator(security.symbol, consolidator)
if security.type == SecurityType.EQUITY:
consolidator.data_consolidated -= self.on_trade_bar_consolidated
else:
consolidator.data_consolidated -= self.on_quote_bar_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.