I would like to dynamically add/remove securities after universe selection in an Alpha Class while also having a Consolidator for the Bar data. Therefore I cannot use self.warmup(…) because the warmup must happen every time when adding a security. I got it to work but only without the Consolidation layer.
With the consolidator I would have to manually update the Consolidator during warmup phase.
Is it correct to use Consolidator.Update(slice.Bars[s]) for this?
self.symbolData = {}
def OnSecuritiesChanged(self, algorithm, changes):
for added in changes.AddedSecurities:
s = added.Symbol
# add data object
if not s in self.symbolData.keys():
self.symbolData[s] = SymbolData(algorithm, added, self.resolution,self.barperiod, self.lookback)
# add consolidator OHCLV
if self.symbolData[s].Consolidator is not None: raise Exception("Consolidator is already existing or was not remvoved")
self.symbolData[s].Consolidator = TradeBarConsolidator(self.symbolData[s].timespan)
self.symbolData[s].Consolidator.DataConsolidated += self.OnDataConsolidated
algorithm.SubscriptionManager.AddConsolidator(s, self.symbolData[s].Consolidator)
# history warmup
hist = algorithm.History(self.lookback)
for added in changes.AddedSecurities:
s = added.Symbol
for slice in hist:
self.Debug(f"time={algorithm.Time},symbol={s},warmup")
self.symbolData[s].Consolidator.Update(slice.Bars[s])
for removed in changes.RemovedSecurities:
s = removed.Symbol
# remove data object
symbolData = self.symbolData.pop(s, None)
# remove consolidator
if symbolData is not None:
algorithm.SubscriptionManager.RemoveConsolidator(s, symbolData.Consolidator)
def OnDataConsolidated(self, sender, bar):
# bar.Time, bar.Close, bar.Open ...
s=bar.Symbol.Value
self.Debug(f"time={bar.Time},symbol={s},OnDataConsolidated")
self.symbolData[s].bar = bar
self.symbolData[s].time = bar.Time
self.symbolData[s].MACD.Update(bar.Time, bar.Close)
self.symbolData[s].window_close.Add(bar.Close)
Varad Kabade
Hi Santa24,
After reviewing your code, we think it is correct. Can you please tell us if it is working as expected?
Moving forward we recommend using the EndTime attribute of TradeBar to update the indicators:
We have an example algorithm that implements the above logic in AlphaModel with a ROC indicator. Refer to the following link.
Best,
Varad Kabade
Santa24
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!