IBKR スキル
概要
このスキルは、Interactive Brokers Trader Workstation(TWS)および IB Gateway と ib_insync を通じて統合し、以下の運用 IBKR ワークフローを公開します:
- 統一 Python CLI:
scripts/ibkr_cli.py - bash CLI ラッパー:
scripts/ibkr.sh - 互換エイリアス:
scripts/openclaw.sh - 下位互換性のあるスクリプトエントリポイント:
scripts/get_account_info.pyscripts/get_historical_data.pyscripts/place_order.py
前提条件
- API 権限が有効な Interactive Brokers アカウント。
- API が有効な状態で実行中の TWS または IB Gateway。
- Python 3.9 以上を推奨。
ib_insyncがインストールされていること:
pip install ib_insync
接続設定
すべての CLI は、フラグまたは環境変数を使用して接続を上書きすることができます。
環境変数:
IBKR_HOST(デフォルト127.0.0.1)IBKR_PORT(デフォルト7497)IBKR_CLIENT_ID(デフォルト1)IBKR_ACCOUNT(オプション)
例:
IBKR_PORT=4002 IBKR_CLIENT_ID=14 ./scripts/ibkr.sh account-summary
python3 scripts/ibkr_cli.py positions --host 127.0.0.1 --port 7496 --account DU123456
bash CLI
./scripts/ibkr.sh <コマンド> [引数]
サポートされているコマンド:
account(アカウントサマリー+ポジション)account-summarypositionsportfoliopnlquotehistoricalplace-ordercancel-orderopen-ordersexecutionscontract-detailsscanner
./scripts/openclaw.sh は同じ CLI サーフェスのエイリアスです。
Python CLI
python3 scripts/ibkr_cli.py <コマンド> [引数]
アカウントサービス
python3 scripts/ibkr_cli.py account-summary --json
python3 scripts/ibkr_cli.py positions --account DU123456
python3 scripts/ibkr_cli.py portfolio --json
python3 scripts/ibkr_cli.py pnl --account DU123456 --wait 2
市場データサービス
python3 scripts/ibkr_cli.py quote --symbol AAPL --sec-type STK --market-data-type 3
python3 scripts/ibkr_cli.py historical --symbol EURUSD --sec-type CASH --duration "30 D" --bar-size "1 hour"
注意:
- 履歴リクエストは、
"30 D"のようなスペースを含むトークンを含む、IB 形式の完全な値をフラグ経由でサポートします。 CASHの場合、明示的に上書きされない限り、デフォルトのwhatToShowはMIDPOINTです。
銘柄検索
python3 scripts/ibkr_cli.py contract-details --symbol ES --sec-type FUT --expiry 202606
取引と注文ライフサイクル
python3 scripts/ibkr_cli.py place-order --symbol AAPL --sec-type STK --action BUY --quantity 10 --order-type MKT
python3 scripts/ibkr_cli.py place-order --symbol AAPL --sec-type STK --action SELL --quantity 10 --order-type LMT --limit-price 250 --tif GTC
python3 scripts/ibkr_cli.py open-orders
python3 scripts/ibkr_cli.py cancel-order --order-id 12345
python3 scripts/ibkr_cli.py executions --json
CLI でサポートされている注文タイプ:
MKTLMTSTPSTP LMT
place-order はステータス更新を待機し、注文作成中に収集された API エラーを表示します。
スキャナーサービス
python3 scripts/ibkr_cli.py scanner --instrument STK --location-code STK.US.MAJOR --scan-code TOP_PERC_GAIN --rows 25
互換性スクリプト
従来のスクリプトは引き続きサポートされており、統一 CLI に委譲されるようになりました:
get_account_info.pyget_historical_data.py(従来の位置指定モードも引き続き動作)place_order.py(従来の位置指定モードも引き続き動作)
エラーハンドリング
CLI は以下の場合にゼロ以外の終了コードを返します:
- インポート/依存関係の失敗
- 接続失敗
- 銘柄(コントラクト)の判定失敗
- 無効な引数
- キャンセル対象のオープン注文が見つからない
参考資料
IBKR および ib_insync のドキュメントリンクについては、references/api_reference.md を参照してください。