Hey QC Community! I'm new to the platform and absolute love it ❤️

I was trying to use custom data to run an Options Backtest, although I was successfully able to import the custom data, I'm not able to send Buy orders. I'm assuming there is some issue with my BuyCall() and BuyPut() methods.

Would be great if you could help me out!

OnData() method:

    def OnData(self, data):
        if data.ContainsKey(self.symbol):
            value = data[self.symbol].Value
            self.Log("Data Added: " + str(value))
            data_list.append(value)

        # To check if portfolio is already invested
        option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==Se		curityType.Option]

        if option_invested:
            if self.Time + timedelta(1) > option_invested[0].ID.Date: # If only one day is left until expiration, we liquidate the position
                self.Liquidate(option_invested[0], "Too close to expiration")
            return

        # code logic not already invested
        try:
            if not self.Portfolio.Invested:
                if data_list[-1] > data_list[-2]: # If current score is more than the previous one, Buy Call
                    self.Log("Buy Call Option")
                    for i in data.OptionChains:
                        chains = i.Value
                        self.BuyCall(chains)
                
                elif data_list[-1] < data_list[-2]: # If current score is less than the previous one, Buy Put
                    self.Log("Buy Put Option")
                    for i in data.OptionChains:
                        chains = i.Value
                        self.BuyPut(chains)
            else:
                self.Log("NO NEW TRADE: PORTFOLIO ALREADY INVESTED")
        except:
            pass

The custom BuyCall() method:

    def BuyCall(self, chains): # To find the right option and buy it
        expiry = sorted(chains, key = lambda x: x.Expiry, reverse = True)[0].Expiry # Picking the furthest away expiration date
        calls = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Call]
        call_contracts = sorted(calls, key = lambda x: abs(x.Strike - x.UndetlyingLastPrice))

        if len(call_contracts) == 0:
            return
        self.call = call_contract[0]

        quantity = self.Portfolio.TotalPortfolioValue / self.call.AskPrice
        quantity =  int(0.05 * (quantity / 100))

        self.Buy(self.call.Symbol, quantity)

The custom BuyPut() method:

    def BuyPut(self, chains): # To find the right option and buy it
        expiry = sorted(chains, key = lambda x: x.Expiry, reverse = True)[0].Expiry # Picking the furthest away expiration date
        puts = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Put]
        put_contracts = sorted(calls, key = lambda x: abs(x.Strike - x.UndetlyingLastPrice))

        if len(call_contracts) == 0:
            return
        self.call = call_contract[0]

        quantity = self.Portfolio.TotalPortfolioValue / self.call.AskPrice
        quantity =  int(0.05 * (quantity / 100))

        self.Buy(self.call.Symbol, -quantity)