https://www.quantconnect.com/tutorials/strategy-library/can-crude-oil-predict-equity-returns
Error Message:Runtime Error: In Scheduled Event 'SPY: MonthStart: SPY: 0 min after MarketOpen', Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the OILB WFO93GQ2HBVP key exist in the collection and/or that collection is not empty. at pandas._libs.hashtable.PyObjectHashTable.get_item File "hashtable_class_helper.pxi" in hashtable_class_helper.pxi:line 1614 KeyError : 'OILB WFO93GQ2HBVP'
Stacktrace:In Scheduled Event 'SPY: MonthStart: SPY: 0 min after MarketOpen'
===========
I have used hist.empty to check if hist is empty, but still receiving the same error.
Do I also need to check whether or not the column 'close' is empty for spy and oil?
Thanks!
AK M
Not quite sure. Quick workaround to just get the example running is to use something comparable. USO correlates > 0.97 most of the time, so try that.
Arthur Asenheimer
You should check if the desired symbol is in index. hist dataframe wasn't empty, but there were only data for SPY:Â https://i.imgur.com/hreB63S.png
Trading for OIL started around april 2011, but your backtest starts at 2010/1/1. In addition, you should also check if there is enough data available via len(df.index) or df.shape[0].Â
Adding the following two lines after the history request solves the issue:Â
if not all(symbl in hist.index.levels[0] and hist.loc[symbl].shape[0] >= self.lookback for symbl in [self.oil, self.spy]):
return
Â
Â
Spacetime
Arthur Asenheimer , thanks for the inputs and it makes sense. I do come across these sorts of errors in other projects, so appreciate your explanation.
AK M , thanks for the inputs. I have tried this workaround already before opening this discusion. I wanted to know how to actually solve errors like these.
Spacetime
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!