ib_insync error (can noy connect to TWS IB)

714 Views Asked by At

yesterday my code was working fine, but today without any reason for me (and no changes to the code) I can not connect to Interactive Brokers TWS via ib_insync

The code itself is longer but error appears after I try to connect to IB

Code is:

from ib_insync import IB

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1, readonly=True)

Error messege is:

Traceback (most recent call last): File "C:\Users\Yar\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 458, in wait_for fut.result() asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Users\Yar\PycharmProjects\positionkeeping2\err.py", line 4, in ib.connect('127.0.0.1', 7497, clientId=1, readonly=True) File "C:\Users\Yar\PycharmProjects\positionkeeping2\venv\lib\site-packages\ib_insync\ib.py", line 279, in connect return self._run(self.connectAsync( File "C:\Users\Yar\PycharmProjects\positionkeeping2\venv\lib\site-packages\ib_insync\ib.py", line 318, in _run return util.run(*awaitables, timeout=self.RequestTimeout) File "C:\Users\Yar\PycharmProjects\positionkeeping2\venv\lib\site-packages\ib_insync\util.py", line 341, in run result = loop.run_until_complete(task) File "C:\Users\Yar\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete return future.result() File "C:\Users\Yar\PycharmProjects\positionkeeping2\venv\lib\site-packages\ib_insync\ib.py", line 1782, in connectAsync await asyncio.wait_for(self.reqExecutionsAsync(), timeout) File "C:\Users\Yar\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 460, in wait_for raise exceptions.TimeoutError() from exc asyncio.exceptions.TimeoutError

I have tried to reisntall TWS API, reboot the computer etc - nothing has helped. I have checked all the API settings - they are correct (7497 - port, ActiveX and Socket Clients Enebled)

Also the following code (which uses ibapi) is workig fine and connects to TWS. So I can connect to TWS via ibapi bit not via ib_insync

from ibapi.client import *
from ibapi.wrapper import *

class TestApp(EClient, EWrapper):
    def __init__(self):
        EClient.__init__(self, self)
        self.instrument = None
        self.bid_price = None
        self.ask_price = None

    def nextValidId(self, orderId: int):
        self.reqMarketDataType(1)
        self.reqMktData(orderId, self.instrument, "", True, 0, [])

    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 1:  # Bid Price
            self.bid_price = price
        elif tickType == 2:  # Ask Price
            self.ask_price = price

        if self.bid_price is not None and self.ask_price is not None:
            print(f"{self.bid_price} / {self.ask_price}")
            print(f'ReqId: {reqId}')
            self.cancelMktData(reqId)
            self.disconnect()


def mainProc ():
    instrument_data = {"symbol": "NG", "secType": "FUT", "exchange": "NYMEX", "currency": "USD", "lastTradeDateOrContractMonth": 202306}

    app = TestApp()
    app.connect("127.0.0.1", 7497, 1)

    contract = Contract()
    contract.symbol = instrument_data["symbol"]
    contract.secType = instrument_data["secType"]
    contract.exchange = instrument_data["exchange"]
    contract.currency = instrument_data["currency"]
    contract.lastTradeDateOrContractMonth = instrument_data["lastTradeDateOrContractMonth"]
    app.instrument = contract

    app.run()
    app.disconnect()


mainProc()
1

There are 1 best solutions below

0
On

Change to STABLE version. File -> Change Version -> Stable