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:
- Option contracts for which I have an open order, and
- 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.
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}")
Mia Alissi
Based on the provided extracts, it seems like you are correctly using the
UpdateLimitPrice
method to update the limit price of your orders. However, without more context or error messages, it's hard to pinpoint exactly why the price is only updating once.One thing to note is that when you update an order, LEAN creates an
UpdateOrderRequest
object. This object has several attributes, includingOrderRequestType
,Quantity
,LimitPrice
,StopPrice
, andTriggerPrice
. If you want to change the quantity of the order, you should update theQuantity
attribute. Similarly, if you want to change the limit price of the order, you should update theLimitPrice
attribute.Here is an example of how to update the limit price and quantity of an order:
And here is how to update individual fields of an order:
If the update is successful,
response.IsSuccess
will beTrue
.Regarding your questions about obtaining the most recent bid and ask prices, you can get these values from the
Securities
object. For example:This will give you the most recent bid and ask prices for the specified option contract.
If you're still having issues, it might be helpful to post more details about the problem or any error messages you're seeing on the QuantConnect Forum.
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.
Holt Norman
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!