Search code examples
javainteractive-brokersibrokers

Order id placeorder Java interactive brokers


Currently each time I place an order with

order.m_action = "BUY";
order.m_totalQuantity = 1;
order.m_lmtPrice = 4.00;
order.m_orderType = "LMT";  
order.m_account = "U123123";
int randomNum = ThreadLocalRandom.current().nextInt(1, 5564 + 1)                    
m_s.placeOrder(randomNum, c, order);

I am getting the error:

1041 103 Duplicate order id

Any ideas on generating a new id for new entry orders?

Thanks.


Solution

  • Your orderId's shouldn't be random numbers. They have to be increasing numbers all the time. last id used +1 is the best algorithm. Duplicate order id can actually mean "not an increasing order Id".

    Note that when the socket connection is started, IB returns the next valid id in the nextValidId callback, so you always know which number to start on.

    Some people (me included) use number ranges for certain requests so that errors with an id can be associated with the request type. eg. I use numbers < 1000 for reqMktData type requests. Once you call placeOrder with a number > 1000, IB will never let you use a lower number for orders.

    You can actually reset the orderId sequence, but I've never done that and won't until I run out of ints.