开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Unable to determine when an order is submitted


 

Hi all,

I am unable to determine when an order is submitted. See the below code of
my on_order_status event. What am I doing wrong? The on_order_status event
does fire. I suspect I have the if condition wrong but am not certain.


# Event handler for order status updates
def on_order_status(trade):
print("order status event fired")
if trade == stop_loss_trade and trade.orderStatus.status ==
"PreSubmitted":
print("Stop loss order submitted, disconnecting.")
ib.disconnect()
sys.exit(0)

Pranav


 

PreSubmitted is not the status you want, as that still does not mean active. What you want is Submitted. You can look at for different possible statuses.
?
But for your case, even simpler, trade object already has method which will verify if Submitted. Use that.


 

开云体育

Hi biney59,

Thanks for your suggestion.

?

I got rid of the event logic and the program is working as I want it to at least in the simulator.

?

Here is a snippet of the modified code.

? ? # Create the limit order (parent order)

? ? limit_order = LimitOrder(order_type, order_quantity, limit_price, outsideRth=True, transmit=False)

? ?

? ? # Place the limit order

? ? print("Placing limit order")

? ? parent_trade = ib.placeOrder(contract, limit_order)

? ?

? ? # Create the stop loss order (child order)

? ? stop_loss_order = Order()

? ? stop_loss_order.action = stop_loss_action

? ? stop_loss_order.orderType = "STP"

? ? stop_loss_order.totalQuantity = order_quantity

? ? stop_loss_order.auxPrice = stop_loss_price

? ? stop_loss_order.parentId = parent_trade.order.orderId

? ? stop_loss_order.transmit = True

? ?

? ? # Place the stop loss order (this transmits both orders)

? ? print("Placing stop loss order")

? ? stop_loss_trade = ib.placeOrder(contract, stop_loss_order)

? ? if stop_loss_trade.isActive():

? ? ? ? ib.disconnect()

? ? ? ? print("Exiting")

? ? ? ? sys.exit(0)

? ? # Run the event loop to process order status updates

? ? ib.run()

?

?

Pranav


 

Might work in simulation, but remember in real trade there will be delay after placing order:
?

? ? stop_loss_trade = ib.placeOrder(contract, stop_loss_order)

# BELOW might not be true always so quickly!

? ? if stop_loss_trade.isActive():

?

You should stick to event driven for robustness as you had before.


 

Hi Prenav,?
?
be careful on choosing the status.. I've noticed in paper trading that stutus (PreSubmitted) could never be reached as those statuses are executed very quickly. Moreover the status gets fired once and not for each status.
?
Elat


 

开云体育

Hi Elat,

<snip Moreover the status gets fired once and not for each status.

PL] Ouch, that is one reason why my code is not executing the way I expect. I’ll do some tweaking.

?

Thanks for your input.

Pranav