Is there a way for TWS to reconnect, once the APP disconnects? (2024)

C++_API
  • All Messages By This Member

#878



Example Scenario:

  1. TWS started by IBController, connected, running.
  2. Open the IBKR APP on a smart phone.
  3. IBKR APP Notifies - "Another session with this name is in use, Click OK to login and disconnect other session".
  4. Click OK on APP
  5. TWS dialog pop up with "connection lost, do you want to disconnect other user, etc".
  6. Make trades on APP
  7. Close APP

At this point TWS does NOT automatically regain connection and have to manually use the dialog (that popped up in step 5) to reconnect TWS.

Is there a way for TWS to reconnect automatically, once the APP disconnects?

Thank You.

J.G.

  • All Messages By This Member

#879


If you are using two factor authentication then IBController is not able to take care of the re-logon. If you have disabled 2FA, then IBController is able to take care of logging on the original TWS application.

Richard L King

  • Member Profile
  • All Messages By This Member

#880


I will be making a new release of IBC within a few days, which will provide some help here.

The new release will accept an additional value for the ExistingSessionDetectedAction setting in config.ini. This value is “primaryoverride” (though it’s possible I might change this value by the time I release, so you’ll have to make sure you read the release announcement carefully).

When ExistingSessionDetectedAction=primaryoverride, then when another login occurs (for the same username), IBC will automatically shut down TWS/Gateway (unless this other session is a result of running IBC with ExistingSessionDetectedAction=secondary, in which case the existing session will keep running).

Also if the username is already logged in elsewhere when you start IBC with ExistingSessionDetectedAction=primaryoverride, then that session will be logged out (unless this other session is a result of running IBC with ExistingSessionDetectedAction=primary, in which case the existing session will keep running).

So, if you run IBC with ExistingSessionDetectedAction=primaryoverride, and then later you logon with the same account via Client Portal or IBKR Mobile (with market data), that instance of IBC will shut down, and your IBKR Mobile session will proceed. So far so good.

In order to automatically re-establish your TWS session when you finish with Client Portal/IBKR Mobile, you need to arrange for IBC to be automatically restarted, again with ExistingSessionDetectedAction=primaryoverride, after a suitable time interval (say 10 minutes). Now IBC will cause the TWS login to proceed, and your market data on your Client Portal/IBKR Mobile session will be stopped. If you haven’t actually finished your work on Client Portal/IBKR Mobile, then you can simply request the market data again, and the TWS session will be terminated by IBC.

On Windows, it’s quite easy to set up a Scheduled Task to restart IBC regularly, but only allow the Task to run if there isn’t already a running instance. In fact a sample task scheduler definition file is included in the IBC download, which you can import into Task Scheduler and customise to suit your needs.

[On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, at least on Windows, this will achieve what you want, except for the fact that if you haven’t finished your Client Portal/IBKR Mobile the next time IBC is started, you’ll have to re-request market data. So it’s a bit clunky, but at present I can’t think of any better alternative. Also I’ve only tried this out with the Client Portal so far (and it works for the upcoming new version of IBC), but not yet with IBKR Mobile because I don’t have an Android or iOS smartphone (I’ll have to try it on my wife’s phone, if she’ll let me!).

Please note that this enhancement will only be available in IBC, not in IBController.

Watch this space!

Richard

toggle quoted messageShow quoted text

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of C++_API
Sent: 03 April 2019 05:00
To: ibcalpha@groups.io
Subject: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?


Example Scenario:

  1. TWS started by IBController, connected, running.
  2. Open the IBKR APP on a smart phone.
  3. IBKR APP Notifies - "Another session with this name is in use, Click OK to login and disconnect other session".
  4. Click OK on APP
  5. TWS dialog pop up with "connection lost, do you want to disconnect other user, etc".
  6. Make trades on APP
  7. Close APP

At this point TWS does NOT automatically regain connection and have to manually use the dialog (that popped up in step 5) to reconnect TWS.

Is there a way for TWS to reconnect automatically, once the APP disconnects?

Thank You.

jesse@abra.com

#881


Hi Richard. Thank you for this suite of applications! I'd just like to make some suggestions regarding only the following paragraph (because Linux is my bailiwick, but anything that this app actually exists to accomplish is most decidedly NOT. haha) No guarantee that these will work!

>> [On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, there's a couple ideas you and other folks might investigate using:

* Use Supervisor (http://supervisord.org/).

- If the event handlers built-in to Supervisor aren't enough, addhttps://pypi.org/project/superlance/to control supervisor-managed programs based on runtime events (based on things like CPU usage, memory usage, etc.)

* In crontab, something like:

0 10 * * * * pgrep -f <definitive process name> || <program name>

pgrep (for p)rocess grep) without -f just looks at the process name, but with -f looks at the full command line. In this example, if the process is *not* running, it would execute <program name>. If it is *already* running, it would do nothing.

Personally, I like the Supervisor model better, because it is well-written and handles it's domain very well. But, hey, the shell and XOR can Just Work (tm).

Hope that helps!

Cheers,

Jesse Adelman

Systems Engineer (DevOps/SRE)

Pittsburgh, PA (and remote to the world)

toggle quoted messageShow quoted text

----- Original message -----

From: Richard L King <rlking@...>

To: ibcalpha@groups.io

Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Date: Wednesday, April 03, 2019 11:16 AM

I will be making a new release of IBC within a few days, which will provide some help here.

The new release will accept an additional value for the ExistingSessionDetectedAction setting in config.ini. This value is “primaryoverride” (though it’s possible I might change this value by the time I release, so you’ll have to make sure you read the release announcement carefully).

When ExistingSessionDetectedAction=primaryoverride, then when another login occurs (for the same username), IBC will automatically shut down TWS/Gateway (unless this other session is a result of running IBC with ExistingSessionDetectedAction=secondary, in which case the existing session will keep running).

Also if the username is already logged in elsewhere when you start IBC with ExistingSessionDetectedAction=primaryoverride, then that session will be logged out (unless this other session is a result of running IBC with ExistingSessionDetectedAction=primary, in which case the existing session will keep running).

So, if you run IBC with ExistingSessionDetectedAction=primaryoverride, and then later you logon with the same account via Client Portal or IBKR Mobile (with market data), that instance of IBC will shut down, and your IBKR Mobile session will proceed. So far so good.

In order to automatically re-establish your TWS session when you finish with Client Portal/IBKR Mobile, you need to arrange for IBC to be automatically restarted, again with ExistingSessionDetectedAction=primaryoverride, after a suitable time interval (say 10 minutes). Now IBC will cause the TWS login to proceed, and your market data on your Client Portal/IBKR Mobile session will be stopped. If you haven’t actually finished your work on Client Portal/IBKR Mobile, then you can simply request the market data again, and the TWS session will be terminated by IBC.

On Windows, it’s quite easy to set up a Scheduled Task to restart IBC regularly, but only allow the Task to run if there isn’t already a running instance. In fact a sample task scheduler definition file is included in the IBC download, which you can import into Task Scheduler and customise to suit your needs.

[On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, at least on Windows, this will achieve what you want, except for the fact that if you haven’t finished your Client Portal/IBKR Mobile the next time IBC is started, you’ll have to re-request market data. So it’s a bit clunky, but at present I can’t think of any better alternative. Also I’ve only tried this out with the Client Portal so far (and it works for the upcoming new version of IBC), but not yet with IBKR Mobile because I don’t have an Android or iOS smartphone (I’ll have to try it on my wife’s phone, if she’ll let me!).

Please note that this enhancement will only be available in IBC, not in IBController.

Watch this space!

Richard

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of C++_API
Sent: 03 April 2019 05:00
To: ibcalpha@groups.io
Subject: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?


Example Scenario:

  1. TWS started by IBController, connected, running.
  2. Open the IBKR APP on a smart phone.
  3. IBKR APP Notifies - "Another session with this name is in use, Click OK to login and disconnect other session".
  4. Click OK on APP
  5. TWS dialog pop up with "connection lost, do you want to disconnect other user, etc".
  6. Make trades on APP
  7. Close APP

At this point TWS does NOT automatically regain connection and have to manually use the dialog (that popped up in step 5) to reconnect TWS.

Is there a way for TWS to reconnect automatically, once the APP disconnects?

Thank You.

Richard L King

  • Member Profile
  • All Messages By This Member

#882


Jesse

Thanks very much for that. I’ll have a play.

But first I really will have to rebuild my Ubuntu VM that was corrupted when I applied some updates a while back, and won’t boot now. I dare say someone with decent Linux skills could resurrect it, but I’m a bit feeble with it and it’s probably quicker just to reinstall.

Richard

toggle quoted messageShow quoted text

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of Jesse Adelman
Sent: 03 April 2019 17:34
To: ibcalpha@groups.io
Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Hi Richard. Thank you for this suite of applications! I'd just like to make some suggestions regarding only the following paragraph (because Linux is my bailiwick, but anything that this app actually exists to accomplish is most decidedly NOT. haha) No guarantee that these will work!

>> [On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, there's a couple ideas you and other folks might investigate using:

* Use Supervisor (http://supervisord.org/).

- If the event handlers built-in to Supervisor aren't enough, addhttps://pypi.org/project/superlance/to control supervisor-managed programs based on runtime events (based on things like CPU usage, memory usage, etc.)

* In crontab, something like:

0 10 * * * * pgrep -f <definitive process name> || <program name>

pgrep (for p)rocess grep) without -f just looks at the process name, but with -f looks at the full command line. In this example, if the process is *not* running, it would execute <program name>. If it is *already* running, it would do nothing.

Personally, I like the Supervisor model better, because it is well-written and handles it's domain very well. But, hey, the shell and XOR can Just Work (tm).

Hope that helps!

Cheers,

Jesse Adelman

Systems Engineer (DevOps/SRE)

Pittsburgh, PA (and remote to the world)

----- Original message -----

From: Richard L King <rlking@...>

To: ibcalpha@groups.io

Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Date: Wednesday, April 03, 2019 11:16 AM

I will be making a new release of IBC within a few days, which will provide some help here.

The new release will accept an additional value for the ExistingSessionDetectedAction setting in config.ini. This value is “primaryoverride” (though it’s possible I might change this value by the time I release, so you’ll have to make sure you read the release announcement carefully).

When ExistingSessionDetectedAction=primaryoverride, then when another login occurs (for the same username), IBC will automatically shut down TWS/Gateway (unless this other session is a result of running IBC with ExistingSessionDetectedAction=secondary, in which case the existing session will keep running).

Also if the username is already logged in elsewhere when you start IBC with ExistingSessionDetectedAction=primaryoverride, then that session will be logged out (unless this other session is a result of running IBC with ExistingSessionDetectedAction=primary, in which case the existing session will keep running).

So, if you run IBC with ExistingSessionDetectedAction=primaryoverride, and then later you logon with the same account via Client Portal or IBKR Mobile (with market data), that instance of IBC will shut down, and your IBKR Mobile session will proceed. So far so good.

In order to automatically re-establish your TWS session when you finish with Client Portal/IBKR Mobile, you need to arrange for IBC to be automatically restarted, again with ExistingSessionDetectedAction=primaryoverride, after a suitable time interval (say 10 minutes). Now IBC will cause the TWS login to proceed, and your market data on your Client Portal/IBKR Mobile session will be stopped. If you haven’t actually finished your work on Client Portal/IBKR Mobile, then you can simply request the market data again, and the TWS session will be terminated by IBC.

On Windows, it’s quite easy to set up a Scheduled Task to restart IBC regularly, but only allow the Task to run if there isn’t already a running instance. In fact a sample task scheduler definition file is included in the IBC download, which you can import into Task Scheduler and customise to suit your needs.

[On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, at least on Windows, this will achieve what you want, except for the fact that if you haven’t finished your Client Portal/IBKR Mobile the next time IBC is started, you’ll have to re-request market data. So it’s a bit clunky, but at present I can’t think of any better alternative. Also I’ve only tried this out with the Client Portal so far (and it works for the upcoming new version of IBC), but not yet with IBKR Mobile because I don’t have an Android or iOS smartphone (I’ll have to try it on my wife’s phone, if she’ll let me!).

Please note that this enhancement will only be available in IBC, not in IBController.

Watch this space!

Richard

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of C++_API
Sent: 03 April 2019 05:00
To: ibcalpha@groups.io
Subject: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?


Example Scenario:

  1. TWS started by IBController, connected, running.
  2. Open the IBKR APP on a smart phone.
  3. IBKR APP Notifies - "Another session with this name is in use, Click OK to login and disconnect other session".
  4. Click OK on APP
  5. TWS dialog pop up with "connection lost, do you want to disconnect other user, etc".
  6. Make trades on APP
  7. Close APP

At this point TWS does NOT automatically regain connection and have to manually use the dialog (that popped up in step 5) to reconnect TWS.

Is there a way for TWS to reconnect automatically, once the APP disconnects?

Thank You.

C++_API
  • All Messages By This Member

#884


Richard,

Thanks for the info, yes that would do the trick.

I will be switching to IBC as soon as the new feature is there.

Will keep an eye out on this post for an update...

Thank You.

Richard L King

  • Member Profile
  • All Messages By This Member

#886


Ok fine.

I won’t be able to test the new version with the IBKR Mobile app (rather than Client Portal which it already works with) until the weekend at the earliest (assuming my wife agrees to me installing it on her phone!), but I may well do the new release before then because if this new setting doesn’t do the job when using IBKR Mobile there’s probably nothing I can do differently in IBC that would help.

I suppose I’m going to have to fork out for an Android phone some time soon. I have two Windows phones that I love, and I’ve been kinda hoping that Microsoft might resurrect Windows 10 Mobile, perhaps in some new form-factor device, but it certainly doesn’t look likely before support for current Windows 10 Mobile ends, and probably never.

toggle quoted messageShow quoted text

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of C++_API
Sent: 04 April 2019 03:46
To: ibcalpha@groups.io
Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Richard,

Thanks for the info, yes that would do the trick.

I will be switching to IBC as soon as the new feature is there.

Will keep an eye out on this post for an update...

Thank You.

ezar11

  • All Messages By This Member

#889


Hi Richard,

Taking advantage of this thread regarding connects and disconnects, if you could clarify a doubt regarding this it would be awesome.

We are building an escalable app that involves different physical client accounts. The system of each client is functioning in a cloud with a certain clientId and has been launched with IBC. I would like to maintain that session which is running the system if me or anyone connects via Client Portal or Ibroker Mobile (connecting another TWS/IBG I "assume" that can be done with another clientID without interfering in the first session) on another device. What I really mean is that I don't want that the automated system, which is the first opened session with IBC to stop if any other new connection comes (i.e. if any client, although we have told them that they should not use this other resources, decides not to remember that and logs in with his account).

NOTE: We are researching to create another account for each client and link it to their master, in which case I think we won't have this problem at all. I think that this is possible.

If I correctly understand, I can manage to do that with the setting ExistingSessionDetectedAction=primary on the automated system instance, so that if any other connection comes with the same account this new session will be ¿stopped? because the first one is running.

¿Am I correct in my assumptions?

Many thanks for the time.

Richard L King

  • Member Profile
  • All Messages By This Member

#891


Yes, that’s correct. It’s exactly why ExistingSessionDetectedAction=primary was provided in the first place quite a few years ago now, to ensure that a live session couldn’t be terminated by someone doing something silly elsewhere.

toggle quoted messageShow quoted text

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of ezar11
Sent: 04 April 2019 12:47
To: ibcalpha@groups.io
Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Hi Richard,

Taking advantage of this thread regarding connects and disconnects, if you could clarify a doubt regarding this it would be awesome.

We are building an escalable app that involves different physical client accounts. The system of each client is functioning in a cloud with a certain clientId and has been launched with IBC. I would like to maintain that session which is running the system if me or anyone connects via Client Portal or Ibroker Mobile (connecting another TWS/IBG I "assume" that can be done with another clientID without interfering in the first session) on another device. What I really mean is that I don't want that the automated system, which is the first opened session with IBC to stop if any other new connection comes (i.e. if any client, although we have told them that they should not use this other resources, decides not to remember that and logs in with his account).

NOTE: We are researching to create another account for each client and link it to their master, in which case I think we won't have this problem at all. I think that this is possible.

If I correctly understand, I can manage to do that with the setting ExistingSessionDetectedAction=primary on the automated system instance, so that if any other connection comes with the same account this new session will be ¿stopped? because the first one is running.

¿Am I correct in my assumptions?

Many thanks for the time.

ezar11

  • All Messages By This Member

#893


Perfect. Thanks for the work and clarification Richard.

Richard L King

  • Member Profile
  • All Messages By This Member

#909


Jesse

I’ve been trying your suggestion for using pgrep to determine whether IBC is already running with cron, and I’ve been tearing my hair out.

Here’s what I’ve got in my crontab (this is Ubuntu 16.04 LTS):

SHELL=/bin/bash

0,15,30,45 * * * * /usr/bin/pgrep -f "/home/richard/ibc/config.ini" || (export DISPLAY=:10 && /opt/ibc/gatewaystart.sh -inline)

Now, the command works perfectly if entered directly in terminal, but it simply does nothing in cron. If I remove the pgrep part it also works fine in cron, ie unconditionally running the gatewaystart.sh script is no problem (provided it’s not already running of course).

Even writing the condition out longhand using if [ … ] doesn’t help: for example

0,15,30,45 * * * * if [ -z $(/usr/bin/pgrep -f "/home/richard/ibc/config.ini") ]; then (export DISPLAY=:10 && /opt/ibc/gatewaystart.sh -inline) ;fi

And even an else clause never gets executed either. So it appears that the pgrep ‘condition’ is simply causing an error, whatever form I write it in.

If you (or anyone else) have any suggestions as to what I’m doing wrong here, I’d be extremely grateful.

Thanks

Richard

toggle quoted messageShow quoted text

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of Richard L King
Sent: 03 April 2019 18:05
To: ibcalpha@groups.io
Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Jesse

Thanks very much for that. I’ll have a play.

But first I really will have to rebuild my Ubuntu VM that was corrupted when I applied some updates a while back, and won’t boot now. I dare say someone with decent Linux skills could resurrect it, but I’m a bit feeble with it and it’s probably quicker just to reinstall.

Richard

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of Jesse Adelman
Sent: 03 April 2019 17:34
To: ibcalpha@groups.io
Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Hi Richard. Thank you for this suite of applications! I'd just like to make some suggestions regarding only the following paragraph (because Linux is my bailiwick, but anything that this app actually exists to accomplish is most decidedly NOT. haha) No guarantee that these will work!

>> [On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, there's a couple ideas you and other folks might investigate using:

* Use Supervisor (http://supervisord.org/).

- If the event handlers built-in to Supervisor aren't enough, addhttps://pypi.org/project/superlance/to control supervisor-managed programs based on runtime events (based on things like CPU usage, memory usage, etc.)

* In crontab, something like:

0 10 * * * * pgrep -f <definitive process name> || <program name>

pgrep (for p)rocess grep) without -f just looks at the process name, but with -f looks at the full command line. In this example, if the process is *not* running, it would execute <program name>. If it is *already* running, it would do nothing.

Personally, I like the Supervisor model better, because it is well-written and handles it's domain very well. But, hey, the shell and XOR can Just Work (tm).

Hope that helps!

Cheers,

Jesse Adelman

Systems Engineer (DevOps/SRE)

Pittsburgh, PA (and remote to the world)

----- Original message -----

From: Richard L King <rlking@...>

To: ibcalpha@groups.io

Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Date: Wednesday, April 03, 2019 11:16 AM

I will be making a new release of IBC within a few days, which will provide some help here.

The new release will accept an additional value for the ExistingSessionDetectedAction setting in config.ini. This value is “primaryoverride” (though it’s possible I might change this value by the time I release, so you’ll have to make sure you read the release announcement carefully).

When ExistingSessionDetectedAction=primaryoverride, then when another login occurs (for the same username), IBC will automatically shut down TWS/Gateway (unless this other session is a result of running IBC with ExistingSessionDetectedAction=secondary, in which case the existing session will keep running).

Also if the username is already logged in elsewhere when you start IBC with ExistingSessionDetectedAction=primaryoverride, then that session will be logged out (unless this other session is a result of running IBC with ExistingSessionDetectedAction=primary, in which case the existing session will keep running).

So, if you run IBC with ExistingSessionDetectedAction=primaryoverride, and then later you logon with the same account via Client Portal or IBKR Mobile (with market data), that instance of IBC will shut down, and your IBKR Mobile session will proceed. So far so good.

In order to automatically re-establish your TWS session when you finish with Client Portal/IBKR Mobile, you need to arrange for IBC to be automatically restarted, again with ExistingSessionDetectedAction=primaryoverride, after a suitable time interval (say 10 minutes). Now IBC will cause the TWS login to proceed, and your market data on your Client Portal/IBKR Mobile session will be stopped. If you haven’t actually finished your work on Client Portal/IBKR Mobile, then you can simply request the market data again, and the TWS session will be terminated by IBC.

On Windows, it’s quite easy to set up a Scheduled Task to restart IBC regularly, but only allow the Task to run if there isn’t already a running instance. In fact a sample task scheduler definition file is included in the IBC download, which you can import into Task Scheduler and customise to suit your needs.

[On Linux, the picture isn’t quite so rosy. I don’t know of a way to restart IBC regularly only if there isn’t an instance already running. You could just use crontab, set to start IBC every 10 minutes on weekdays, for example: what will happen then is that the second and subsequent attempts while one is still running will fail because the log file won’t be accessible (already in use). Without actually trying it, I’m not sure what will happen, but I suspect you’ll end up with a mas of red banner windows. I’ll have to find some time to check this out.]

So, at least on Windows, this will achieve what you want, except for the fact that if you haven’t finished your Client Portal/IBKR Mobile the next time IBC is started, you’ll have to re-request market data. So it’s a bit clunky, but at present I can’t think of any better alternative. Also I’ve only tried this out with the Client Portal so far (and it works for the upcoming new version of IBC), but not yet with IBKR Mobile because I don’t have an Android or iOS smartphone (I’ll have to try it on my wife’s phone, if she’ll let me!).

Please note that this enhancement will only be available in IBC, not in IBController.

Watch this space!

Richard

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of C++_API
Sent: 03 April 2019 05:00
To: ibcalpha@groups.io
Subject: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?


Example Scenario:

  1. TWS started by IBController, connected, running.
  2. Open the IBKR APP on a smart phone.
  3. IBKR APP Notifies - "Another session with this name is in use, Click OK to login and disconnect other session".
  4. Click OK on APP
  5. TWS dialog pop up with "connection lost, do you want to disconnect other user, etc".
  6. Make trades on APP
  7. Close APP

At this point TWS does NOT automatically regain connection and have to manually use the dialog (that popped up in step 5) to reconnect TWS.

Is there a way for TWS to reconnect automatically, once the APP disconnects?

Thank You.

ezar11

  • All Messages By This Member

#910


Hi Richard and Jesse,

I will try to add my two cents, although I'm not an expert on Linux. However, I needed to find a way to execute a script in a terminal that was launched from a cron script. The following idea might work well if the command you are trying can be perfectly executed in a normal terminal, which if I understood well your below response, it occurs.

The idea is to use cron to launch a terminal and then execute the command there. Once the command is finished, you can choose on closing the terminal or leave it open. It would look like this:

11 07 * * 1-5 DISPLAY=:2 /usr/bin/mate-terminal -t "c1" --geometry=146x28 -x bash -c 'command/to/execute; exec bash'

Clues:

  • This case is for a mate-terminal in Ubuntu Mate. However, it should work with other terminals associated with their respective desktop environment.

  • You need to discover the display, which in my case was the number 2 - it seems that you have already found it - (I don't remember exactly where I found the display command, but I have found this). The script is to launch a terminal on that display. The other options are for setting the tittle (-t) and setting its size (--geometry).

  • To ensure that I'm running bash, I just execute bash (the terminal is launched in sh, not bash) and then use the -c option to execute the command.

  • The final part (i.e. exec bash) is to remain the terminal open once the command has finished its execution.

Hope it helps to gain more intuition. If not, tell me and I will try to help more related to my experience with this.

Richard L King

  • Member Profile
  • All Messages By This Member

#912


Thanks for the suggestion: I can see that it will work, but first I wanted to try and understand why what I was doing doesn’t work, and I’ve now managed to do that.

I was using the /usr/bin/pgrep -f "/home/richard/ibc/config.ini" statement to find an existing instance of IBC by searching for a process with the specified config.ini as a parameter, since in general there can only be one IBC instance using a given config.ini. [ Note that that’s not completely true, because some things can be specified in the start scripts themselves, so for example you could use a single config.ini and use it for both live and paper trading at the same time by specifying the username, password and paper trading mode in the start scripts: but that’s not a normal setup.]

So if IBC is running with the specified config.ini, then running the pgrep command in an existing terminal session will find it, and if it’s not running it won’t find it. So that gives me a nice check. By the way, I can’t use the process name because that’s “java” for all java programs.

But when you run the pgrep command within a cron job, cron evidently runs it in a new process whose command line is exactly the pgrep statement itself, and therefore the command line for this new process contains the string we’re looking for, and hence pgrep always finds a process with the specified string!

The answer to this conundrum is simply to put the complete command that I mentioned last time into a script file of its own, and to specify that script file in the crontab.

So now I have a gatewaystartcron.sh file which contains:

/usr/bin/pgrep -f "/home/richard/ibc/config.ini" || (export DISPLAY=:10 && /opt/ibc/gatewaystart.sh -inline)

And my crontab contains:

SHELL=/bin/bash

0,15,30,45 * * * * /opt/ibc/ gatewaystartcron.sh

Note that use of the -inline switch in the gatewaystartcron.sh file suppresses the banner display that displays where the logfile is etc. If you prefer to see the banner window, just omit -inline.

I may decide to incorporate the pgrep -f “<configfilename>” check into the normal gatewaystart.sh and twsstart.sh scripts (or perhaps into the lower-level scripts/ibcstart.sh) so that they can be used directly in crontab.

Richard

toggle quoted messageShow quoted text

From: ibcalpha@groups.io <ibcalpha@groups.io> On Behalf Of ezar11
Sent: 23 April 2019 00:17
To: ibcalpha@groups.io
Subject: Re: [ibc] Is there a way for TWS to reconnect, once the APP disconnects?

Hi Richard and Jesse,

I will try to add my two cents, although I'm not an expert on Linux. However, I needed to find a way to execute a script in a terminal that was launched from a cron script. The following idea might work well if the command you are trying can be perfectly executed in a normal terminal, which if I understood well your below response, it occurs.

The idea is to use cron to launch a terminal and then execute the command there. Once the command is finished, you can choose on closing the terminal or leave it open. It would look like this:

11 07 * * 1-5 DISPLAY=:2 /usr/bin/mate-terminal -t "c1" --geometry=146x28 -x bash -c 'command/to/execute; exec bash'

Clues:

  • This case is for a mate-terminal in Ubuntu Mate. However, it should work with other terminals associated with their respective desktop environment.
  • You need to discover the display, which in my case was the number 2 - it seems that you have already found it - (I don't remember exactly where I found the display command, but I have found this). The script is to launch a terminal on that display. The other options are for setting the tittle (-t) and setting its size (--geometry).
  • To ensure that I'm running bash, I just execute bash (the terminal is launched in sh, not bash) and then use the -c option to execute the command.
  • The final part (i.e. exec bash) is to remain the terminal open once the command has finished its execution.

Hope it helps to gain more intuition. If not, tell me and I will try to help more related to my experience with this.

Is there a way for TWS to reconnect, once the APP disconnects? (2024)
Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5533

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.