网淘吧来吧,欢迎您!

IBKR Trading技能使用说明

2026-03-29 新闻来源:网淘吧 围观:17
电脑广告
手机广告

IBKR交易技能

使用Client Portal Gateway API实现Interactive Brokers自动化交易。

概述

此技能支持:

IBKR Trading

  • 通过IBeam + IBKR Key实现自动化IBKR身份验证
  • 投资组合与持仓监控
  • 订单提交与管理
  • 构建自定义交易策略

前提条件

  • IBKR账户(实盘或模拟)
  • 手机上安装IBKR Key应用(用于两步验证)
  • 配备Java 11+和Chrome/Chromium的Linux服务器

快速设置

1. 安装依赖项

# Java (for Client Portal Gateway)
sudo apt-get install -y openjdk-17-jre-headless

# Chrome + ChromeDriver (for IBeam)
sudo apt-get install -y chromium-browser chromium-chromedriver

# Virtual display (headless auth)
sudo apt-get install -y xvfb

# Python venv
python3 -m venv ~/trading/venv
source ~/trading/venv/bin/activate
pip install ibeam requests

2. 下载Client Portal Gateway

cd ~/trading
wget https://download2.interactivebrokers.com/portal/clientportal.gw.zip
unzip clientportal.gw.zip -d clientportal

3. 配置凭证

创建~/trading/.env

IBEAM_ACCOUNT=your_username
IBEAM_PASSWORD='your_password'
IBEAM_GATEWAY_DIR=/path/to/trading/clientportal
IBEAM_CHROME_DRIVER_PATH=/usr/bin/chromedriver
IBEAM_TWO_FA_SELECT_TARGET="IB Key"

身份验证

启动Gateway + 身份验证

# 1. Start Client Portal Gateway
cd ~/trading/clientportal && bash bin/run.sh root/conf.yaml &

# 2. Wait for startup (~20 sec)
sleep 20

# 3. Run IBeam authentication
cd ~/trading
source venv/bin/activate
source .env
export DISPLAY=:99
Xvfb :99 -screen 0 1024x768x24 &
python -m ibeam --authenticate

重要提示:用户必须在约2分钟内在手机上批准IBKR Key通知!

检查认证状态

curl -sk https://localhost:5000/v1/api/iserver/auth/status

认证响应包含"authenticated": true

API 使用

账户信息

# List accounts
curl -sk https://localhost:5000/v1/api/portfolio/accounts

# Account summary
curl -sk "https://localhost:5000/v1/api/portfolio/{accountId}/summary"

持仓

# Current positions
curl -sk "https://localhost:5000/v1/api/portfolio/{accountId}/positions/0"

市场数据

# Search for symbol
curl -sk "https://localhost:5000/v1/api/iserver/secdef/search?symbol=AAPL"

# Get quote (after searching)
curl -sk "https://localhost:5000/v1/api/iserver/marketdata/snapshot?conids=265598&fields=31,84,86"

下单

curl -sk -X POST "https://localhost:5000/v1/api/iserver/account/{accountId}/orders" \
  -H "Content-Type: application/json" \
  -d '{
    "orders": [{
      "conid": 265598,
      "orderType": "MKT",
      "side": "BUY",
      "quantity": 1,
      "tif": "DAY"
    }]
  }'

会话管理

会话约24小时后过期。选项:

  1. 保活定时任务- 心跳/v1/api/tickle每5分钟
  2. 自动重新认证- 会话过期时运行IBeam(需要手机批准)

保活脚本

import requests
import urllib3
urllib3.disable_warnings()

def keepalive():
    try:
        r = requests.post("https://localhost:5000/v1/api/tickle", verify=False, timeout=10)
        status = requests.get("https://localhost:5000/v1/api/iserver/auth/status", verify=False, timeout=10)
        return status.json().get("authenticated", False)
    except:
        return False

故障排除

问题解决方案
网关无响应检查Java进程是否正在运行:ps aux | grep GatewayStart
登录超时用户未及时批准IBKR密钥 - 请重试认证
连接被拒绝网关未启动 - 请运行bin/run.sh root/conf.yaml
Chrome浏览器错误确保Xvfb正在运行:Xvfb :99 &export DISPLAY=:99

文件参考

查看references/api-endpoints.md获取完整的API文档。 查看scripts/获取即用型自动化脚本。

免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏
文章底部电脑广告
手机广告位-内容正文底部

相关文章

您是本站第326344名访客 今日有221篇新文章/评论