# 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 datetime import date, timedelta
### <summary>
### Basic template futures framework algorithm uses framework components
### to define an algorithm that trades futures.
### </summary>
class BasicTemplateFuturesFrameworkAlgorithm(QCAlgorithm):
def Initialize(self):
self.UniverseSettings.Resolution = Resolution.Minute
self.SetStartDate(2020, 7, 20)
#self.SetEndDate(2020, 7, 21)
self.SetCash(100000)
ES = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
ES.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen())
self.consolidators = dict()
def OnDataConsolidated(self, sender, quoteBar):
# This should be called every 90 minutes, after consolidating a minute
# interval. How do I access the slice object or otherwise discover a
# front-month contract to now begin analyzing/trading?
pass
def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
consolidator = QuoteBarConsolidator(timedelta(minutes=90))
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator(security.Symbol, consolidator)
self.consolidators[security.Symbol] = consolidator
for security in changes.RemovedSecurities:
consolidator = self.consolidators.pop(security.Symbol)
self.SubscriptionManager.RemoveConsolidator(security.Symbol, consolidator)
consolidator.DataConsolidated -= self.OnDataConsolidated