Back

'NoneType' object is not iterable

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.SetCash(100000)
self.symbols = []
for ticker in ["ABX", "ABB"]:
option = self.AddOption(ticker)
self.symbols.append(option.Symbol)
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:
self.Log(str(x.Symbol.Value))
# 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:
self.Log(str(x.Symbol.Value))

Sometimes they work fine though. Why is that?

Kind regards,

Christian

Update Backtest







To make sure the option chain value isn't None, please add a check

chain = slice.OptionChains.GetValue(self.option_symbol)
if chain is None:
return
0

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.


Update Backtest





0

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.


Loading...

This discussion is closed