Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client sometimes does not reconnect gracefully after sleeping for an hour #181

Open
leebailey88 opened this issue Feb 28, 2023 · 0 comments

Comments

@leebailey88
Copy link

I've used this repo for 3 years now with varying success. My use case involves needing to run my model and possibly make trades every hour, with a sleeping period inbetween, at a high level:

let ib;
while (true) {
  try {
    ib = new IB({
      clientId: 1,
      host: '127.0.0.1',
      port: 4003
    });
    // add IB event handlers here
    break;
  } catch (e) {
    console.log('IB is down, restarting in 3 minutes', e);
    sleep(1000 * 60 * 3);
  }
}
while (true) {
  const mainArgs = { ib, sheets, sheetData, cacheEnabled, adjustLeverage, noTrades, longLeverage, shortLeverage };
  await mainLoop(mainArgs);
  const nextHourStart = getNextHourStart();
  const nextHourWait = nextHourStart.diff(moment().tz('America/New_York'));
  console.log('\nWaiting', roundTo(nextHourWait / (1000 * 60)), 'minutes to start at', nextHourStart.format('lll'), 'EST\n');
  await sleep(nextHourWait);
}

inside mainLoop:

const mainLoop = ({ ib, }) => {
  ib.connect();
  runModel(ib); // evaluate algorithm and possibly place trades
  ib.disconnect();
};

I've always had the problem that sometimes the IB client would fail to reconnect at the next hour, and even calling ib.disconnect() and ib.connect() again in a try catch would not resolve it. It's as if the client has died and only completely restarting my script and the Docker container for IB Gateway would fix it. This used to be an ok solution until IB started forcing the 2FA requirement. Now it's terrible as I'm trading futures and often asleep or not by my phone which means I experience downtime until I'm at my phone again to re-authenticate.

Has anyone else had intermittent connection issues? How did you resolve them? Is there a less buggy and more up to date Node IB API I should use instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant