Accessing futures front-month contract within a Consolidator event method

Back

Hi all,

Apologies if this is the wrong style of question or if there's another place to put mechanics questions about the API and framework. As the title suggests, I'm a relative beginner here and having difficulty reading the documentation on Consolidators and applying that to a practical (non-example) algorithm. I've read the documentation reference and understand it in broad strokes, as well as read through the example futures consolidation code, but can't seem to distill how to now access a front-month contract within the Consolidator's event method.

My understanding from completing the Futures boot camp is that the OnData() method receives as parameters the slice object, which contains FutureChains to parse down for the front-month contract.

However, the process of registering an event handler for a Consolidator has a different signature, so if I rebase a minute resolution into, say, 90-minute bars, how do I go about parsing the FutureChains (and including additional trading logic) within the OnDataConsolidated() method?

Below is an attempt at a MWE I pieced together quickly to give a code illustration to what I'm attempting to describe.

class BasicTemplateFuturesConsolidationAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetCash(100000)

ES = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
ES.SetFilter(0, 182)

self.consolidators = dict()

def OnData(self,slice):
pass

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?

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

Thanks in advance for any help anyone can provide.

Update Backtest







 
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Eli,

Instead of trying to work with FuturesChains within the consolidator, to get only the front month contract we can do future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen()). I've shown this in the attached backtest.

Best,
Shile Wen

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!