¿ªÔÆÌåÓý

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

How to handle if I sell stock from TWS while it was bought by API application ?


 

Hi all,

I have a question for a case which I need to handle. During my tests I have experienced the following

a. API application generates a signal to buy and place the order, I keep the inventory of orders(id, action, qty etc) and portfolio(stockId, bought, sold etc)
b. TWS transmits the order and order is executed, orderStatus, execDetails, commissionReport callbacks are called
c. API application waits for a sell signal to be generated
d. At some point I decide to sell stock manually and I close the position on TWS manually.

TWS shows the stock as sold afterwards but in this case, I see that
1. I receive orderStatus callbacks - but the returned orderId is not the ones that I keep so I cannot relate the orderStatus with the existing portfolio of stocks.
2. I receive NO execDetails for this manual order.
3. I receive commissionReport, but again no way to relate it with a stock in my portfolio of API application.?
4. API application generates the sell signal because it does not aware that stocks gone, and make a Short Sell unintentionally

What would be the way to handle manual sells from TWS while the API application is running ??

Thanks in advance.?


 

One way I deal with this scenario is using the call back function.? It will return 0 shares after you close the position in TWS.
-Steve


 

¿ªÔÆÌåÓý

I¡¯m familiar with this situation. BTW - working with other brokers we face the same problem - sending order from their GUI cannot be tracked in your application as you cannot bind it to your algorithm. The only way we deal with it - never send an order from their GUI. For the purpose of such situations we use our own GUI that send such order. Then you can trace it. This is not a perfect solution as sometimes (rarely but still) the broker can decide to close your position.

But in 14 years of work with IB it happened only a couple of times. So the own GUI solution is the best for us.

On 20 Sep 2022, at 19:24, Steve Tuscher <steve.tuscher@...> wrote:

One way I deal with this scenario is using the call back function.? It will return 0 shares after you close the position in TWS.
-Steve


 

Steve and Edward have pointed you in the right direction already. The only thing I'd like to add is that you can control how much information (if any) your client application receives about orders placed in TWS or by other client applications.

If you have not done so, take a look at the concepts of "client 0" and the "master client" in the section of the API Reference Guide. And since 0 is a legitimate value for the "Master Client ID" you can configure TWS/IBGW such that your client with an ID of 0 is both, a "client 0" and a "master client".

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

On Tue, Sep 20, 2022 at 11:32 AM, Edward wrote:

I¡¯m familiar with this situation. BTW - working with other brokers we face the same problem - sending order from their GUI cannot be tracked in your application as you cannot bind it to your algorithm. The only way we deal with it - never send an order from their GUI. For the purpose of such situations we use our own GUI that send such order. Then you can trace it. This is not a perfect solution as sometimes (rarely but still) the broker can decide to close your position.
?
But in 14 years of work with IB it happened only a couple of times. So the own GUI solution is the best for us.

On 20 Sep 2022, at 19:24, Steve Tuscher <steve.tuscher@...> wrote:

One way I deal with this scenario is using the call back function.? It will return 0 shares after you close the position in TWS.
-Steve


 

Thanks ´³¨¹°ù²µ±ð²Ô for taking my attention to MasterClient ID.

Actually I have realized that I have overlooked the documentation which states MasterClient ID is unset initially in TWS. I was using client Id 0 and was assuming it is the masterclient but not set in TWS. After setting Master Client ID () I do receive execDetails call backs for the sell executions for the orders which is directly placed from TWS GUI. I need to figure out the order action(Buy/Sell) in the application somehow but main issue was solved which was due to unset MasterClientID.?

Thanks Edward for confirming my idea to have a GUI for my application. Monitoring signals, portfolio and P&L on text files/db tables/excel files tiring after all but developing a GUI also needs some effort. I will see how it goes.

Thanks Steve for reminding positions call back. Let me ask, can I leave it open/subscribed and get position updates always? or should I periodically request reqPositions to get updates in timely manner ?
Actually I wonder if I start application and execute reqPositions and get all the position updates as long as the application runs or until I send cancelPositions? I'm a bit confused in documentation which says positionEnd will be triggered automatically. Is it only for the first request which means I need to request reqPositions twice ?

Many thanks for your replies.?





 

I only call reqPositions once after the TWS connection is made and the Positions callback function executes after every change of position.
-st


 

I guess the confusion may come from the fact that immediately sends you a snapshot of all current positions followed by a call of AND, as Steve described, continues to send you position updates for those and any future positions until you call .or your client exits.

It basically makes your client know exactly what TWS shows in the position window. That includes positions that recently were closed (e.g. have a quantity of 0) as long as TWS still shows them.

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



On Tue, Sep 20, 2022 at 04:11 PM, Steve Tuscher wrote:
I only call reqPositions once after the TWS connection is made and the Positions callback function executes after every change of position.
-st


 

Thanks, that might solve the issues.

I guess if I sell through TWS I will get QTY = 0 position update, if I buy I get the QTY!=0 position update, which solves the problem to understand if the order is Buy or Sell which is missing in orderStatus/execDetails callbacks.?

Thanks