1

Everytime before I place a new order to IB, I need to make a request to IB for next valid orderId and do Thread.Sleep(500) to sleep for 0.5 seconds and wait for IB API's callback function nextValidId to return the latest orderID. If I want to place multiple orders out, then I have to naively do thread.sleep multiple times, This is not a very good way to handle this, as the orderID could have been updated earlier and hence the new order could have been placed earlier. And what if the orderID takes longer time to update than thread sleep time, this would result in error.

Is there a more efficient and elegant way to do this ?

Ideally, I want the program to prevent running placeNewOrder until the latest available orderID is updated and notify the program to run placeNewOrder.

I do not know much about Java data synchronization but I reckon there might be a better solution using synchronized or wait-notify or locking or blocking.

my code:

// place first order
ib_client.reqIds(-1);
Thread.sleep(500);
int currentOrderId = ib_wrapper.getCurrentOrderId();
placeNewOrder(currentOrderId, orderDetails); // my order placement method 
// place 2nd order
ib_client.reqIds(-1);
Thread.sleep(500);
int currentOrderId = ib_wrapper.getCurrentOrderId();
placeNewOrder(currentOrderId, orderDetails); // my order placement method 

IB EWrapper:

public class EWrapperImpl implements EWrapper {
 ...
 protected int currentOrderId = -1;
 ...
 public int getCurrentOrderId() {
 return currentOrderId;
 }
 public void nextValidId(int orderId) {
 System.out.println("Next Valid Id: ["+orderId+"]");
 currentOrderId = orderId;
 }
 ...
}
asked Sep 13, 2021 at 0:29
2
  • So at some point in your program you've registered ib_wrapper to receive callbacks? Commented Sep 13, 2021 at 0:51
  • yes. Correct... Commented Sep 13, 2021 at 0:58

2 Answers 2

2

You never need to ask for id's. Just increment by one for every order.

When you first connect, nextValidId is the first or second message to be received, just keep track of the id and keep incrementing.

The only rules for orderId is to use an integer and always increment by some amount. This is per clientId so if you connect with a new clientId then the last orderId is something else.

I always use max(1000, nextValidId) to make sure my id's start at 1000 or more since I use <1000 for data requests. It just helps with errors that have ids.

You can also reset the sequence somehow.

https://interactivebrokers.github.io/tws-api/order_submission.html

This means that if there is a single client application submitting orders to an account, it does not have to obtain a new valid identifier every time it needs to submit a new order. It is enough to increase the last value received from the nextValidId method by one.

answered Sep 13, 2021 at 1:53
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @brian, Thanks for your answer. And yes I am aware of this solution. But is there a way to be notified before processing the next block of code ?
I'm not sure what you mean exactly. There are many ways but there is nothing to wait for. If you really want to wait for more ids, then just check for a next order in the nextValidId callback. You could queue the orders beforehand. This is something some people used to do as there is a 50 msg/sec sending limit. I think the newer APIs have a setting to automatically limit messages but I doubt many people need to worry about it.
0

You should not mess around with order ID, it's automatically tracked and being set by the API. Otherwise you will get the annoying "Duplicate order id" error 103. From ApiController class:

public void placeOrModifyOrder(Contract contract, final Order order, final IOrderHandler handler) {
 if (!checkConnection())
 return;
 // when placing new order, assign new order id
 if (order.orderId() == 0) {
 order.orderId( m_orderId++);
 if (handler != null) {
 m_orderHandlers.put( order.orderId(), handler);
 }
 }
 m_client.placeOrder( contract, order);
 sendEOM();
}
answered Sep 11, 2022 at 21:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.