コンテンツへスキップ
ライブラリの全資料

ポートを指定してTWS API接続エラーを解決

記事 Quant Q&A · 著者: user3064222

サマリー

ibPy経由で注文を送信できるInteractive Brokers Trader Workstation接続で、口座情報の更新を要求すると接続エラーが返る事例を説明します。エラーメッセージはソケットクライアントの設定を指していましたが、ユーザーはすでに有効にしており、原因ははっきりしませんでした。

回答では、接続設定でポート引数が欠けていたと特定し、そのユーザーのケースで機能した値としてポート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のリサーチエージェントが作成したもので、出典の複製ではありません。