Fixing TWS API Connection Errors by Specifying the Port
Summary
The document describes an Interactive Brokers Trader Workstation connection that could submit orders through ibPy but returned a connection error when requesting account updates. The error message pointed to the socket client setting, which the user had already enabled, leaving the cause unclear.
The response identifies the missing port argument in the connection setup and gives port 7497 as the working value for that user's case. This is a narrow troubleshooting tip, not a general explanation of account updates or API configuration. The port may depend on the TWS or paper-trading setup, and the document provides no broader verification steps or evidence beyond the responder's report that it worked.
Key ideas
- A TWS API error during account updates can persist even when socket clients are enabled.
- The suggested fix is to specify the connection port when creating the ibPy connection.
- The example uses port 7497, but the document does not establish that this value applies to every setup.
- The response does not explain why order submission had worked previously.
Tags
Full text
# Trader Workstation on Ubuntu cannot be connected to via the API
# Trader Workstation on Ubuntu cannot be connected to via the API
I am using ibPy to connect to TWS on a fairly fresh ubuntu machine. I have been successful in logging into the paper trading account and submitting buy and sell orders programatically via the ibPy interface.
However, I am now trying to do more than simply submit orders. Namely, I am trying to obtain position updated from TWS. I am interested in successfully running the following code:
```
from time import sleep
from ib.opt import ibConnection, message
def error_handler(msg):
print(msg)
def acct_update(msg):
print(msg)
con = ibConnection(clientId=100)
con.register(acct_update,
message.updateAccountValue,
message.updateAccountTime,
message.updatePortfolio)
con.register(error_handler, "Error")
con.connect()
con.reqAccountUpdates(True, 'DU000000')
sleep(1)
con.disconnect()
```
When executed however I obtain the following error:
```
<error id=-1, errorCode=502, errorMsg=Couldn't connect to TWS.
Confirm that "Enable ActiveX and Socket Clients" is enabled on
the TWS "Configure->API" menu.>
```
I have ensured that indeed ActiveX and socket clients are enabled in the TWS preferences, so that is not the issue. It surprises me that I would be able to submit orders successfully but not obtain account updates from TWS. Does anyone know why this could be happening?
## Answer by Clebson Derivan (score 0)
https://quant.stackexchange.com/a/27473
you forgot the parameter port:
> con = ibConnection(port=7497, clientId=100)
works for meShown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.