盈透连接器

📎 引用文件

本文引用的文件 - agent/src/trading/connectors/ibkr/local.py - agent/src/trading/connectors/ibkr/profiles.py - agent/src/trading/connectors/ibkr/classification.py - agent/src/trading/service.py - agent/cli/_legacy.py - agent/tests/test_ibkr_local.py

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构总览
  5. 详细组件分析
  6. 依赖关系分析
  7. 性能与可靠性
  8. 故障排查指南
  9. 结论
  10. 附录:配置与使用示例

简介

本文件面向盈透证券(Interactive Brokers,简称 IBKR)连接器的实现与使用,聚焦以下目标: - 本地模式:通过 TWS 或 IB Gateway 的 Socket API 进行只读访问,不向 Vibe-Trading 传入任何 IBKR 凭据。 - 认证方式:本地模式无需 IBKR 账号密码;远程 MCP 模式需完成官方 OAuth 授权。 - TWS/Gateway 配置要求:启用 Socket API、正确端口、登录账户、客户端 ID 等。 - API 版本管理:通过 ib_async 适配层兼容不同版本的 IBKR API 调用差异。 - 复杂订单与风险管理:说明当前连接器能力边界(只读),并给出在系统内如何扩展至写操作的思路。 - 算法交易、组合策略与多资产类别:基于 IBKR 合约模型与历史数据接口,展示可支持的资产类型与数据获取方式。 - 连接状态监控与自动重连:提供健康检查、端口扫描、连接池与线程安全机制。

项目结构

与盈透连接器相关的代码主要分布在以下位置: - 连接器实现:local.py(本地 TWS/Gateway 桥接)、profiles.py(内置配置文件)、classification.py(工具读写分类)。 - 服务编排:service.py(统一入口,负责选择本地或远程路径、构建配置、调用具体实现)。 - CLI 集成:_legacy.py(命令行路由与参数传递)。 - 测试用例:test_ibkr_local.py(覆盖配置、快照、持仓、报价、历史、连接池行为等)。

graph TB subgraph "连接器" L["local.py<br/>本地TWS/Gateway桥"] P["profiles.py<br/>内置配置"] C["classification.py<br/>工具读写分类"] end S["service.py<br/>统一服务入口"] CLI["_legacy.py<br/>CLI路由"] T["test_ibkr_local.py<br/>单元测试"] CLI --> S S --> L S --> P S --> C T --> L

图表来源 - agent/src/trading/connectors/ibkr/local.py:1-686 - agent/src/trading/connectors/ibkr/profiles.py:1-45 - agent/src/trading/connectors/ibkr/classification.py:1-27 - agent/src/trading/service.py:880-1046 - agent/cli/_legacy.py:3918-4021 - agent/tests/test_ibkr_local.py:1-348

章节来源 - agent/src/trading/connectors/ibkr/local.py:1-686 - agent/src/trading/connectors/ibkr/profiles.py:1-45 - agent/src/trading/connectors/ibkr/classification.py:1-27 - agent/src/trading/service.py:880-1046 - agent/cli/_legacy.py:3918-4021 - agent/tests/test_ibkr_local.py:1-348

核心组件

章节来源 - agent/src/trading/connectors/ibkr/local.py:48-153 - agent/src/trading/connectors/ibkr/profiles.py:7-44 - agent/src/trading/connectors/ibkr/classification.py:14-26 - agent/src/trading/service.py:883-898 - agent/cli/_legacy.py:3918-4021

架构总览

下图展示了从 CLI 到本地 TWS/Gateway 的数据流与控制流,以及连接池的生命周期管理。

sequenceDiagram participant U as "用户/CLI" participant CLI as "_legacy.py" participant SVC as "service.py" participant LOC as "local.py" participant POOL as "连接池_TwsPool" participant TWS as "TWS/Gateway" U->>CLI : 执行 connector check/use 等命令 CLI->>SVC : 解析参数并调用服务方法 SVC->>LOC : 构建IBKR本地配置(合并持久化) LOC->>POOL : acquire(config) POOL->>TWS : 建立Socket连接(唯一clientId) LOC->>TWS : 请求账户快照/持仓/报价/历史 TWS-->>LOC : 返回数据 LOC-->>SVC : 标准化结果 SVC-->>CLI : 返回JSON CLI-->>U : 输出结果 LOC->>POOL : release() (引用计数归零时断开)

图表来源 - agent/cli/_legacy.py:3918-4021 - agent/src/trading/service.py:883-898 - agent/src/trading/connectors/ibkr/local.py:429-503 - agent/src/trading/connectors/ibkr/local.py:241-425

详细组件分析

本地连接器(local.py)

classDiagram class IBKRLocalConfig { +string host +int port +int client_id +string profile +string account +float timeout +bool readonly +from_mapping(data) IBKRLocalConfig +with_overrides(...) IBKRLocalConfig } class _TwsPool { -threading.local _local -Lock _lock +acquire(config) IB +release() void -_new_client_id(base) int } class LocalAPI { +check_local_status(cfg, scan) dict +get_account_snapshot(cfg) dict +get_positions(cfg) dict +get_open_orders(cfg, include_executions) dict +get_quote(symbol, cfg, exchange, currency, sec_type) dict +get_historical_bars(symbol, cfg, exchange, currency, sec_type, duration, bar_size, what_to_show, use_rth) dict } IBKRLocalConfig <.. LocalAPI : "使用" _TwsPool <.. LocalAPI : "管理连接"

图表来源 - agent/src/trading/connectors/ibkr/local.py:48-153 - agent/src/trading/connectors/ibkr/local.py:429-503 - agent/src/trading/connectors/ibkr/local.py:165-425

章节来源 - agent/src/trading/connectors/ibkr/local.py:48-153 - agent/src/trading/connectors/ibkr/local.py:165-425 - agent/src/trading/connectors/ibkr/local.py:429-503

内置配置与远程 MCP(profiles.py)

章节来源 - agent/src/trading/connectors/ibkr/profiles.py:7-44

工具分类(classification.py)

章节来源 - agent/src/trading/connectors/ibkr/classification.py:14-26

服务编排(service.py)

章节来源 - agent/src/trading/service.py:883-898 - agent/src/trading/service.py:901-1046

CLI 集成(_legacy.py)

章节来源 - agent/cli/_legacy.py:3918-4021

依赖关系分析

graph LR CLI["_legacy.py"] --> SVC["service.py"] SVC --> LOC["local.py"] LOC --> IB["ib_async"] LOC --> TWS["TWS/Gateway"] TESTS["test_ibkr_local.py"] --> LOC

图表来源 - agent/cli/_legacy.py:3918-4021 - agent/src/trading/service.py:883-898 - agent/src/trading/connectors/ibkr/local.py:156-163 - agent/tests/test_ibkr_local.py:1-348

章节来源 - agent/src/trading/connectors/ibkr/local.py:156-163 - agent/src/trading/service.py:883-898 - agent/cli/_legacy.py:3918-4021 - agent/tests/test_ibkr_local.py:1-348

性能与可靠性

章节来源 - agent/src/trading/connectors/ibkr/local.py:312-350 - agent/src/trading/connectors/ibkr/local.py:429-503 - agent/src/trading/connectors/ibkr/local.py:165-218

故障排查指南

章节来源 - agent/src/trading/connectors/ibkr/local.py:156-218 - agent/src/trading/connectors/ibkr/local.py:530-541 - agent/src/trading/service.py:901-996 - agent/tests/test_ibkr_local.py:155-163

结论

附录:配置与使用示例

章节来源 - agent/src/trading/connectors/ibkr/local.py:48-153 - agent/src/trading/connectors/ibkr/local.py:241-425 - agent/cli/_legacy.py:3918-4021 - agent/tests/test_ibkr_local.py:110-170 - agent/tests/test_ibkr_local.py:237-316 - agent/tests/test_ibkr_local.py:317-348