¿ªÔÆÌåÓý

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

Re: Order Id in error messages


 

Well, your characterization is not correct. The id parameter for callbacks always identifies the message the error relates to:
  • If the error relates to a request, id will have the numerical value of that requestId
  • If the error relates to an order, id will have the numerical value of the orderId
  • if the error is not directly related to a message your client sent (such as global errors), id will have a value of -1.

Between requestIds and orderIds, numeric values for orderIds have to follow strict requirements possibly for very long periods of time (as in forever or until you reset the sequence in TWS/IBGW) while requestIds are ephemeral and can be reused as soon as the last request they have been used for is complete.

Therefore, you simple design a numeric assignment strategy for your client that makes sure that, at any point in time, the id value of an error callback can be uniquely related to a recent request or an order. There are may ways you can do that, but a simple approach, that worked well for us for years is this:
  • When your client connects, it receives the numeric value for the next valid orderId through the nextValidId() callback. For clientIds that have never been used before, nextValidId will return a value of 1
  • Memorize that value as the nextOrderId and increment it every time you need to assign an orderId
  • Similarly, create a nextRequestId that you use to assign unique ids for your requests from. Assign the initial value with a large offset from nextValidId(), such as, nextRequestId = nextOrderId + 10_000_000

Your error callback can uniquely be related to requests or order.

´³¨¹°ù²µ±ð²Ô



On Mon, Oct 30, 2023 at 05:19 AM, bespalex wrote:
This one thing has almost blown my mind recently, so I guess the 'discovery' may be helpful for others.
Normally, when you handle errors, you would expect to get an order id the callback, but it actually does not work this way.
You only get the order id as a reqId if the last thing you did was placing an order, but if you requested something else requiring reqId, like reqPnLSingle for example, you would be getting this reqId back in the errors callback.
Yes, I know this is mentioned in the documentation, but still come to think about it, this can really mess up your algo, if you are not careful enough!

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