¿ªÔÆÌåÓý

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

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.

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


 

Thank you Dave, very useful reply.


 

Yes very useful indeed
Just a grain of salt about request ID: Some request (just a few) does use a request ID but do not send it in the call back.
like ? that deliver back mktDepthExchanges(const std::vector<DepthMktDataDescription> &depthMktDataDescriptions)? without reqID.

In never saw this as an issue because these kinds of requests are far less critical, they are either:
- part of what should be called only once on init. (and are serviced back very quickly)
- are far less critical, the only one I take some care about is reqAccountUpdates (if you use it, this is not so critical as it's serviced rather quickly)
- does return OrderId (or Order.orderId) which is enough to collate results (but still do not deliver the reqId that trigger the call);



 

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:

C++ EClient.h void reqMktDepthExchanges();
C# EClient.cs public void reqMktDepthExchanges()
Java EClient.java public synchronized void reqMktDepthExchanges() {
Python client.py def reqMktDepthExchanges(self):

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


On Sun, Dec 25, 2022 at 06:39 PM, <hymagik@...> wrote:

Yes very useful indeed
Just a grain of salt about request ID: Some request (just a few) does use a request ID but do not send it in the call back.
like ? that deliver back mktDepthExchanges(const std::vector<DepthMktDataDescription> &depthMktDataDescriptions)? without reqID.

In never saw this as an issue because these kinds of requests are far less critical, they are either:
- part of what should be called only once on init. (and are serviced back very quickly)
- are far less critical, the only one I take some care about is reqAccountUpdates (if you use it, this is not so critical as it's serviced rather quickly)
- does return OrderId (or Order.orderId) which is enough to collate results (but still do not deliver the reqId that trigger the call);



 

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:

stopLoss = Order()
stopLoss.orderId = 99
stopLoss.action = "SELL"
stopLoss.orderType = "STP"
stopLoss.auxPrice = 100.01
stopLoss.tif = "GTC"
stopLoss.totalQuantity = 1
stopLoss.parentId = 98                  # Parent ID
stopLoss.transmit = True
stopLoss.eTradeOnly = ""
stopLoss.firmQuoteOnly = ""
self.placeOrder(99, IBContract, 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:
  • Client 0
  • Master Client - configured in the API section of TWS/IBGW global configuration.
    NOTE: Master Client can be set to "0" to make the client with id 0 a "super client" that has the "powers" of both the "Master Client" and "Client 0"
  • The API configuration "Use negative numbers to bind automatic orders" and the tooltip

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:

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:

stopLoss = Order()
stopLoss.orderId = 99
stopLoss.action = "SELL"
stopLoss.orderType = "STP"
stopLoss.auxPrice = 100.01
stopLoss.tif = "GTC"
stopLoss.totalQuantity = 1
stopLoss.parentId = 98                  # Parent ID
stopLoss.transmit = True
stopLoss.eTradeOnly = ""
stopLoss.firmQuoteOnly = ""
self.placeOrder(99, IBContract, 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.


 

If nobody gives you exact answer, my suggestion is to learn and try.
Most likely everyone has methods of their own for placing /managing orders? and may not be exactly as you asked for it.
So your relatively on your own to master that.
And it is not a waist of time to try and experiments.

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


Read carefully this one (twice)


Read it also with other language like Java or C++ aside of Python (your language). because this may help you getting a better understanding of how crude examples are :

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
This object is a major one and is returned once you ±è±ô²¹³¦±ð°¿°ù»å±ð°ù(¡­)? when call back fires up.
This bring to you the order.permId and order.orderRef


#3 Read again David Walker answer.

Cheers

?


 

Oups !
Looks like Jurgen Reinold did answered you while I was writing also an answer.
Sorry Jurgen


 

Hi, thank you for your replies, but I am still confused on what to do next with my code after reading all the posts. I dont understand what to do.

How do I send a new placeOrder() to amend an existing order using permID?


 

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:

How do I send a new placeOrder() to amend an existing order using permID?


 

I think I understand now. Thanks for your reply. I didnt know that orders need to be bound first and then mapped: