Keyboard Shortcuts
Likes
Search
Duplicate OrderID
IB doesn't cease to amuse. I am baffled.
Yesterday, the last executed orderId was 161178. Today,??ibapi.wrapper: ANSWER nextValidId {'orderId': 161044} No need to say, this caused a major headache today, as orderIds eventually started to coincide with already executed yesterday's ones that are still in my open position. How is this possible? What can be done to prevent this? |
¿ªÔÆÌåÓýFirst of all this is really weird case. I never seen this type of issue. Are you waiting for next valid order id event before starting with other things? ? But if you really want to Hack it then use Different Client ID every day, so that they can never collide for a week. ? From: [email protected] <[email protected]> On Behalf Of bespalex
Sent: Wednesday, November 29, 2023 12:02 AM To: [email protected] Subject: [TWS API] Duplicate OrderID ? IB doesn't cease to amuse. I am baffled. |
We don't have enough detail about your setup to give you a definitive answer, but my best guess is that your client does not properly manage the next valid orderId. It is not sufficient that you simply start at the "nextValidId" you receive during connection and increment from there. That is necessary but not sufficient. Take another look at the "", especially the second paragraph where the other (and often overlooked) condition is described as:
When your client connects to TWS/IBKR, TWS API sends you the next valid orderId based upon the client ID you use. If that clientId has previously been used (does not matter how long ago), nextValidId will be one larger than the orderId of the last order that clientId has placed (whenever that was). If the clientId has never been used or no orders have been placed with that clientId, nextValidId will likely be 1. You can now place orders by keeping an internal nextOrderId that you increment for each order you place. Until you receive an openOrder or orderStatus callback for an order that was placed by a different client or manually in TWS. At that point in time you have to set your internal nextOrderId counter to:
So your next orderId must be higher than the id for any order your client has placed or has been related to. ´³¨¹°ù²µ±ð²Ô On Tue, Nov 28, 2023 at 10:02 AM, bespalex wrote: IB doesn't cease to amuse. I am baffled. |
¿ªÔÆÌåÓýThe latest order ids for each client id are stored, on the client computer, in the xml settings files for TWS and Gateway For example my tws.xml has this: ? ??????????? <MapOfStrings varName="ddeIdMap"> ????????????????? <Entry key="123" val="2"/> ????????????????? <Entry key="906564398" val="268435468"/> ????????????????? <Entry key="4739156" val="268435456"/> ????????????????? <Entry key="69744289" val="268565542"/> ????????????????? <Entry key="49723415" val="268575547"/> ????????????????? <Entry key="11344589" val="268435456"/> ????????????????? <Entry key="938945948" val="268435462"/> ????????????????? <Entry key="537090480" val="268435472"/> ??????????? </MapOfStrings> ? I placed an order using clientId 49723415 a few minutes ago, and you can see that the order id was 268575547. ? So when an API connection is made, the value for the chosen clientId is retrieved from the settings file and incremented, and sent via the nextValidId() callback. ? But the thing is, my recent order id is only visible because the settings file has been saved since the order was placed: I saved it manually using the ¡®File | Layout Settings | Save settings¡¯ menu entry. ? Thus the question is, when is this file actually saved? Certainly it is saved when you do a tidy exit from TWS/Gateway but certainly not immediately after the placeOrder() call. (The settings file is also uploaded to the IB servers at this point, unless otherwise configured) ? So there is a period, and it may be long, after an order is filled, where closing TWS untidily might mean that the settings file doesn¡¯t get saved. And so next time you connect to the API, the nextValidId() will be incorrect. Killing the process rather than using the normal exit method would do it. ? Of course one doesn¡¯t normally close down TWS/Gateway in this undisciplined fashion (I hope!), but I¡¯ve hit this problem a couple of times with the paper-trading TWS due to carelessness, power failures etc. My protection when connecting is to round up the nextValidId() to a number that couldn¡¯t conceivably have been reached in the previous session: I add 10000 to it, because I¡¯m very certain I will never exceed 10000 orders in a single day. ? By the way, the settings file also contains a mass of other stuff, for example layouts, open charts etc, and if the program is shut down untidily any such changes during the session are lost. That¡¯s why IBC can be configured to automatically save the settings on a user-specified schedule. ? Richard ? ? From: [email protected] <[email protected]> On Behalf Of bespalex
Sent: Tuesday, November 28, 2023 4:02 PM To: [email protected] Subject: [TWS API] Duplicate OrderID ? IB doesn't cease to amuse. I am baffled. |
My humble solution for this problem of duplicate order Id: First, I request all the open positions (client id = 0) and on the "openorder" callback: ? ? If orderId >= m_orderId Then ? ? ? ? m_orderId = orderId ? ? End If If the number of the order (still open) is greater than the nextvalidId number I got at the beginning then I update it. This way the next increment is a number which won't collide with another order on the system.? El mar, 28 nov 2023 a las 19:45, bespalex (<bespalex@...>) escribi¨®: Jurgen, I will perform this check from now on. |
joanmarcel119 wrote: "If the number of the order (still open) is greater than the nextvalidId number I got at the beginning then I update it. This way the next increment is a number which won't collide with another order on the system. " I don't think that this is a foolproof solution. You only check for the highest order ID for the currently open orders. But you neglect to check for the order IDs for the orders that have already filled. It could be that an order (having order ID 120) you placed yesterday is still open, while a next order (e.g. order ID 123), placed today, has already filled.
|
Yes I know but this repeated order number won't collide with the already executed order because?it's gone El s¨¢b, 2 dic 2023 a las 8:19, J G via (<windmill_1965=[email protected]>) escribi¨®: joanmarcel119 wrote: "If the number of the order (still open) is greater than the nextvalidId number I got at the beginning then I update it. This way the next increment is a number which won't collide with another order on the system. " I don't think that this is a foolproof solution. You only check for the highest order ID for the currently open orders. But you neglect to check for the order IDs for the orders that have already filled. It could be that an order (having order ID 120) you placed yesterday is still open, while a next order (e.g. order ID 123), placed today, has already filled. |