I get that error when working with options on certain stocks on certain dates.

from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2018, 6, 1)
self.SetEndDate(2018, 9, 13)
self.symbols = []
for ticker in ["ABX", "ABB"]:
option = self.AddOption(ticker)
option.SetFilter(-3, +3, timedelta(0), timedelta(180))

def OnData(self,slice):
#if self.Portfolio.Invested: return
for symbol in self.symbols:
chain = slice.OptionChains.GetValue(symbol)
for x in chain:
if x.Right == 0:
# we sort the contracts to find at the money (ATM) contract with farthest expiration
contracts = sorted(sorted(sorted(chain,
key = lambda x: abs(chain.Underlying.Price - x.Strike)),
key = lambda x: x.Expiry, reverse=True),
key = lambda x: x.Right, reverse=True)

# if found, trade it
if len(contracts) == 0: continue
symbol = contracts[0].Symbol
if self.Portfolio[symbol].Invested: return
self.MarketOrder(symbol, 1)

These lines are causing the problem:

for x in chain:
if x.Right == 0:

Sometimes they work fine though. Why is that?

Kind regards,