Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
Re: ISSUES WITH SMOOTH FAILOVER IN TWS APPLICATION - IBKR SERVER CONECTION DROP.
Richard nicely described how you'd use the various order and trade related callbacks to manage your orders, and there is really nothing for me to add there. Orders and trades do not happen in the vacuum and they are always related to an Account and a Position. TWS API has a rich set of requests, subscriptions, and callbacks that keep your client informed about those as well. Every trade changes the position so that you have a "second opinion" of the exact impact of an order (or a series of orders or modifications). You can simply compare the cumulative effect of a series of execDetails() callbacks with the change of the position over the same time frame. Below a very simplified data model of how they fit together. 闯ü谤驳别苍 PS. Generally one Trade will receive one TradeReport. But for completeness the model indicates there may be more than one. In rare situations (and I never had that happen, but it may), TWS API clients may receive one or more TradeReport corrections through execDetails callbacks later in time. The documentation says:
? ?
?
?
?
On Fri, Feb 14, 2025 at 01:01 PM, Richard L King wrote:
|
Re: ISSUES WITH SMOOTH FAILOVER IN TWS APPLICATION - IBKR SERVER CONECTION DROP.
开云体育Rather than comment on the questions you’ve raised, what I’ll do here is just lay out the basic principles that underly the order management component of my trading platform, which I have honed over the past 22 years. I believe it is immune to all the situations you describe. ?
?
?
?
?
?
?
? I think that more or less covers it, though there is a lot of detail I’ve glossed over. ? Sounds like a lot of work? It is!... ? ? Richard ? |
ISSUES WITH SMOOTH FAILOVER IN TWS APPLICATION - IBKR SERVER CONECTION DROP.
ALERT LONG MESSAGE.
Hello Folks, I am working on implementing a smooth failover mechanism to ensure the integrity of the code whenever there is a connection drop between the TWS application and the IBKR server. Request you to provide valuable insights on below scenarios. Case 1:
Despite the connection drop, the order was successfully modified at the exchange. Scenario During Connection Loss
Problem with Execution Callback Data
Example to Illustrate the Issue
This results in an inconsistency in tracking the order status and execution details. Currently, each time the TWS API receives an execution update, we calculate the remaining order size as follows: remaining_order_size = remaining_order_size - current_execution_size; if (remaining_order_size <= 0) { ????std::cout << "Order is fully executed"; ????// Remove order from metadata maps } if(remaining_order_size<0) remaining_order_size = 0; To ensure proper synchronization, we need to pass the remaining order size to the Execution listeners. However, in the above logic, as soon as remaining_order_size reaches zero or a negative value, the order is immediately removed from the metadata maps. To prevent premature removal and ensure consistency, I have implemented a safeguard by updating the order size at the time of order submission. This adjustment helps maintain synchronization and prevents potential discrepancies. I have identified a potential issue with updating the order size at the time of order submission. Consider the following scenario:
To address this issue, we can retrieve CompletedOrders from TWS. Any other method to handle above scenarios.? Thanks for you patience :) |
Re: TWS & IBKR CONNECTION STATUS.
Hello Richard,
First of all, thank you for the detailed information. To answer your question—if such a mechanism existed, I would use it to prevent any order requests (new, modify, or cancel) from being sent while the connection is down. During the 45-second interval before the disconnect callback is received, multiple requests could be placed. Once the connection is restored, I want TWS to discard all order requests made during the disconnection period, ensuring they are not sent to the IBKR server. However, any pending orders that were already active on the exchange should remain unaffected. In essence, TWS should flush only the unsent requests while maintaining all existing orders. Additionally, I have another question regarding the reliability of the connection loss/recovery callbacks. Is it guaranteed that the following callbacks will always be triggered in the event of a connection loss and subsequent restoration?
I ask because, in some cases, I have manually disconnected the internet, yet I did not receive these callbacks. Could you clarify if there are scenarios where these messages might not be triggered? Looking forward to your insights. |
Re: TWS & IBKR CONNECTION STATUS.
开云体育No, there is no way for either of these things. ? The TCP protocol suite contains a variety of mechanisms to automatically detect and recover from such incidents. But what it doesn’t have is a mechanism whereby an unexpected incident (eg spade through a cable, plug pulled from patch panel) results in some kind of notification being sent to the entities at both ends of the connection. It’s easy to imagine such a thing, but the practicality of doing it is close to zero, especially if you take into account all the timeouts, retries, resend requests etc involved in the automatic error detection and recovery machinery (not to mention the considerable range of different technologies that may be ?involved in what appears to be a simple internet connection). Also it would not be very helpful. Do you really want your application to be notified of every event that causes data to find a different route through the internet? All such mechanisms take time, not least because it takes time for them to even suspect that something might be wrong somewhere. ? Although the TCP/IP stack makes great efforts to ensure that what goes in one end comes out at the other end in the same order, it inevitably incurs delays when something nasty happens somewhere in the middle. And if the recovery efforts are successful, you really don’t want the application entities to be told about this at all. ? It’s worth noting that the API connection to TWS uses TCP Keepalives to ensure that failures are notified to the application fairly quickly. Presumably they are also used for the TWS to IBKR server connection. Without this optional TCP extension, an application only knows an error has occurred next time it tries to write to the connection (reads don’t generate an error if there is nothing to read). But the TCP Keepalives helps this by sending small packets periodically and checking for receipt of acknowledgment packets: if these acknowledgements are not received (after a suitable number of retries etc) then a fault is notified on the read. ? As an example, a TWS API client application connection might not send anything for hours: for example, if the application is just receiving market data there is no reason to send anything. Without the TCP Keepalives, it would never know if there were a network fault, or if TWS froze and stopped sending data. ? The parameters used for Keepalives for the TWS API connection (see the SetKeepAlive method in EClientSocket.cs) use an initial inactivity period of 20 seconds, followed by up to 10 repeats of the Keepalive packet at 100ms intervals after which an error occurs, so you can see that an error on this connection can result in a wait of up to 21 seconds before ?it is notified to the application. ? Presumably the connection from TWS to the IBKR server uses Keepalives with longer parameters to give the 45 seconds you’ve observed. ? But for neither connection can you specify the Keepalive parameters. ? Thus there’s no way to get an instant error callback. What would you even do differently if there were such a thing? You just need to know when it’s possible for you to start sending and receiving data again, and 1102 gives you this. ? As for as the second part of your question is concerned, I’m not even sure what you have in mind. When you get an error 1100, you must not send any further API requests until you’ve had the 1102. There could be one or more requests that you sent before the 1100 which you don’t know whether they have been processed. When you get the 1102, you can determine the state of PlaceOrder requests by use of ReqOpenOrders and ReqExecutions. But for things like market data or historical data requests you’re pretty much on your own. Personally I maintain a ‘ticker table’ where I record that data has been received, and after an 1102 I cancel and then reissue the market data request for any entry that is not marked as having received data. ? I hope at least some of this makes sense!... ? Richard |
TWS & IBKR CONNECTION STATUS.
Hello Folks,
Is there a mechanism to receive an instant callback when the connection between the TWS application and the IBKR server is lost, specifically due to an internet disruption? Currently, TWS provides this information via the following callback: void error( With the response: However, this callback takes approximately 45 to 60 seconds to trigger. I'm looking for a way to detect the disconnection immediately. Additionally, is there a method to cancel all pending requests that the TWS application attempts to send to the IBKR server once the connection is restored? Currently, reconnection is indicated by: ?
Thanks. |
Re: 10326 - OCA group revision is not allowed
Today,?OcaType 2 works with both 10.30 and 10.34, at least in a paper trading account.? I will try it in a live account with?version 10.30 this evening, and if it works, in a live account during the day tomorrow. On Tue, Feb 11, 2025 at 2:22?PM Jimmy via <jimmydtalbot=[email protected]> wrote:
|
Re: 10326 - OCA group revision is not allowed
If one order gets 1 contract filled, I don't want the other orders in the OCA group to be cancelled immediately; I want the other orders to remain active, with 1 contract to get subtracted from their?quantity.? Only when an entire order in the?OCA group is filled do I want the other orders to be cancelled.? This is the "reduce" behavior I believe (at least it was until now). I could live with OcaType 2 or 3, but right now, as far as I can tell, neither works with TWS 10.30+. On Tue, Feb 11, 2025 at 12:42?PM 闯ü谤驳别苍 Reinold via <TwsApiOnGroupsIo=[email protected]> wrote:
|
Re: 10326 - OCA group revision is not allowed
Thanks for the clarification, Jimmy. I had responded to when you had said in an earlier reply "I see this message when trying to modify the limit or stop price of an OCA order" What are you trying to achieve with the OCA order? Do you want one and only one of those two orders to fill? If that is the case, wouldn't OCA Type 1 "CancelWithBlocking" be the type you are looking for? Types 2 and 3 are "Reduce" types with and without blocking. 闯ü谤驳别苍 ? On Tue, Feb 11, 2025 at 11:18 AM, Jimmy wrote:
|
Re: 10326 - OCA group revision is not allowed
Thanks for the reply 闯ü谤驳别苍.
?
At this time, I'm not modifying orders. I am just submitting two single-legged option OCA bracket orders, one with a stop price, one with a limit price.
?
I am testing using a paper trading account.
I unchecked the "bypass No Overfill Protection precaution for destinations where implied natively" checkbox in API -> precautions, but I can't get the dialog to come up again (I don't know if the behavior would be the same a live account).
?
I tested with both the current stable version (10.30.1t) and latest (10.34.1b); the results are the same
?
When submitting Option OCA orders with OcaType = 2 (which the precaution dialog implies is what is needed), they get rejected with "OCA handling method mismatch".
When using OcaType = 3, they get rejected with "Invalid parameter for OCA group for exchange SMART".
?
OcaType = 3 works fine in 10.19 and before (that's what I've always used). However, I tried OcaType = 2 with 10.19 in a live account yesterday, and I also got the same error message "Invalid parameter for OCA group for exchange SMART".
?
Is anyone else using OCA orders with options? Is it working for you in 10.30+? What OcaType are you using??
?
Thanks
Jimmy |
Re: 10326 - OCA group revision is not allowed
You can permanently enable/disable various order precautions in the global settings of TWS and IBGW under API --> Precautions. When you call placeOrder() to modify the OCA order, what kind of Order object do you use?
Also, do you modify OCA order before or after the parent order has been filled? 闯ü谤驳别苍 ? ?
On Mon, Feb 10, 2025 at 07:08 PM, Jimmy wrote:
|
Re: 10326 - OCA group revision is not allowed
Revisiting this as the deadline to upgrade to 10.30.1t looms.
?
Raoul, my OCA orders' parent id is 0 already, so unfortunately I think the issue I'm encountering is different than what your post suggests.
?
The first time I run 10.30.1t and submit (via the API) OCA orders, a dialog comes up that says:
?
Overfill Protection Precaution
Invalid parameter for OCA group for exchange SMART. Overfill protection is implied.
Do you want to apply the correct parameter?
?
I choose yes and check "Don't display this message again".
?
Afterwards, the OCA orders get submitted, but they get rejected with "OCA handling method mismatch".
?
I brought this up with IB in October, yet the issue persists in "stable" version 10.30.1t.
?
Has anyone here figured out a solution to this "OCA handling method mismatch"?
My previous tests showed this was working in 10.32, but I will try this again tomorrow to confirm (likely in the latest offline version 10.34.1a, since 10.32 is not available anymore).
?
Thanks! |
Re: Automatic bid or ask price change until funded
. I found the IB algo etc, but this seems to peg the order to the midpoint and changes the price based on market price changes. Within the api I'd like to give my order a start price and have the algo add say 0.1$ per second until the buy is filled. So regardless of market price changes in that one second. |
Re: TrailStopPrice is not correct in the OrderStatusEvent
开云体育Is this email intended for me? -- Secured with Tuta Mail: Feb 10, 2025, 01:23 by TwsApiOnGroupsIo@...:
|
Re: TrailStopPrice is not correct in the OrderStatusEvent
You might want to ask this question in the ib_sync/ib_async group. For IBKR provided TWS API implementations., openOrder() callbacks provide updated Order objects each time the trail order stop price has been adjusted. The "trailStopPrice" field of the Order object from the last openOrder() callback before the order executes shows the same price as TWS. 闯ü谤驳别苍 ? On Sun, Feb 9, 2025 at 09:34 AM, <hadry.hamza@...> wrote:
|
Automatic bid or ask price change until funded
开云体育Tws interactive brokers visual basic api question Is it possible within the IB Api (I use? the VB version) to use an order type that based on a user set bid ( or ask ) price, walks the price up ( or down ) in preset steps until the order is filled?
|
Re: ib market data. is it clean and any good alterantive sources?
开云体育I used DTN-IQ for many years.? It is very affordable, but it does not give historical BBO data, only trades...On 2/8/2025 3:17 PM, Dale Joyce via
groups.io wrote:
|
TrailStopPrice is not correct in the OrderStatusEvent
Hello, I'm trying to get the trailing trigger price for the filled trail order (not the execution price) for that I'm using the orderStatusEvent : but when checking the TrailStopPrice I don't see the same price as what I see in the TWS app: ? ? here is what I see in the TWS app 'the price = 364.38: ? ? Do you have any idea if this is a common issue here? and how can I get the correct information please? ? Thanks, ? |
Bug and fix in TWS python API 10.34 (unknown module "server_versions")
Hello all - I just wanted to share a bug and fix I found in the latest version. Using both the windows and mac/linux version 10.34 API, you will error out if you do:
?
from ibkr.client import *
?
for not being able to find the "server_versions" module. There's a typo in client.py. The fix is to go to line 138 of client .py and change
?
from server_versions import MIN_SERVER_VER_CURRENT_TIME_
?
to
?
from ibapi.server_versions import MIN_SERVER_VER_CURRENT_TIME_
|