Can't connect to API interactive brokers

Discussion in 'Interactive Brokers' started by rudj87, Aug 29, 2021.

  1. rudj87

    rudj87

    Hi, i'm trying to learn using python to access to IB, but i got stuck before i start.

    I copied this script from the documentation of IB for a test:

    from ib_insync import *

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

    and i have this error:

    API connection failed: ConnectionRefusedError(10061, "Connect call failed ('127.0.0.1', 7497)")
    Make sure API port on TWS/IBG is open
    Traceback (most recent call last):
    File "E:/Users/rudy/PycharmProjects/Interactive Brokers/main.py", line 5, in <module>
    ib.connect('127.0.0.1', 7497, clientId=1)
    File "E:\Users\rudy\PycharmProjects\Interactive Brokers\venv\lib\site-packages\ib_insync\ib.py", line 272, in connect
    host, port, clientId, timeout, readonly, account))
    File "E:\Users\rudy\PycharmProjects\Interactive Brokers\venv\lib\site-packages\ib_insync\ib.py", line 310, in _run
    return util.run(*awaitables, timeout=self.RequestTimeout)
    File "E:\Users\rudy\PycharmProjects\Interactive Brokers\venv\lib\site-packages\ib_insync\util.py", line 322, in run
    result = loop.run_until_complete(task)
    File "E:\Users\rudy\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 467, in run_until_complete
    return future.result()
    File "E:\Users\rudy\PycharmProjects\Interactive Brokers\venv\lib\site-packages\ib_insync\ib.py", line 1626, in connectAsync
    await self.client.connectAsync(host, port, clientId, timeout)
    File "E:\Users\rudy\PycharmProjects\Interactive Brokers\venv\lib\site-packages\ib_insync\client.py", line 212, in connectAsync
    await asyncio.wait_for(self.conn.connectAsync(host, port), timeout)
    File "E:\Users\rudy\AppData\Local\Programs\Python\Python36-32\lib\asyncio\tasks.py", line 358, in wait_for
    return fut.result()
    File "E:\Users\rudy\PycharmProjects\Interactive Brokers\venv\lib\site-packages\ib_insync\connection.py", line 38, in connectAsync
    lambda: self, host, port)
    File "E:\Users\rudy\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 777, in create_connection
    raise exceptions[0]
    File "E:\Users\rudy\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 764, in create_connection
    yield from self.sock_connect(sock, address)
    File "E:\Users\rudy\AppData\Local\Programs\Python\Python36-32\lib\asyncio\selector_events.py", line 451, in sock_connect
    return (yield from fut)
    File "E:\Users\rudy\AppData\Local\Programs\Python\Python36-32\lib\asyncio\selector_events.py", line 481, in _sock_connect_cb
    raise OSError(err, 'Connect call failed %s' % (address,))
    ConnectionRefusedError: [Errno 10061] Connect call failed ('127.0.0.1', 7497)

    How can i solve this?
     
  2. check in TWS settings if API access authorization is enabled?
    also checks if the IP used is the same as the configuration IP in the TWS settings
     
  3. rudj87

    rudj87

    Thanks for the answers,

    yes, i enabled Activex and socket client

    where can i check if the IP is the same as the configuration IP in the tws settings?
     
  4. otctrade

    otctrade

    Do you have a paper trading account or real account @ iB?
    From the code you pasted it looks like it's trying to connect to TWS on port 7497

    Port 7497 is for connection to TWS using paper trading account
    Port 7496 is for connection to TWS using real trading account

    Port 4002 is for connection to IB Gateway using paper trading account
    Port 4001 is for connection to IB Gateway using real trading account
     
    rudj87 likes this.
  5. rudj87

    rudj87

    yeah! now it works with the port 7496 :) thanks otctrade :)
     
  6. otctrade

    otctrade

    You are welcome! If you are starting out and wanting to send trade orders, I would recommend opening a paper trading account (it's free) and trying the API through that.
     
  7. rudj87

    rudj87

    I don’t think i will send trade orders, my target is just get my opened position to an excel file ;) thanks for the advice :)
     
    otctrade likes this.