ib_insync icon indicating copy to clipboard operation
ib_insync copied to clipboard

ib_insync connect timing-out in "live" mode only, when ibapi is able to connect

Open JadSayegh opened this issue 6 years ago • 3 comments

I'm trying to connect to my TWS live on localhost using ib_insync but the connection is timing out (stacktrace for that at the bottom). Here's the weird parts:

  • This only happens in "Live" mode
  • Same script connects successfully to paper trading, despite the same configuration aside from the port
  • Similar script using the ibapi package is successfully connecting to TWS despite the same port and host

What could make ib_insync "live trading" behave so differently from ibapi and/or "paper trading" during connection? How can I fix the timeouts

ib_insync test code that times out

from ib_insync import *
ib = IB()
ib.connect('127.0.0.1', 7496, clientId=100)

ibapi test code that works

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.utils import iswrapper
class TestWrapper(EWrapper):
    pass

class TestClient(EClient):
    def __init__(self, wrapper):
        EClient.__init__(self, wrapper)

class TestApp(TestWrapper, TestClient):
    def __init__(self):
        TestWrapper.__init__(self)
        TestClient.__init__(self, wrapper=self)

TestApp().connect("127.0.0.1", 7496, clientId=100)

Timeout stack trace

Traceback (most recent call last):
  File "src/test.py", line 105, in <module>
    ib.connect('127.0.0.1', 7496, clientId=100)
  File "/Users/jadsayegh/Documents/code/testib/venv/lib/python3.6/site-packages/ib_insync/ib.py", line 261, in connect
    host, port, clientId, timeout, readonly, account))
  File "/Users/jadsayegh/Documents/code/testib/venv/lib/python3.6/site-packages/ib_insync/ib.py", line 292, in _run
    return util.run(*awaitables, timeout=self.RequestTimeout)
  File "/Users/jadsayegh/Documents/code/testib/venv/lib/python3.6/site-packages/ib_insync/util.py", line 311, in run
    result = loop.run_until_complete(task)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
    return future.result()
  File "/Users/jadsayegh/Documents/code/testib/venv/lib/python3.6/site-packages/ib_insync/ib.py", line 1619, in connectAsync
    await asyncio.wait_for(connect(), timeout or None)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 362, in wait_for
    raise futures.TimeoutError()
concurrent.futures._base.TimeoutError

JadSayegh avatar Mar 31 '20 05:03 JadSayegh

This could be the same as #224.

erdewit avatar Apr 01 '20 20:04 erdewit

Hi @erdewit, after discussing with IB staff and going over our logs, we found out that the issue was being caused by the API loading all subaccounts upon connection. Because we have very many subaccounts, it is causing the timeout. I tried setting the timeout to 0 and the connection simply never happened. I quickly (within a few seconds) got an error message from the API saying I was close to exceeding allowed number of requests for minute.

Is there any way I can avoid having all those sub-accounts load on startup?

JadSayegh avatar Apr 02 '20 12:04 JadSayegh

v0.9.60 has an added IB.MaxSyncedSubAccounts setting that will skip downloading subaccounts if their number exceeds this setting.

erdewit avatar May 07 '20 10:05 erdewit