¿ªÔÆÌåÓý

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

A couple of questions regarding the C++ Tws api client


 

Dear community,
i have been working with the tws api client (c++) for more than two weeks now. Even after carefully studying the code provided by Interactive Brokers i couldn't manage to find a suitable closing function for the opended Orders. So my first question is:
- How is that done properly ? Shall i create a new order to "neutralize" the old one ? For example "Long apple stock with? quantity 3" shall be neutralized with "Sell apple stock with quantity3" . IS this always matched correctly by TWS ?
The second one is, why are there "api orders", what is that and why it is different from active orders . This is confusing and i couldn't find an explanation in the documentation.
The third and last (for now) is, can it be guaranteed, that for every symbole there is a history of 1 minute candles that at least is, 2-3 weeks in duration ?

Thanks in advance,

Arthur


 

¿ªÔÆÌåÓý

Sounds like you might not have found the documentation, which should be able to help with many things:


Malcolm

On 28 Jul 2023, at 11:54, aSchwenk64@... wrote:

Dear community,
i have been working with the tws api client (c++) for more than two weeks now. Even after carefully studying the code provided by Interactive Brokers i couldn't manage to find a suitable closing function for the opended Orders. So my first question is:
- How is that done properly ? Shall i create a new order to "neutralize" the old one ? For example "Long apple stock with? quantity 3" shall be neutralized with "Sell apple stock with quantity3" . IS this always matched correctly by TWS ?
The second one is, why are there "api orders", what is that and why it is different from active orders . This is confusing and i couldn't find an explanation in the documentation.
The third and last (for now) is, can it be guaranteed, that for every symbole there is a history of 1 minute candles that at least is, 2-3 weeks in duration ?

Thanks in advance,

Arthur


 

Hello Malcolm,

as a matter of fact i work with the documentation and i'm well aware of the cancel_order function. If my order is filled i get an error that it's not possible to cancel an order that's already filled. Due to this, i suppose that canceling orders is when they are not active. And this is the confusing part. So again my question: What is the proper way of closing an order ? Shall i save the quantity, direction (Buy / Sell) and "construct" an order to "neutralize" the existing one or is there a simpler approach to this ?

regards,
Arthur


 

¿ªÔÆÌåÓý

Hi Arthur

Maybe I¡¯m misunderstanding your question, but a filled order does not need to be closed. As far is IB are concerned, a filled order is history.

Malcolm


On 28 Jul 2023, at 16:38, aSchwenk64@... wrote:

Hello Malcolm,

as a matter of fact i work with the documentation and i'm well aware of the cancel_order function. If my order is filled i get an error that it's not possible to cancel an order that's already filled. Due to this, i suppose that canceling orders is when they are not active. And this is the confusing part. So again my question: What is the proper way of closing an order ? Shall i save the quantity, direction (Buy / Sell) and "construct" an order to "neutralize" the existing one or is there a simpler approach to this ?

regards,
Arthur


 

Hi,

i place an order and i get the following:

ExecDetails. ReqId: -1 - EUR, CASH, USD - 000132b0.64c2e326.01.01, 9, 20000, 20000, 2
OrderStatus. Id: 9, Status: Filled, Filled: 20000, Remaining: 0, AvgFillPrice: 1.10343, PermId: 1476143489, LastFillPrice: 1.10343, ClientId: 0, WhyHeld: , MktCapPrice: 0

This states (i'm not mistaken) that the sent order was executed. If i check the tws server there is a position with -20.000 (as it was a sell order).

Then i try to close it by executing ->? m_pClient->cancelOrder(m_orderId, "");
It states: Error. Id: 9, Code: 10147, Msg: OrderId 9 that needs to be cancelled is not found.

As i see it, the order is still open. If i send a buy order with +20.000 in quantity i can close it.

Cancel implies for me, that if the order isn't executed i can cancel it but not to close an opened order

regards,




 

¿ªÔÆÌåÓý

This is a problem with nomenclature. Your order is not open, but you have an open position.

You were correct in your first email. To close the position just do the opposite in a new order.

Malcolm


On 28 Jul 2023, at 17:08, aSchwenk64@... wrote:

Hi,

i place an order and i get the following:

ExecDetails. ReqId: -1 - EUR, CASH, USD - 000132b0.64c2e326.01.01, 9, 20000, 20000, 2
OrderStatus. Id: 9, Status: Filled, Filled: 20000, Remaining: 0, AvgFillPrice: 1.10343, PermId: 1476143489, LastFillPrice: 1.10343, ClientId: 0, WhyHeld: , MktCapPrice: 0

This states (i'm not mistaken) that the sent order was executed. If i check the tws server there is a position with -20.000 (as it was a sell order).

Then i try to close it by executing ->? m_pClient->cancelOrder(m_orderId, "");
It states: Error. Id: 9, Code: 10147, Msg: OrderId 9 that needs to be cancelled is not found.

As i see it, the order is still open. If i send a buy order with +20.000 in quantity i can close it.

Cancel implies for me, that if the order isn't executed i can cancel it but not to close an opened order

regards,





 

On Fri, Jul 28, 2023 at 03:10 PM, Malcolm Haylock wrote:

This is a problem with nomenclature

There's a lot of lingo to get used to, it's all part of the fun!

As Malcolm mentioned... you don't close an , you close a position. A is established once an order is filled.

-Good luck


 

Thank you for clarifying this.

The second question is, why there is such a thing like api orders ?

As i see it all the provided callbacks can't see sent orders that are listed in the api tab. if i set the transmit-flag to false in the order object the order is stored in the api tab but i cant "reach" them anymore (and i already looked in the documentation).

BTW. Are you a developer at IB ? I found an error in the linux version of the client and emailed it to feedback@... but didn't get any reply. I post the mail here if this is of any use.


 

On Fri, Jul 28, 2023 at 03:18 PM, aSchwenk64@... wrote:

Thank you for clarifying this.

You're welcome.

The second question is, why there is such a thing like api orders ?

The short answer is: so that you can automate your trading and make or lose money in quantities and time-frames never imagined before :-) But, I suspect the crux of your question may be getting lost in translation. So, apologies in advance if my answer sounds flippant.

As i see it all the provided callbacks can't see sent orders that are listed in the api tab. if i set the transmit-flag to false in the order object the order is stored in the api tab but i cant "reach" them anymore (and i already looked in the documentation).

You've jammed quite a bit into this so I'll only address what stands out to me which is that you may have neglected to set and use a .

BTW. Are you a developer at IB ? I found an error in the linux version of the client and emailed it to feedback@... but didn't get any reply. I post the mail here if this is of any use.

No, but sometimes I wish I were so I could help them avoid some of their questionable design decisions and fix bugs they're "too busy for", lol. Seriously though... if you really think you've found a bug, emailing the feedback address is not be the best course of action.

I'd suggest that you first establish, beyond doubt, that it is a bug by bringing the issue up to their support team. More often than not they'll help address the misunderstanding and then triage the bug, internally, if it really exists. If you visit you'll find helpful resources along with an option, further down the page, to report problems and bugs.

-Good luck


 

I try my approach with a set Master Client ID. This sounds interesting.

Concerning the bug: If you try to compile the code under linux, you will get errors because the attached libbid.a and so are in Windows format . One need to replace it with the linux version.


 

On Fri, Jul 28, 2023 at 04:22 PM, aSchwenk64@... wrote:

If you try to compile the code under linux, you will get errors

That's been brought up before here.


 

You can skip the first few messages of that thread actually and just start reading from the one numbered 51303.

-Good luck


 

Yes this is exactly what i encountered. I got the client build but i dont want to use any workarounds like this if my money is on the line, therefore i would like some reply from officials at IB that confirm that this is the way to go : )


 

It sounds like you are struggling with some of the concepts, Arthur. Such as how Orders may have Executions that become Trades and change the Account Position. If you have not done so yet, fire up TWS, do some manual trading and look at the various windows (orders, trades, account). Every major function in TWS has a corresponding set of requests and responses within TWS API.

TWS API is very flexible and you could envision a complex scenario where orders are placed by several clients and, unless you use IBGW for the connection with IBKR, manually through TWS. In order ro manage that correctly:
  • "API Orders" are those create automatically by one of your client apps via TWS API as opposed to orders created anually in TWS
  • If you have one client with clientId 0, it can see and manage orders that were placed manually in TWS
  • buddy pointed you to the Master Client, which can see all orders placed by other clients. You pick and configure the clientId within the API settings in TWS/IBGW
  • You can set the Master Client ID to "0" and give that client visibility to and manageability of orders placed by all clients and TWS

The and sections have more detail.

The C++ compile issue you are experiencing is going on for a while (since the introduction of the Decimal data type during the transition of V9 to V10) and you can find many posts about this in our forum.

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


On Fri, Jul 28, 2023 at 11:22 AM, <aSchwenk64@...> wrote:

I try my approach with a set Master Client ID. This sounds interesting.

Concerning the bug: If you try to compile the code under linux, you will get errors because the attached libbid.a and so are in Windows format . One need to replace it with the linux version.


 

On Fri, Jul 28, 2023 at 04:50 PM, aSchwenk64@... wrote:

i would like some reply from officials at IB that confirm that this is the way to go : )

I don't blame you one bit! That said... I'd like the tooth-fairy to come and leave gold bullion under my pillow. In other words, IBKR will get around to fixing the problems they value, which may not necessarily coincide perfectly with the ones their individual customers value. Hey, it's a developer's God given right to complain sometimes, no?


 

Thank you guys for all the replies and help sofar. @´³¨¹°ù²µ±ð²Ô Struggling is indeed the case, as the complexity of tws itself and the api client is quite big. I do the trading and programming in parallel for the particular reason you mentioned. As coming from the MetaTrader 4 i find some functionality very different in tws.


 

Arthur,
are you perhaps coming from a trading environment where a hedging position accounting system is being used (e.g. MetaTrader)? In those systems you have to "neutralize" a previous order. Your IB account does not work like that. Instead it is a netting position accounting system. For each contract (stock, futures, options, etc) you can have only one position at any moment in time. The result is that any past order which (partially) executed resulted in you have a new position size (i.e your new net position size). Past, executed, orders are therefore considered not relevant and not kept or maintained.
Some of the basic differences between a hedging and netting account are described here:? https://www.mql5.com/en/articles/2299


Arthur
 

I will keep that difference in mind and have i look at the article? on mql5