Hi folks, Below is my snippet. While checking if the data is present using if condition, the slice contains the symbol, hence not going inside the if loop. And in the next line while trying to access the TradeBar, it is throwing an error like “data is not present at this moment”.
for symbol in self.symbols:
# check if symbol is present in slice else continue to the next iteration
if not(slice.ContainsKey(symbol) and slice[symbol]):
self.Log(f"{self.Time} | {symbol} not present in slice.")
continue
trade_bar = slice.Bars[symbol]
self.Log(f"{trade_bar}")
I also tried accessing with data using the below code
def OnData(self, data):
if not data.Bars.ContainsKey(self.symbols[0]):
self.Log(f"{self.symbols[0]} not present in slice.")
return
bar = data.Bars[self.symbols[0]]
self.Log("Bar close {} high {}".format(bar.Close,bar.High))
Its returning with “symbol not present in slice”.
My question is slice is having the information whereas tradebar is not having the information. How is it possible? Could you please help me out on this? Am I missing something?
Mia Alissi
It seems like you're encountering an issue where the
Sliceobject contains the symbol, but theTradeBarobject does not. This could be due to the fact that not all symbols in theSliceobject have trade bar data at every time step.In QuantConnect, the
Sliceobject is a time-slice of all the data at a single time point. It contains all the data types you've subscribed to, indexed by the symbol objects. TheTradeBarobject, on the other hand, represents trade bar data for a particular symbol.When you check if the symbol is in the
Sliceobject, it only checks if there's any data for that symbol at the current time point, not specificallyTradeBardata. So, it's possible for a symbol to be in theSliceobject but not haveTradeBardata.To avoid the error, you should check if the symbol is in
slice.Barsbefore trying to access theTradeBardata. Here's how you can do it:This way, you're specifically checking if there's
TradeBardata for the symbol.For more information, you can refer to the QuantConnect documentation on handling data.
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.
Mohamed Muzamil
Hi Mia,
Thanks for your reply. I've attached my backtest below. So basically I'm using 1min resolution custom data and the data is available for the entire backtest period. So, I'm curious why is it not having the TradeBar data for these two symbols
Mia Alissi
From the code you've shared, it seems like you're trying to access
TradeBardata for custom data symbols. However,TradeBardata is specific to QuantConnect's built-in data types and may not be available for custom data.In your custom data class
MyHistoricalMinData, you've defined custom properties such as "Open", "High", "Low", and "Close". To access these properties, you should use theSliceobject indexed by the symbol, notslice.Bars.Here's how you can do it:
This way, you're accessing the custom data properties directly from the
Sliceobject.For more information, you can refer to the QuantConnect documentation on custom data.
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.
Mohamed Muzamil
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!