Trying to code an example algo after taking all of the boot camps. I am stuck having a problem updating the rolling window with a consolidator bar. I am getting a typeError which I assume means the consolidated bar doesn't match the "quotebar" attribute. Am I correct? If so, how would I go about fixing this? Also another dumb question: This example I am trying to edit here doesn't seem to parse the futures chain and pick the one with the most volume as the bootcamp example did. How is the backtest working? Thanks in advance!
class BreakOutExample(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 12, 1) # Set Start Date
self.SetEndDate(2020, 12, 31) # Set End Date
self.SetCash(1000000) # Set Strategy Cash
# Subscribe and set our expiry filter for the futures chain
future = self.AddFuture(Futures.Indices.SP500EMini)
# future.SetFilter(timedelta(0), timedelta(182))
self.consolidators = dict()
self.window = RollingWindow[TradeBar](3)
dailyhigh = 0
dailylow = 0
def OnDataConsolidated(self, bar):
self.window.Add(bar.Symbol)
symbol = bar.Symbol
price = self.Securities[symbol].Close
#Debug Testing
#self.Debug(" Bar Close " + str(bar.High))
#self.Debug(" Bar Time Hour " + str(bar.Time.hour))
#self.Debug(" Bar Time Min " + str(bar.Time.minute))
dailyhigh = max(window[0].High, window[1].High, window[2].High)
self.Debug(" Daily High is now: " + str(dailyhigh))
dailylow = min(window[0].Low, window[1].Low, window[2].Low)
self.Debug(" Daily Low is now: " + str(dailylow))
if price >= dailyhigh:
self.SetHoldings(symbol, 1)
if price <= dailylow:
self.SetHoldings(symbol,-1)
def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
symbol = security.Symbol
consolidator = self.Consolidate(symbol, timedelta(minutes=3), self.OnDataConsolidated)
self.consolidators[symbol] = consolidator
for security in changes.RemovedSecurities:
symbol = security.Symbol
consolidator = self.consolidators.pop(symbol)
self.SubscriptionManager.RemoveConsolidator(symbol, consolidator)
consolidator.DataConsolidated -= self.OnDataConsolidated
if security.Invested:
self.Liquidate(symbol, 'Remove from Futures Chains')
Derek Melchin
Hi Justin,
Instead of using a RollingWindow, it's more convenient in this situation to utilize the Minimum and Maximum indicators.
The algorithm above works because the bar passed to OnDataConsolidated corresponds to an individual a futures contract. Thus, SetHoldings purchases the contract. To navigate the FuturesChain, we should update the indicators of each individual contract in the OnDataConsolidated method but place our ordering logic inside an OnData method.
See the attached backtest for reference. Note that the example below trades gold futures instead of the SP500 E-mini to ensure we have multiple contracts to trade.
Best,
Derek Melchin
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.
Justin Pedersen
Derek! Thank you so much for the insight and guidance. I will take this and experiment. I really appreciate it!
Justin Pedersen
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.
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!