¿ªÔÆÌåÓý

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

Re: Persistent reqId or orderID


 

A reqId and an orderId are two quite different beasts and behave differently.

reqIds are sent once to the API, and only ever once. They are used for 'requests' for data rather than for orders - i.e. to request Historical Data, or scanner data, or stream data etc. All data that is returned from each request will be stamped (in the eWrapper callback) with the reqId that you sent, so that you can match it up with your original request. You can't 'manage' a request by using the same reqId in a new command - you can only send a new request with a new reqId. reqIds do not persist between sessions.

An orderId on the other hand is used to uniquely identify an order (only). It is a parameter in the placeOrder(orderId, contract, order) function, as well as of the order object. The API will return the status of the placed order via the openOrder and orderStatus callbacks, and uniquely identify these callbacks using the orderId you submitted. However (this is important to note), these callbacks only occur while the order is still active (i.e. prior to completion/cancel). You can modify an open order by sending a placeOrder with the same orderId and a modified order object (i.e. modified to whatever you want to update the order to) - this of course will return an error if you try to modify an order that has already been completed/cancelled.

Once an order is completed, the user-supplied orderId disappears from the TWS API world - e.g. the completedOrder callback does not identify the order using the orderId - and you must use either the permId or the orderRef parameters of the returned order object to uniquely identify the completedOrder callbacks. The orderRef parameter you can set yourself when you first submit the order, but you will need to make sure it's unique; the permId is assigned by TWS after you place an order and can be obtained from an openOrder or orderStatus callback. OrderIds typically do not persist between sessions unless the order is sitting there unfilled (??and maybe not even then) - in any case, you should work with the permId parameter (or, if you wish, the orderRef parameter) instead.

Hope that helps.

Dave

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Sam Mills
Sent: Wednesday, 21 December 2022 7:31 PM
To: [email protected]
Subject: [TWS API] Persistent reqId or orderID

Hi, is there a way to manage trades placed when TWS is closed and then reopened?

I have been using reqId's to create OCA orders and then manage them whilst TWS is open. But when I close TWS and reopen TWS several days later, using the same reqId doesnt seem to work.

Is there a persistent reqId that persists in between sessions?

Thank you

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