erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.81k stars 754 forks source link

Error 103 Duplicate order id when I specified an unique order id to the order #644

Closed aladdinaire closed 1 year ago

aladdinaire commented 1 year ago

The error message is as below:

my_order_id = 14403343 Error 103, reqId 14403343: Duplicate order id Canceled order: Trade(contract=Stock(conId=756733, symbol='SPY', exchange='SMART', primaryExchange='ARCA', currency='USD', localSymbol='SPY', tradingClass='SPY'), order=LimitOrder(orderId=14403343, clientId=9999, action='BUY', totalQuantity=1, lmtPrice=420), orderStatus=OrderStatus(orderId=14403343, status='Cancelled', filled=0.0, remaining=0.0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2023, 9, 19, 2, 45, 15, 36409, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2023, 9, 19, 2, 45, 15, 37409, tzinfo=datetime.timezone.utc), status='Cancelled', message='Error 103, reqId 14403343: Duplicate order id', errorCode=103)], advancedError='')

I would get the above error message when I run the following script multiple times in a few seconds.

from ib_insync import * import time

ib = IB() ib.connect('127.0.0.1', 7497, clientId=9999)

contract = Stock('SPY', 'SMART', 'USD')

my_order_id = int((time.time()%1)*(10**9)) print('my_order_id = ', my_order_id) order = LimitOrder('BUY', 1, 420, orderId=my_order_id)

ib.qualifyContracts(contract)
trade = ib.placeOrder(contract, order)

ib.waitOnUpdate()

open_orders = ib.openOrders() print('# of open_orders = ', len(open_orders))

ib.disconnect()

erdewit commented 1 year ago
my_order_id = int((time.time()%1)*(10**9))

There's no need to issue an orderId, ib_insync does that for you. In the way it is done above there's actually a fair chance of ID collisions.