I am using the Algorithm Framework, and have an Alpha in which I'm trying to maintain a pandas dataframe of the most recent 5 days of Daily history for a small set of securities.
In OnSecuritiesChanged, I have the code that gets the initial history for anything added:
## In OnSecuritiesChanged
symbols_added = [ x.Symbol for x in changes.AddedSecurities ]
df_symbol_history = algorithm.History(symbols_added, self.lookback, self.resolution)[['close']]
self.df_history = pd.concat([self.df_history, df_symbol_history])
In Update, I add the new data:
records = [{ 'symbol': kvp.Value.Symbol, 'time': kvp.Value.Time, 'close': kvp.Value.Close} for kvp in slice]
df_slice = pd.DataFrame.from_records(records, index=['symbol', 'time'])
##Failing on this next line
self.df_history = pd.concat([self.df_history, df_slice])
This seems to me to be the most straightforward thing in the world, but it's not working. When I run this as is, Update ends up throwing a ‘Categorical categories must be unique’ exception. I've confirmed that there are no categorical datatypes in the dataframe (and you can see from the code that should be impossible).
Any ideas what I'm doing wrong here?
Lucas
Hi Shaun.
What you could do, and what would be more efficient, is to instanciate a rolling window of a value of 5 for every security. With every update, you update the rolling window with the closing window. If you then need a pandas dataframe, you could do the following
If this is not a approach you like, I would like to help, but I would need the full example ( and seems the IDE is down right now)
Have a good day
Lucas
Shaun Dawson
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!