I'm receiving this in my backtest:
Data for symbol IR has been limited due to numerical precision issues in the factor file. The starting date has been set to 3/9/2020.
Then right after, I'm getting this error:
Runtime Error: KeyNotFoundException : 'IR R735QTJ8XC9X' wasn't found in the TradeBars 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("IR R735QTJ8XC9X")
The backtest is for 2016-2019, how can I just remove all securities from my universe that do not have data relative to the timeframe of the backtest.
My logic in my OnData function is the following:
if self.IsWarmingUp:
return
if data.Count < len(self.universe):
self.Debug("Not Ready. Getting data...")
return
self.Debug("Initialized! All data is present.")
# Add to and update all the moving averages
for symbol in self.universe:
if symbol not in self.averages:
# 1. Call history to get an array of day of longest average of history data
history = self.History(symbol, self.longAvg, Resolution.Daily)
#2. Adjust SelectionData to pass in the history result
self.averages[symbol] = SelectionData(history, self.longAvg, self.shortAvg)
self.averages[symbol].update(self.Time, tradeBars[symbol].Close)
Basically, I'm calculating moving averages for every stock in my universe and adding it to a dictionary later, just like in the tutorials. However, when I hit that security, it bugs out at me.
Is there a HasData property on these security I can check for?
Rahul Chowdhury
Hi John,
The data received in OnData represents all the data available at that time slice. Sometimes data for a symbol may not be available for a specific time slice.Since you are using trade bars data, you just need to check if trade bar data is available for that symbol at that time slice with
data.Bars.ContainKey(symbol)
Best
Rahul
John Radosta
Is it not available because the data event has not arrived to the OnData event handler or is it because data is missing for that security? If its the former, how do I let make the algorithm wait for the data event to arrive, I already have:
if self.IsWarmingUp: return
Rahul Chowdhury
Hi John,
It is because the data is missing for that time slice. The data may be available in the next time slice. To wait for the data, simply wait for the next iteration of OnData. We can do this by checking if the data is there with
# Returns True if TradeBar data is present else False if not data.Bars.ContainKey(symbol): return
and doing nothing if the data is not present.
Best
Rahul
John Radosta
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!