Hello All,
I have a universe selection algorithm already successfully stores OHLC (Open, High, Low, Close) data into rolling windows and prepopulates those windows with historical data. I would like add another rolling window that stores the timestamps associated with those OHLC entries, but I am not sure how query the timestamps from historical pandas dataframe. Can anyone offer some guidance? The last line of the block of code below is where I am trying to query the timestamp.
def OnSecuritiesChanged(self, algorithm, changes):
# clean up data for removed securities
symbols = [ x.Symbol for x in changes.RemovedSecurities ]
if len(symbols) > 0:
for subscription in algorithm.SubscriptionManager.Subscriptions:
if subscription.Symbol in symbols:
self.symbolDataBySymbol.pop(subscription.Symbol, None)
subscription.Consolidators.Clear()
# initialize data for added securities
addedSymbols = [ x.Symbol for x in changes.AddedSecurities if x.Symbol not in self.symbolDataBySymbol]
if len(addedSymbols) == 0: return
self.windowLength = max(self.strategyLookback + 90,self.volatilityLookback)
history = algorithm.History(addedSymbols, self.windowLength, self.resolution)
for symbol in addedSymbols:
self.openWindows[symbol] = RollingWindow[float](self.windowLength)
self.highWindows[symbol] = RollingWindow[float](self.windowLength)
self.lowWindows[symbol] = RollingWindow[float](self.windowLength)
self.closeWindows[symbol] = RollingWindow[float](self.windowLength)
self.timestampWindows[symbol] = RollingWindow[float](self.windowLength)
for tuple in history.loc[symbol].itertuples():
self.openWindows[symbol].Add(tuple.open)
self.highWindows[symbol].Add(tuple.high)
self.lowWindows[symbol].Add(tuple.low)
self.closeWindows[symbol].Add(tuple.close)
self.timestampWindows[symbol].Add(tuple.time)
Adam W
The DataFrame returned by `.History` has MultiIndex (symbol, time), so you can just take the index of the second level. Also might be cleaner to just put everything into one RollingWindow, like
Â
Varad Kabade
Hi Garrett,
Thank you, Adam, for your answer.
We recommend using a single RollingWindow which stores TradeBar objects so it would contain OHCLV data along with the time stamp.
To create TradeBar objects from rows of the historical data refer to the following snippet:
Best,
Varad Kabade
Garrett Grow
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!