Hi!
I am trying to incorporate a history call whenever my futures contract rolls over, then update my consolidator and indicator to save the algorithm from having to wait until the indicator is ready again. I am using 512 tick trade bars and am struggling with the history call. I have attached my code below, is this a problem with the structure of tick dataframe in the history call? I'd really appreciate any examples or nudges in the right direction.
class MyStrugglesContinuous(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1) #Set Start Date
self.SetEndDate(2021, 4, 1) #Set End Date
self.SetCash(15000)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.Settings.FreePortfolioValuePercentage = 0.3
self._continuousContract = self.AddFuture(Futures.Metals.Copper, Resolution.Tick,
dataNormalizationMode = DataNormalizationMode.Raw,
dataMappingMode = DataMappingMode.OpenInterest,
extendedMarketHours = True,
contractDepthOffset= 0)
self.consolidators = dict()
self._macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
self._currentContract = None
def OnData(self, data):
for changedEvent in data.SymbolChangedEvents.Values:
if changedEvent.Symbol == self._continuousContract.Symbol:
self.Log(f"SymbolChanged event: {changedEvent}")
def OnSecuritiesChanged(self, changes):
self.Liquidate()
self._currentContract = self.Securities[self._continuousContract.Mapped]
self.Debug(f"{self.Time}-{changes}")
self.Log(f"{self.Time}-{changes}")
for security in changes.AddedSecurities:
consolidator = TickConsolidator(512)
self.SubscriptionManager.AddConsolidator(security.Symbol, consolidator)
self.RegisterIndicator(security.Symbol, self._macd, consolidator)
self.consolidators[security.Symbol] = consolidator
consolidator.DataConsolidated += self.OnDataConsolidated
# make history call
history = self.History(self._currentContract.Symbol, timedelta(days=10), Resolution.Tick)
for index, row in history.iterrows():
bar = TradeBar(index[1], self._currentContract.Symbol, row.open, row.high, row.low, row.close, row.volume)
# Allow the consolidators to update
consolidator.Update(data[self._currentContract.Symbol])
self._macd.Update(data[self._currentContract.Symbol].EndTime, data[self.symbol].Close)
#self._macd.Update(bar)#.Index[1], bar.close)
for security in changes.RemovedSecurities:
consolidator = self.consolidators.pop(security.Symbol)
self.SubscriptionManager.RemoveConsolidator(security.Symbol, consolidator)
consolidator.DataConsolidated -= self.OnDataConsolidated
Cheers!
Derek Melchin
Hi Ariel,
The code above tries to create TradeBar objects from the DataFrame of ticks. The issue is that the DataFrame has no OHLCV properties, but the following line is trying to access them:
Consider requesting a list of Tick objects instead of a DataFrame.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Arthur Asenheimer
Derek MelchinÂ
I would like to request a list of Tick objects instead of a DataFrame, but it seems there is a bug with History[Tick](…) as it returns only a small portion of the Ticks. See also this GitHub  issue. Â
Derek Melchin
Hi Arthur,
Thanks for opening the GitHub Issue. The LEAN team will address it.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Ariel Nechemia
Thank you Derek and Arthur for your input. Once I call on tick data as a list like above I would still have to manually update the consolidator and the indicator with the tick data right? Could you show me a snippet of code to see how it's done? I'm confused about how the data would be structured in this cased, I've attached what I have below.
Â
Derek Melchin
Hi Ariel,
See the attached backtest for reference.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Ariel Nechemia
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!