|
| 1 | +# Broker Integration Spec — alpha-v1.0 实盘接入层 |
| 2 | + |
| 3 | +> **状态**:设计文档,未实现 |
| 4 | +> **前提**:需要券商账户 + API 权限 |
| 5 | +> **风险警告**:实盘交易可能亏损本金。本系统不构成投资建议。 |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## 1. 架构 |
| 10 | + |
| 11 | +``` |
| 12 | +live_engine.py |
| 13 | + │ signal, risk, state |
| 14 | + ▼ |
| 15 | +execution/adapter.py ← broker adapter layer |
| 16 | + │ 统一的 order/report 接口 |
| 17 | + ▼ |
| 18 | +broker SDK (IB / 富途 / 某国内券商) |
| 19 | + │ |
| 20 | + ▼ |
| 21 | +交易所 |
| 22 | +``` |
| 23 | + |
| 24 | +## 2. BrokerAdapter 接口 |
| 25 | + |
| 26 | +```python |
| 27 | +class BrokerAdapter(ABC): |
| 28 | + """券商接入抽象层.""" |
| 29 | + |
| 30 | + @abstractmethod |
| 31 | + def connect(self, config: dict) -> bool: |
| 32 | + """连接券商 API.""" |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def get_account(self) -> AccountInfo: |
| 36 | + """查询账户信息 (现金, 持仓, 购买力).""" |
| 37 | + |
| 38 | + @abstractmethod |
| 39 | + def place_order(self, order: Order) -> OrderId: |
| 40 | + """下单.""" |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def cancel_order(self, order_id: OrderId) -> bool: |
| 44 | + """撤单.""" |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def get_positions(self) -> list[Position]: |
| 48 | + """查询当前持仓.""" |
| 49 | + |
| 50 | + @abstractmethod |
| 51 | + def get_orders(self) -> list[OrderStatus]: |
| 52 | + """查询未成交订单.""" |
| 53 | + |
| 54 | + @abstractmethod |
| 55 | + def disconnect(self): |
| 56 | + """断开连接.""" |
| 57 | +``` |
| 58 | + |
| 59 | +## 3. 数据模型 |
| 60 | + |
| 61 | +```python |
| 62 | +@dataclass |
| 63 | +class Order: |
| 64 | + asset: str |
| 65 | + side: Literal["buy", "sell"] |
| 66 | + quantity: int |
| 67 | + order_type: Literal["market", "limit"] = "market" |
| 68 | + limit_price: float | None = None |
| 69 | + |
| 70 | +@dataclass |
| 71 | +class OrderStatus: |
| 72 | + order_id: str |
| 73 | + asset: str |
| 74 | + side: str |
| 75 | + quantity: int |
| 76 | + filled: int |
| 77 | + status: str # pending, filled, cancelled, rejected |
| 78 | + avg_price: float |
| 79 | + |
| 80 | +@dataclass |
| 81 | +class AccountInfo: |
| 82 | + cash: float |
| 83 | + equity: float |
| 84 | + buying_power: float |
| 85 | + positions: list[Position] |
| 86 | +``` |
| 87 | + |
| 88 | +## 4. 接入方式 |
| 89 | + |
| 90 | +| 券商 | SDK | 接入难度 | 备注 | |
| 91 | +|------|-----|---------|------| |
| 92 | +| Interactive Brokers | ib_insync | 中 | 标准 API,文档完善 | |
| 93 | +| 富途牛牛 | FutuOpenD | 中 | 需要 OpenD 进程 | |
| 94 | +| 国内券商 (QMT) | xtquant | 低 | 国金/华鑫等支持 miniQMT | |
| 95 | +| 华泰 | | 高 | 需要额外权限 | |
| 96 | + |
| 97 | +## 5. 安全规则(不可违反) |
| 98 | + |
| 99 | +``` |
| 100 | +1. adapter 不存储任何账号密码 |
| 101 | +2. 首次交易前必须人工确认 |
| 102 | +3. 每笔订单有金额上限保护 |
| 103 | +4. Kill switch 必须可随时手动触发 |
| 104 | +5. 默认为 paper mode,需显式切换为 live |
| 105 | +``` |
| 106 | + |
| 107 | +## 6. 实现优先级 |
| 108 | + |
| 109 | +``` |
| 110 | +P0: BrokerAdapter 接口定义 |
| 111 | +P1: PaperBrokerAdapter (已有 SimulatedExchange, 可复用) |
| 112 | +P2: 接入一家具体券商 |
| 113 | +P3: 订单状态同步 + 对账 |
| 114 | +P4: 断线重连 + 错误恢复 |
| 115 | +``` |
0 commit comments