Hello,
I'm doing a little experiment with tick data, I keep getting this error
Runtime Error: TypeError : 'NoneType' object is not subscriptable
at OnData
quantity = 0 if slice[self.ibm][-1].Quantity is None else slice[self.ibm][-1].Quantity
===
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 24
(Open Stacktrace)
even checking if the current slice contains the key
if not self.CurrentSlice.ContainsKey(self.ibm):
return
the code works at first but at some point I get the error, could it be a tick missing?
Thanks for the help!
here's the code
class RetrospectiveRedSalmon(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 11, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.ibm = self.AddEquity("IBM", Resolution.Tick).Symbol
self.sum = 0
self.td = 10000000
self.n = 1
self.count = 0
self.Schedule.On(self.DateRules.On(2021,11, 14), self.TimeRules.At(15,45), self.ExitPositions)
def OnData(self, slice):
if not self.CurrentSlice.ContainsKey(self.ibm):
return
#self.Debug(f"Last price: {tick[-1].LastPrice}")
#self.Debug(f"Last Quantity: {tick[-1].Quantity}")
quantity = 0 if slice[self.ibm][-1].Quantity is None else slice[self.ibm][-1].Quantity
self.sum = quantity + self.sum #slice['AAPL'] instead of ticks
tl = []
if abs(self.sum)/self.n == self.td:
tl[(self.n - 1)] = self.sum/self.n
if tl[-1] > 0 and tl[-2] is not None:
if tl[-2] > 0:
self.count = self.count + 1
else:
return
self.n = self.n + 1
def ExitPositions(self):
self.Liquidate()
self.Debug("p estimate: " + str(self.count))
Varad Kabade
Hi Renata,
The slice object consists of Trade events and Non-Trade events. Slice.ContainsKey checks if the current slice object has an event or not; sometimes, it may be a Non-trade event like splits, dividends, etc. To check for bars, we recommend :
Refer to the attached backtest.
Best,
Varad Kabade
Renata
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!