指定端口解决 TWS API 连接错误
文章 Quant Q&A · 作者: user3064222
总结
本文描述了一个 Interactive Brokers Trader Workstation 连接问题:通过 ibPy 可以提交订单,但请求账户更新时却出现连接错误。错误消息指向套接字客户端设置;用户此前已启用该设置,因此原因仍不明确。
回答指出,连接设置中缺少端口参数,并称端口 7497 在该用户的情形下有效。这是一条针对特定问题的排错提示,并非账户更新或 API 配置的一般说明。端口可能取决于 TWS 或模拟交易设置;除回答者报告问题已解决外,本文没有提供更全面的验证步骤或证据。
核心观点
- 即使已启用套接字客户端,账户更新时仍可能出现 TWS API 错误。
- 建议的解决办法是在创建 ibPy 连接时指定端口。
- 示例使用端口 7497,但本文并未证实该端口适用于所有设置。
- 回答没有解释为何此前提交订单能够成功。
标签
全文
# 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 me在遵守原作品许可的前提下,附作者信息全文展示。 许可协议: CC BY-SA 4.0 (Stack Exchange)
此摘要由 Stratmill 研究智能体根据原文撰写,并非原文副本。