3

I am having some strange behavior while sending orders to interactive brokers.

Consider this code:

from ib_insync import *
import time

ib = IB()
client_id = int(time.time())  # gets second since epoch. No need for the milliseconds, so round to int
ib.connect('127.0.0.1', 7497, clientId=client_id, timeout=10)

ib.placeOrder(contract=Stock('SPY', exchange='SMART', currency='USD'), order=MarketOrder('BUY', 100))
ib.placeOrder(contract=Stock('AAPL', exchange='SMART', currency='USD'), order=MarketOrder('BUY', 100))
ib.placeOrder(contract=Stock('IBM', exchange='SMART', currency='USD'), order=MarketOrder('BUY', 100))

When I run it, only the first order is displayed in TWS: enter image description here

However, if I then run ib.sleep(0) the other orders appear in TWS:

enter image description here

Any idea what may be causing this issue? I can put ib.sleep(0) in different spots throughout my code to make sure information is being transmitted, but this is a workaround.

Thank you.

mks212
  • 901
  • 1
  • 18
  • 40
  • I've experienced a similar thing. When trying to attach child orders to a parent order and sending them to TWS, only the parent order showed up in TWS. In order to make it work, I had to call _ib.sleep(0)_ between each _ib.placeOrder()_ call. – bluebox Nov 13 '21 at 22:29

0 Answers0