IbcAlpha / IBC

Automation of Interactive Brokers TWS. You can download the latest release here: https://github.com/ibcalpha/ibc/releases/latest
GNU General Public License v3.0
974 stars 174 forks source link

Strange behaviour when using ConfigureAutoLogoffOrRestartTimeTask #258

Closed acrive82 closed 1 month ago

acrive82 commented 1 month ago

Background: I am using IBC directly from Java. I am using version 3.19.0 with IBGW 1029.

I am overriding the Settings class to enable an auto restart at a specified time (AutoRestartTime 03:00 PM). However, I am encountering the following issue in the log:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2024-05-24 16:58:13:603 IBC: could not find auto logoff/restart time controls
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Upon analyzing the code, I discovered that inside ConfigureAutoLogoffOrRestartTimeTask, when attempting to locate the radio button for Auto Logoff or Restart, the variable autoAction is null:

    JRadioButton autoAction = SwingUtils.findRadioButton(c, autoActionName);

When I attempt to do this manually, I observe the following: image instead this (Run directly, not by IBC): image

IBC does not modify the UI, so why is this happening? Does anyone know the cause? Any suggestion is appreciated!

In the meantime, I continue to investigate, and if I have any news, I will let you know.

Thanks!

rlktradewright commented 1 month ago

[I’m away from home at the moment. I do have access to my systems via a VPN tunnel, but only have a small screen so a bit limited in looking at the code. But I’ll try to check it out. If I can’t manage it, I’ll be back home on Wednesday 29 May so please be patient.]

I don’t quite understand what you mean by ‘when I do this manually’. Do you mean when you run Gateway directly, without IBC? Or something else?

Your screenshot certainly looks odd, as it doesn’t have the AutoRestart option. Have you checked whether there is a later version of Gateway 10.29? Maybe IBKR have messed up with the version you’ve installed, but ‘latest’ versions are updated frequently so they may have fixed this by now. I have 10.29.1g, and that certainly looks fine.

As you say, IBC doesn’t modify the UI, so this behaviour does appear to be a problem with Gateway itself, and not an IBC issue.

Richard

acrive82 commented 1 month ago

I don’t quite understand what you mean by ‘when I do this manually’. Do you mean when you run Gateway directly, without IBC? Or something else?

Exactly. Run the app by click on their icon. The same app that IBC use ~/Jts/...

rlktradewright commented 1 month ago

I think I've just realised what the cause of the problem is. In order to offer the auto-restart functionality, TWS has to know where its autorestart file is stored. This is done by passing a folder path as a parameter to the java.exe command. So in your case, this parameter will need to be passed to the command that starts your Java program, and which then loads IBC. I'm not in a position to check this out just now, but l'll try do it tomorrow - might be later in the day though. 

rlktradewright commented 1 month ago

After thinking about this a bit more, I don't think you'll be able to achieve what you want.

Here's what I think you're probably hoping for:

  1. You run your Java app.
  2. Your app starts IBC directly, with the settings injected using a class that implements the appropriate interface (I'm not looking at the IBC code here because I'm still away from home, so I can't remember exactly what that interface is called).
  3. IBC starts Gateway as normal.
  4. IBC reads the setting for the auto-restart time, and uses it to set the auto-restart time in the loack and exit section of the Gateway's configuration dialog.
  5. At the auto-restart time, your app keeps running but Gateway is magically restarted without requiring re-authentication.

Unfortunately this is not achievable. It is certainly possible to give Gateway the information it needs to offer the auto-restart time field in the config dialog: this just needs an extra parameter on your java.exe command like this:

-DjtsConfigDir=<gateway-settings-folder>

However when the auto-restart time arrives, Gateway actually exits completely, and this will take IBC and your program with it. This means that you have to start your program again, this time with yet another parameter to the java.exe command program, like this:

-Drestart=<name-of-folder-containing-file-called-autorestart>

This folder is a subfolder of the jtsConfigDir, and has a long name (specific to your IBKR user id) similar to this: cefiodafddealakeckhkohbbaenmdfncgjgmpgpb

Now, this is all quite do-able (it's what the IBC start scripts do). But is it what you want? Here are the downsides:

  1. Extra complexity in your app to do the restart stuff that the IBC scripts do for you.
  2. Your app won't get any notification that Gateway is about to shut down, so it needs to be prepared to cope with this (but I expect that there is means to define a listener that will receive such a notification from the Java runtime)
  3. Your app will be limited to the particular version of Java that IBKR supports for Gateway, thus ruling out all the Java goodness introduced since Java 8.

So I wonder whether it's really worth trying to run IBC/Gateway directly from your Java program? Probably a lot easier to just use the scripts.

acrive82 commented 1 month ago

Thank you very much for your help and suggestions. I’m in a situation where it is more practical and beneficial to run IBC and GW/TWS programmatically. I have a lot to do before starting IB. In my case, scheduling my app as a service with cron is the solution.