Closed loucal closed 3 days ago
Oh also I did try hardcoding a longer timeout (double and triple), but that didn't make a difference at all since the Timeout comes the instant the connection opens (attempts to open) on TWS
Those do sound like odd errors. The systems are currently working for me, so I don't think anything is critically broken with the python libraries themselves. Sometimes the TWS or IBKR gateway breaks and requires a full exit/restart cycle to fix itself, but it sounds like you tried across multiple days already.
Are you sure the client id is unique everywhere?
Did you try maybe running the gateway only and not a full TWS?
I assume your ruby tests are also using the same network address? So you're connecting to a remote system and not just something over localhost?
Initial suggestions:
0
which controls everything)3.11.0rc1
to the latest 3.11 or 3.12 and install the dependencies again:
$ pyenv install -l |grep 3.1\[12]
3.11.0
3.11-dev
3.11.1
3.11.2
3.11.3
3.11.4
3.11.5
3.11.6
3.11.7
3.11.8
3.11.9
3.12.0
3.12-dev
3.12.1
3.12.2
3.12.3
Thank you for the quick response, I had suspected I should/could be using a newer version of python but my pyenv even after updating (and installing again by following the docs) shows this as the result
3.11.0rc1
3.11-dev
3.12-dev
miniconda3-4.3.11
I'm going to keep digging, but if you have any idea why this would be happening. My guess was whatever debian or ubuntu distro I'm using here is too old and it was installed from the pkg manager, which is why I tried installing from scratch as per the docs. Maybe I'll nuke ~.pyenv and start over...
Ok, scratch that last comment, I have confirmed I get the same error on 3.11.9
Traceback (most recent call last):
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/asyncio/tasks.py", line 500, in wait_for
return fut.result()
^^^^^^^^^^^^
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/asyncio/tasks.py", line 694, in _wrap_awaitable
return (yield from awaitable.__await__())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/site-packages/ib_async/ib.py", line 337, in connect
return self._run(
^^^^^^^^^^
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/site-packages/ib_async/ib.py", line 381, in _run
return util.run(*awaitables, timeout=self.RequestTimeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/site-packages/ib_async/util.py", line 357, in run
result = loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/site-packages/ib_async/ib.py", line 1973, in connectAsync
await self.client.connectAsync(host, port, clientId, timeout)
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/site-packages/ib_async/client.py", line 228, in connectAsync
await asyncio.wait_for(self.apiStart, timeout)
File "/home/lou/.pyenv/versions/3.11.9/lib/python3.11/asyncio/tasks.py", line 502, in wait_for
raise exceptions.TimeoutError() from exc
TimeoutError
I haven't yet tried the standalone gateway, I'd like to ideally avoid it since I would use my implementation side by side with TWS running.
Also, to answer your other questions I do usually use random to generate the clientId, and I always try a few times and also hard code it to something unique.
Yes, this is over my local network so not just localhost but I know there are no firewall issues because the ruby library connects from all the different machines I've tried (just like ib_insync did for over 2 years lol).
Looks like I need to really dig into the asyncio lib and figure this out :-|
I also tried python v 3.12.3 by using icli and poetry and reproduced the problem there.
2024-05-15 23:39:02.093 | INFO | icli.helpers:<module>:53 - Futures Next Roll-Forward Date: 2024-06-17
2024-05-15 23:39:02.922 | INFO | icli.cli:reconnect:2680 - Connecting to IBKR API...
2024-05-15 23:39:07.039 | ERROR | icli.cli:reconnect:2741 - [] Failed to connect to IB Gateway, trying again...
2024-05-15 23:39:14.050 | ERROR | icli.cli:reconnect:2741 - [] Failed to connect to IB Gateway, trying again...
2024-05-15 23:39:21.062 | ERROR | icli.cli:reconnect:2741 - [] Failed to connect to IB Gateway, trying again...
2024-05-15 23:39:28.072 | ERROR | icli.cli:reconnect:2741 - [] Failed to connect to IB Gateway, trying again...
^C2024-05-15 23:39:33.335 | ERROR | icli.cli:reconnect:2741 - [] Failed to connect to IB Gateway, trying again...
^C2024-05-15 23:39:34.196 | WARNING | icli.cli:reconnect:2751 - Exit requested during sleep. Goodbye.
Just baffled because it does seem like it should be some misconfiguration in my TWS but I haven't changed the settings in months (and they are backed up) plus the whole ruby library working in the same situation leads me to think its something in asyncio
maybe its better if you share part of your code that relates to connecting.. and also share your TWS API config settings..
from what i can gather above i'm still not very sure what's the steps to replicate your scenario.. is the prompt asking you to allow connection to TWS still popping up? or since 2 weeks ago you seen it its no longer showing?
I had a previously working implementation of ib_insync that started giving "Failed to connect to IB gateway/ Timeout Error" as soon as the TWS popup asks if I want to allow the connection about 2 or 3 weeks ago. I've been going crazy trying to track down the exact cause and was shocked to find the news about ib_insync's creator, and then even more shock to have the same issue when using this new and updated library. Whitelisting doesn't help and as a way to rule out my implementation, I have the same issue with @mattsta 's icli (and also I ran the example code in a jupiter notebook to triple check, same results output follows:
Any feedback would be VERY appreciated, strangely the ruby IBKR library that hasn't been updated since 2016 connects properly to my TWS (and even submits trades successfully) so I know it TWS still set up correctly, but I am starting to think I need to reach out and see if I'm the only one.
P.S. I was previously using it with older python versions (3.10) but as you can see above, 3.11 also didn't work.