Hi QC Community,

I am having an issue with an option strategy I am testing and would love if someone can help me out. The order of operations is: buy signal generated, limit order is placed, after a certain amount of time if the order is not yet filled, the algo will attempt to update the order using the then current bid/ask prices. But the issue is that the price will only update once as you can see in the image below. Order is originally submitted at $0.5 then updated to $0.7, and then not again, though it goes through the motions like it is updating. This happens for both buy and sell orders. Relevant code is pasted below.

What am I doing wrong? I guess my two questions are, how can I call the most recent bid and ask prices for both:

  1. Option contracts for which I have an open order, and 
  2. Option contracts that I am currently holding

 

Or am I doing something wrong in the first block of code where the algo checks if there's an open buy/sell order?

I have been teaching myself Python/QC side by side so apologies if my questions are elementary, and apologies if this is the wrong format. I've been able to debug everything else so far but not this, and at the risk of pulling my hair out I thought I'd post here. Thank you in advance. 

243045_1694731489.jpg

This is the block of code in OnData that calls the below methods and then updates the buy/sell order tickets to what's supposed to be a new price. 

            if self.minute_bar_count == 15:
                if self.buy_order_ticket:
                    self.Debug(f"buy_order_ticket: {self.buy_order_ticket}")
                    # Increase buy limit price
                    
                    self.update_buy_limit_order_price()

                    # Update the limit price
                    response = self.buy_order_ticket.UpdateLimitPrice(self.buy_order_price, tag=f"Buy limit updated: {self.buy_order_price}")

                    # Minute counter reset
                    self.minute_bar_count = 0

                    # Check the OrderResponse
                    if response.IsSuccess:
                        self.Debug(f"Buy order successfully updated: {self.buy_order_ticket}")

                if self.sell_order_ticket:
                    self.Debug(f"sell_order_ticket: {self.sell_order_ticket}")
                    # Lower sell limit price
                    
                    self.calculate_sell_limit_order_price()

                    # Update the limit price
                    response = self.sell_order_ticket.UpdateLimitPrice(self.sell_order_price, tag=f"Sell limit updated: {self.sell_order_price}")

                    # Minute counter reset
                    self.minute_bar_count = 0

                    # Check the OrderResponse                
                    if response.IsSuccess:
                        self.Debug(f"Sell order successfully updated: {self.sell_order_ticket}")

Update buy limit order price method called in OnData. Calculate sell limit price as well:

    def update_buy_limit_order_price(self):
        option_symbol = self.buy_order_ticket.Symbol
        option_security = self.Securities[option_symbol]
        if option_security:
            b_bid = option_security.BidPrice
            b_ask = option_security.AskPrice
            if b_bid == 0 or b_ask == 0:
                self.buy_order_bid_ask_zero = True
                self.Debug(f"buy_order_bid_ask_zero is True: {b_bid}, {b_ask}")
            else:
                b_diff_price = b_ask - b_bid
                b_avg_price = round((b_bid + b_ask)/2, 1)
                self.buy_order_price = round(b_avg_price + (b_diff_price * 0.75), 1)
                self.Debug(f"Buy order price updated: bid: {b_bid}, ask: {b_ask}, buy_order_price: {self.buy_order_price}")





    def calculate_sell_limit_order_price(self):
        # Calculate sell limit order price
        option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
        for option in option_invested:
            s_bid = self.Securities[option].BidPrice
            s_ask = self.Securities[option].AskPrice
            s_diff_price = s_ask - s_bid
            s_avg_price = round((s_bid + s_ask)/2, 1)
            self.sell_order_price = round(s_avg_price - (s_diff_price * 0.75), 1)
            self.Debug(f"Calculate sell called: bid: {s_bid}, ask: {s_ask}, sell_order_price: {self.sell_order_price}")