¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: Question regarding reboot of trader workstation


 

Not sure what needs to be tested here since the documentation at is crystal clear.? Also not clear why you are still trying to force the API into some kind of a "server increments orderIds"? mode. That is not how it works and you just need to accept that.

For your convenience, below and annotated copy of the first paragraph of

Perhaps the most important event received after successfully connecting to the TWS is the [...] As its name indicates, the nextValidId event provides the next valid identifier needed to place an order. This identifier is nothing more than the next number in the sequence. 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. For example, if the valid identifier for your first API order is 1, the next valid identifier would be 2 and so on.

This is what you should do for now.

The situation may become a little more complicated as JG has suggested when orders can simultaneously be placed by multiple clients and TWS. Particularly if one of those clients is the "Master Client" (received order status for all orders placed by all clients) or "Client 0" (receives orders order status updates for all orders placed in TWS). The documentation is again crystal clear for that case:

However if there are multiple client applications connected to one account, it is necessary to use an order ID with new orders which is greater than all previous order IDs returned to the client application in openOrder or orderStatus callbacks.

In your TWS API adapter (the small layer of code that shields your business logic from any and all TWS API details), you'd provide a very simple nextOrderId() service that works as follows:
  1. The service keeps a private copy of the next valid orderId. Let's call that private variable nextOrderId
  2. During connect(), initialize nextOrderId with the value you receive from
  3. Whenever you receive an openOrder or orderStatus callback, extract orderId from the provided Order object and set nextOrderId = MAX( nextOrderId, orderId + 1 )
  4. Whenever your business logic calls nextOrderId(), return nextOrderId++.

You obviously want to put some synchronization in place to make sure increment and set operations are atomic.

The final piece of documentation says:

You can always use the method in the event that your client application loses track of the sequence.

I hope this settles it.
´³¨¹°ù²µ±ð²Ô


On Mon, Aug 7, 2023 at 08:18 AM, Arthur wrote:
@ ´³¨¹°ù²µ±ð²Ô
I had to read your last reply a couple of times due to its length and richness of detail.

The solution with nextRequestId() with nextValidId() + someLargeOffset where someLargeOffset > 4,320,000 sounds feasible to me. I'll give it a try.

From tests i did earlier today, it seems that the tws server doesn't increment the orderID by itself it only deliver the max that it received from N Client
at any particular point during the session ? Is this correct ?

Join [email protected] to automatically receive all group messages.