This part of code I am trying to exit my bought call or put position if certain conditions are meet or the expiration date for the options is the next day. However my program seems to sell the options then continues to short the contract after selling it. I can't seem to figure out how to fix it.

 

def OnData(self, slice): #'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.''' #if not self.Portfolio.Invested: if not self.Portfolio.Invested: if self.bullSqueeze(slice) == True: self.Log("Bullish squeeze") optionchain = slice.OptionChains for i in slice.OptionChains: if i.Key != self.symbol: continue chains = i.Value contract_list = [x for x in chains] # if there is no contracts in this optionchain, pass the instance if (slice.OptionChains.Count == 0) or (len(contract_list) == 0): return # if there is no securities in portfolio, trade the options #if not self.Portfolio.Invested: self.TradeCallOptions(optionchain) elif self.bearishSqueeze(slice) == True: self.Log("Bearish squeeze") optionchain = slice.OptionChains for i in slice.OptionChains: if i.Key != self.symbol: continue chains = i.Value contract_list = [x for x in chains] # if there is no contracts in this optionchain, pass the instance if (slice.OptionChains.Count == 0) or (len(contract_list) == 0): return # if there is no securities in portfolio, trade the options #if not self.Portfolio.Invested: self.TradePutOptions(optionchain) if self.Portfolio.Invested: option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option] for a in option_invested: open_orders = self.Transactions.GetOpenOrders(a) if len(open_orders) == 0: if a.ID.OptionRight == OptionRight.Call: if self.Time.date() == (self.Securities[a].Expiry.date()+timedelta(1)): self.LimitOrder(a, -(self.quantityC), self.Securities[a].BidPrice) list(filter(lambda c: c != a, option_invested)) elif self.bullishTradeCheck(slice) == True: self.LimitOrder(a, -(self.quantityC), self.Securities[a].BidPrice) list(filter(lambda c: c != a, option_invested)) elif a.ID.OptionRight == OptionRight.Put: if self.Time.date() == (self.Securities[a].Expiry.date()+timedelta(1)): self.LimitOrder(a, -(self.quantityP), self.Securities[a].BidPrice) list(filter(lambda c: c != a, option_invested)) elif self.bearishTradeCheck(slice) ==True: self.LimitOrder(a, -(self.quantityP), self.Securities[a].BidPrice) def OnOrderEvent(self, orderEvent): self.Log("order event") self.Log(str(orderEvent)) def TradeCallOptions(self,optionchain): for i in optionchain: self.Log("in trade options function") if i.Key != self.symbol: continue chain = i.Value # sorted the optionchain by expiration date and choose the furthest date expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry # filter the call options from the contracts expires on that date call = [i for i in chain if i.Expiry == expiry and i.Right == 0] # sorted the contracts according to their strike prices call_contracts = sorted(call,key = lambda x: x.Strike) if len(call_contracts) == 0: continue self.call = call_contracts[0] self.quantityC = self.CalculateOrderQuantity(self.call.Symbol, .05) self.LimitOrder(self.call.Symbol, int(self.quantityC), self.call.AskPrice) def TradePutOptions(self,optionchain): for i in optionchain: self.Log("in trade options function") if i.Key != self.symbol: continue chain = i.Value # sorted the optionchain by expiration date and choose the furthest date expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry # filter the call options from the contracts expires on that date put = [i for i in chain if i.Expiry == expiry and i.Right == 1] # sorted the contracts according to their strike prices put_contracts = sorted(put,key = lambda x: x.Strike) if len(put_contracts) == 0: continue self.put = put_contracts[0] self.quantityP = self.CalculateOrderQuantity(self.put.Symbol, .05) self.LimitOrder(self.put.Symbol, int(self.quantityP), self.put.AskPrice)

Author