Keyboard Shortcuts
Лайки
Пошук
Question on nextValidId function
Hi All, one question on the orderId and the necessity of nextValidId function. From the documentation (https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#receive-news-articles) it is stated that “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 ”. so I expect that the nextValidId function is needed in case of multiple clients for one account. However i have created a program in python that handles orderIds internally without relying on the nextvalidid function. By launching multiple instances of it (each with different client id) I see no problems in handling orders (each client autonomously updating its orderId). What am I missing? Is nextValidId really needed? thanks, Marco |
There is nothing wrong with your approach, as long as your internal counter gets initialized with the value of the automatic nextValidId() callback you receive when your client connects to TWS API. If your client gets "in contact" with orders from other clients, you need to adjust your interal counter so that it is higher than the orderId of all orders your client sees. That is mostly important for client 0 and the master client since they receive openOrder() callback for orders placed by other clients or TWS in real time, but all clients might need to do that in case they request all open orders, for example, and the list contains order from other clients. The simplest approach is that your openOrder() callback makes sure that your internal counter is always higher than all orderIds it sees. Jürgen On Fri, Dec 27, 2024 at 08:03 AM, Marco wrote:
|
Hello Jürgen, thanks for your detailed reply. Yes I confirm my counter(s) get initialized with the value from the automatic nextValidId() callback. It is not completely clear to me when you mention "in contact". Does it mean in case different clients can manage same order? if the clients are independently managing different orders I can have independent counters so in principle the same numerical orderId can be reused for different clients, am I correct? thanks Marco On Fri, Dec 27, 2024 at 3:04 PM Jürgen Reinold via groups.io <TwsApiOnGroupsIo=Reinold.org@groups.io> wrote:
|
You are correct, Marco. Each clientId has its own sequence of orderIds and, since nextValidId() has a value of 1 when a clientId is used for the first time, the numeric values for orderIds for different clientIds can (and will be) be identical at times. The technical requirement your client has to be aware of is that each placeOrder() call shall present an orderId that is higher than all orderIds the calling clientId has ever received in openOrder() or orderStatus() callbacks. As long as a client receives only openOrder() callbacks for orders it placed itself, keeping a counter that is initialized from nextValidId() at connection time is sufficient. When a client terminates, TWS/IBGW will remember the highest orderId used in placeOrder() calls and assures that nextValidId() returns a value higher than that when it connects again later. But there are cases where a client can receive openOrder() callbacks for orders placed by other clients (or TWS). In these cases, the client may need to bump the internal nextValidid counter accordingly. Let me quote directly from the TWS API documentation since it nicely describes this:
The second part of that statement is what you need to be aware of. Say your client needs to be know about orders for the instrument is manages that are open when it connects, a call to reqAllOpenOrders() might generate openOrder() callbacks with orderIds that are higher than the nextValidId(). In these cases, simply bump the internal counter to that orderId + 1. And to make this very simple and safe for all scenarios, just perform that test unconditionally for all openOrder() and orderStatus() callbacks regardless of why your client receives them. Sounds complicated, but is really not. There is also an API Global Configuration option called "Use negative numbers to bind automatic orders." Take a look at the tool-tip. Jürgen On Fri, Dec 27, 2024 at 09:36 AM, Marco wrote:
|
thanks again Jürgen, everything is clearer now, complicated as you said, but clearer :) BR, Marco On Sat, Dec 28, 2024 at 4:48 AM Jürgen Reinold via groups.io <TwsApiOnGroupsIo=Reinold.org@groups.io> wrote:
|
Повідомлення
Більше