Is there a way to sell specific shares, or "lots", through the TWS API, ideally using ib_async? #112
-
Hypothetically, I would like to specify a unique If this sort of functionality existed, it would make many tasks in automated trading simpler: It would make Schedule D (and 8949) tax prep trivial; it would provide the cost basis required by the specific-shares method (read lower tax bills); and it would allow multiple trading algorithms to be tested within the same account, which would simplify portfolio rebalancing, re-login complexity, calculation of taxable interest, and so on. But does the TWS API have this functionality or anything like it? Currently, I know of only two hacky workarounds, and neither really solves the problem: (1) For two consecutive orders, we could collect the (2) We could query Are there other ways of approaching this problem that I haven't considered here? All useful ideas would be appreciated, code-tested ideas especially so. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Thanks for all those details, but sadly, the answer is just: No. The IBKR APIs do not let you close specific instrument holdings at the API level because IBKR doesn't report historical holding details at all. Every position in your portfolio is only reported with an You can configure your entire account through the mobile app or web app to prefer to sell oldest first or newest first (and other combinations of internal lot details IBKR doesn't expose to clients), but I haven't tested if the API respects their tax lot harvesting metadata account settings or if it only works when manually trading through mobile/web/TWS. Good luck. We all eventually have to balance the reality of "IBKR has great access to markets and funding, but IBKR interfaces are a bit difficult and restrictive and rarely refactored or updated compared to what should actually be possible." |
Beta Was this translation helpful? Give feedback.
Thanks for all those details, but sadly, the answer is just: No.
The IBKR APIs do not let you close specific instrument holdings at the API level because IBKR doesn't report historical holding details at all.
Every position in your portfolio is only reported with an
avgPrice
(or is itavgCost
? multiple names get used for the same concept in different places) field which is updated every time new quantities are added to a position. IBKR APIs only let you close using position detail of(contract, quantity,)
and there's no place for "historical lot" since there's no way to view or address historical lots at all (or even would be useful: "attempt to close N quantity for C contract if N were a…