I've had to skip this lesson and move on. But, I don't like leaving things in the air.
I got this message:
SPY' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("SPY")
at on_data
spy_price = data["SPY"].close
~~~~^^^^^^^
It doesn't seem to matter if I run the code I edited or the Solution code. Both result the same.
At first I thought this might happen if the market wasn't open. But today, now, it surely is open. So that can't be it I guess.
Any suggestions? Here is the code:
# region imports
from AlgorithmImports import *
# endregion
class MomentumBasedTacticalAllocation(QCAlgorithm):
def initialize(self):
self.set_start_date(2007, 8, 1) # Set Start Date
self.set_end_date(2010, 8, 1) # Set End Date
#1. Subscribe to SPY -- S&P 500 Index ETF -- using daily resolution
self.spy = self.add_equity("SPY", Resolution.DAILY)
#2. Subscribe to BND -- Vanguard Total Bond Market ETF -- using daily resolution
self.bnd = self.add_equity("BND", Resolution.DAILY)
#3. Set strategy cash to $3000
self.set_cash(3000)
def on_data(self, data):
spy_price = data["SPY"].close
bnd_price = data["BND"].close
pass
Mia Alissi
The error message you're seeing is indicating that at the time your algorithm is trying to access the price data for "SPY", there is no data available in the
Slice
object. This could be due to a variety of reasons, such as the data not being available at that specific time, or the data not being properly subscribed to.In your code, you're trying to access the close price of "SPY" and "BND" directly from the
Slice
object. However, it's recommended to first check if the data exists in theSlice
object before trying to access it. You can do this by using theslice.ContainsKey(symbol)
method.Here's how you can modify your
on_data
method to first check if the data exists:This will ensure that your algorithm only tries to access the price data if it's available in the
Slice
object.If you're still encountering issues, it might be due to data errors. Data errors do happen, as no data in trading is perfect. You can check out the data issues discussion on the QuantConnect forum for more information.
Sources:
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.
Fred Marshall
Thank you! I added output so we could see where data was missing. The results were unexpected with 1 to 3 missing data points per month. How can we explain this? I did learn a lot but data like this wouldn't be expected nor acceptable in live trading.
Louis Szeto
Hi Fred
You may use `self.log` method, but be aware that there is a log limit based on your organization plan.
Best
Louis
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.
Fred Marshall
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!