开云体育

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

How to place multiple orders AT THE SAME TIME?


 

I want to place two orders and they are OCA.
I have defined the ocsGroup to bind these two orders, HOWEVER, I found I cannot place two orders just one after another. I need to insert an ib.sleep(2) between the two placeOrder to make them work
Sometimes, the first order will just fire while the second order will wait for 2s to arrive....
?
Can someone help me with this?
?
oca_group = f"OCA_Group_{datetime.now().strftime('%Y%m%d%H%M%S')}"

# Create the Buy Stop order (do not transmit yet)
buy_stop_order = Order(
? ? action='BUY',
? ? totalQuantity=ORDER_QUANTITY,
? ? orderType='STP',
? ? auxPrice=buy_stop_price,
? ? tif='DAY',
? ? ocaGroup=oca_group,
? ? ocaType=1, ?# OCA type
? ? transmit=True
)

# Create the Sell Stop order (this will transmit the group)
sell_stop_order = Order(
? ? action='SELL',
? ? totalQuantity=ORDER_QUANTITY,
? ? orderType='STP',
? ? auxPrice=sell_stop_price,
? ? tif='DAY',
? ? ocaGroup=oca_group,
? ? ocaType=1,
? ? transmit=True ?# Transmit the group
)

# Submit the OCA order group
print(f"Submitting Buy Stop and Sell Stop orders. OCA Group: {oca_group}")
ib.placeOrder(stock, buy_stop_order) ?# Create the Buy Stop order without transmitting
ib.sleep(2) ##### I REALY WANT TO REMOVE THIS 2S AND MAKE SURE THEY ARE PLACED AT THE SAME TIME#############
ib.placeOrder(stock, sell_stop_order) ?# Create and transmit the Sell Stop order (activates the group)
?
?


 
Edited

Hey zangyythu,?
?
I had a similar issue. Needed to fire off a bunch of orders asap. I use the below to pause for the least amount of time possible.?
?
I should mention that I'm not the greatest python user so if someone else has a better solution, please post it.?
?
def wait_for_trade_to_exist(trade):
? ? # if not trade: trade = Trade() # uncomment to easy trigger intelli-sense
? a = i = 0
? ? while a == 0:
? ? ? i += 1
? ? ? ? if trade in ib.openTrades() and trade.order in ib.openOrders():
? ? ? ? ? ? a = 1
? ? ? ? ib.sleep(0.05)
? ? ? ? if i > 40: a = 1 # 40 * 0.05 => wait upwards of 2 seconds
? ? return trade
?
trade = ib.placeOrder(option_contract, order)
wait_for_trade_to_exist(trade)
trade_2 = ib.placeOrder(option_contract_2, order)
wait_for_trade_to_exist(trade_2)


 

Another idea could be to submit both orders using the Good After Time field. Something like this:?
?
now_plus_3_seconds = datetime.now(tz:=ZoneInfo(key='America/New_York')) + timedelta(seconds = 3)
gat_str = now_plus_3_seconds.strftime('%Y%m%d %H:%M:%S') + ' US/Eastern'
order_1.goodAfterTime = gat_str
order_2.goodAfterTime = gat_str


 

How about an Hedging Order? I use it, it works well


 

Hedging Orders


 

Your first order has transmit=True, it needs to be transmit=False, and you need to add the parent's order id to your exit orders.
?
The last order in the group that has transmit = True will submit all the orders in the group and you don't need any ib.sleep.


 

Note that in some cases it will be necessary to include a small delay of 50 ms or less after placing the parent order for processing, before placing the child order. Otherwise the error “10006: Missing parent order” will be triggered.

I use ib.sleep(0.2)


 

Check out this video for more info https://www.youtube.com/watch?v=D76CjP2fbhg


 

On Wed, Dec 11, 2024 at 06:58 AM, Kianti wrote:

Note that in some cases it will be necessary to include a small delay of 50 ms or less after placing the parent order for processing, before placing the child order. Otherwise the error “10006: Missing parent order” will be triggered.

Interesting.. I've never done that and never seen that error. Not sure why but good to know.