Keyboard Shortcuts
Likes
Search
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 |
||||||||
A reqId and an orderId are two quite different beasts and behave differently.
toggle quoted message
Show quoted text
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 |
||||||||
Yes very useful indeed |
||||||||
You are correct that there are a few responses that return no reqId and I agree with your assessment that those really don't need to be called several times in parallel. But the corresponding requests do not require reqId either. Here the request call signatures for the response example you gave:
´³¨¹°ù²µ±ð²Ô On Sun, Dec 25, 2022 at 06:39 PM, <hymagik@...> wrote:
|
||||||||
Where I am now confused is how to modify an existing order using permID and not orderID. I have been modifying orders within a running instance of TWS using an Order() object, eg to change a stoploss:
Where does permID get put in here? And once TWS is closed, all of this information becomes redundant (eg- "parentId" because this is the stop loss in an OCA Buy order) - so how do I modify an order now? (I use python). Thanks. |
||||||||
You probably might want to reread at least the section of the and make sure you understand the following concepts:
toggle quoted message
Show quoted text
Clients can request the ability to control existing or future open orders that were placed by the same clientId, all other clientIds, or even via the TWS GIU. This is done by "binding" those orders (that you you currently see with an orderId of 0) to an orderId the client can use. We find it most convenient to enable the "Use negative numbers ..." option since that nicely separates ids for orders placed by the client from "bind" orders that the client did not create but may wish to control. Order binding works even after TWS or your client restarts. You will receive openOrder callbacks with (negative) orderIds other than 0 after your client makes one or more of the open orders requests mentioned above. ´³¨¹°ù²µ±ð²Ô On Mon, Dec 26, 2022 at 03:27 AM, Sam Mills wrote:
|
||||||||
If nobody gives you exact answer, my suggestion is to learn and try. The API is pretty powerful. The price you pay is that you should do your own gradual experiments. ? Suggestion for learning it: #1 Become familiar with OrderID
In C++? exemple: m_pClient->placeOrder(m_orderId++, ContractSamples::USStock(), OrderSamples::LimitOrder("SELL", stringToDecimal("1"), 50)); I read IB intent for training , using the ¡°++¡± in m_orderId++ as a way to say ¡°be sure to never reuse same ID for next order, so here we force a new one, that can be as simple as +1 of previous¡± ? also C++: m_pClient->cancelOrder(m_orderId-1, ""); Same thing with ¡°-1¡± to be understood as meaning: ?¡°here we cancel last order, could be any other, but please be sure to use a freshly brewed orderId for next ±è±ô²¹³¦±ð°¿°ù»å±ð°ù(¡).¡± Java: IB exemple use a ¡°cancelID¡± which I don¡¯t know about but seems to me more an implementation method, I don't see what else it could be aside of the Orderid you use for the ±è±ô²¹³¦±ð°¿°ù»å±ð°ù(¡). ? #2 Become familiar with the Order object #3 Read again David Walker answer. Cheers ? |
||||||||
You cannot just with permId. You will always need an orderId. I repeat my recommendation that you study and specifically . ´³¨¹°ù²µ±ð²Ô On Mon, Dec 26, 2022 at 03:52 PM, Sam Mills wrote:
|
||||||||