Can't connect to API interactive brokers

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?
 
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?
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
 
Thanks for the answers,

Have you enabled API access to your TWS or Gateway? The error message hints that this is not the case.
https://interactivebrokers.github.io/tws-api/initial_setup.html

yes, i enabled Activex and socket client

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

where can i check if the IP is the same as the configuration IP in the tws settings?
 
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
 
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

yeah! now it works with the port 7496 :) thanks 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.
 
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.
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 :)
 
Back
Top