数据模型设计

📎 引用文件

本文引用的文件 - agent/backtest/models.py - agent/backtest/engines/base.py - agent/backtest/metrics.py - agent/src/entities/models.py - agent/src/tools/trade_journal_parsers.py

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构总览
  5. 详细组件分析
  6. 依赖关系分析
  7. 性能考量
  8. 故障排查指南
  9. 结论
  10. 附录:扩展指南与最佳实践

简介

本文件面向 Vibe-Trading 的回测引擎数据模型,围绕 Position、FillRecord、TradeRecord、EquitySnapshot 四个核心实体进行系统化说明。文档涵盖设计理念(不可变数据类)、字段定义、验证规则与业务约束、实体关系图、示例数据、在回测引擎中的作用及与其他组件的交互方式,并提供扩展指南与最佳实践,帮助读者在不深入源码的情况下也能准确理解和使用这些模型。

项目结构

与数据模型直接相关的代码主要分布在以下位置: - 回测共享数据模型:agent/backtest/models.py - 回测引擎基类(使用上述模型):agent/backtest/engines/base.py - 回测指标计算(消费 FillRecord、TradeRecord):agent/backtest/metrics.py - 非K线资产实体模型(Entity/Instrument/Fund/Bond等,提供领域建模参考):agent/src/entities/models.py - 交易流水解析器(输出标准化 TradeRecord):agent/src/tools/trade_journal_parsers.py

graph TB subgraph "回测数据模型" M["models.py<br/>Position / FillRecord / TradeRecord / EquitySnapshot"] end subgraph "回测引擎" E["engines/base.py<br/>BaseEngine 运行循环"] end subgraph "指标计算" MET["metrics.py<br/>基于 FillRecord / TradeRecord"] end subgraph "实体领域模型" ENT["src/entities/models.py<br/>Entity / Instrument / Fund / Bond"] end subgraph "交易流水解析" PJ["trade_journal_parsers.py<br/>标准化 TradeRecord"] end E --> M MET --> M PJ --> M ENT -. 领域建模参考 .-> M

图表来源 - agent/backtest/models.py:13-118 - agent/backtest/engines/base.py:377-800 - agent/backtest/metrics.py:1-200 - agent/src/entities/models.py:141-397 - agent/src/tools/trade_journal_parsers.py:63-88

章节来源 - agent/backtest/models.py:1-118 - agent/backtest/engines/base.py:377-800 - agent/backtest/metrics.py:1-200 - agent/src/entities/models.py:141-397 - agent/src/tools/trade_journal_parsers.py:63-88

核心组件

以上均为不可变数据类(frozen dataclass),确保构造后状态不变,便于并发安全、可追溯和可测试。

章节来源 - agent/backtest/models.py:13-118

架构总览

回测引擎以 BaseEngine 为核心,按Bar推进执行策略信号,产生 FillRecord;当头寸被完全关闭时生成 TradeRecord;在每个Bar结束时产出 EquitySnapshot。指标模块基于 FillRecord 与 TradeRecord 计算换手率、收益、风险等统计。

sequenceDiagram participant Engine as "BaseEngine" participant Models as "数据模型(models.py)" participant Metrics as "指标(metrics.py)" Engine->>Models : 创建 Position(开仓) Engine->>Models : 记录 FillRecord(每次成交) Engine->>Models : 生成 TradeRecord(平仓完成) Engine->>Models : 追加 EquitySnapshot(每Bar) Metrics->>Models : 读取 FillRecord / TradeRecord Metrics-->>Engine : 返回统计指标

图表来源 - agent/backtest/engines/base.py:377-800 - agent/backtest/models.py:13-118 - agent/backtest/metrics.py:1-200

详细组件分析

实体关系图(ER)

erDiagram POSITION { string symbol int direction float entry_price timestamp entry_time float size float leverage int entry_bar_idx float entry_commission } FILL_RECORD { string symbol timestamp timestamp int bar_idx string action float signed_quantity float notional float execution_price float fee float margin string reason float holding_bars } TRADE_RECORD { string symbol int direction float entry_price float exit_price timestamp entry_time timestamp exit_time float size float leverage float pnl float pnl_pct string exit_reason float holding_bars float commission float entry_margin float exit_margin } EQUITY_SNAPSHOT { timestamp timestamp float capital float unrealized float equity int positions } POSITION ||--o{ FILL_RECORD : "由多次成交构成" POSITION ||--o{ TRADE_RECORD : "最终闭环形成往返交易" EQUITY_SNAPSHOT ||--o{ TRADE_RECORD : "时间序列上关联"

图表来源 - agent/backtest/models.py:13-118

Position 设计要点

章节来源 - agent/backtest/models.py:13-36

FillRecord 设计要点

章节来源 - agent/backtest/models.py:38-60

TradeRecord 设计要点

章节来源 - agent/backtest/models.py:62-99

EquitySnapshot 设计要点

章节来源 - agent/backtest/models.py:101-118

回测引擎中的使用流程(序列图)

sequenceDiagram participant BE as "BaseEngine" participant M as "models.py" participant MET as "metrics.py" Note over BE,M : 每个Bar执行 BE->>M : 根据信号创建/更新 Position BE->>M : 写入 FillRecord每次成交 BE->>M : 若头寸关闭则生成 TradeRecord BE->>M : 生成 EquitySnapshot组合快照 MET->>M : 读取 FillRecord / TradeRecord MET-->>BE : 返回统计指标年化、换手、收益等

图表来源 - agent/backtest/engines/base.py:377-800 - agent/backtest/models.py:13-118 - agent/backtest/metrics.py:1-200

数据验证规则与业务约束

章节来源 - agent/backtest/models.py:13-118 - agent/backtest/engines/base.py:377-800

示例数据(示意)

以下为概念性示例,展示各实体的典型取值范围与含义(不直接粘贴源码): - Position:symbol="AAPL", direction=1, entry_price=150.0, entry_time="2024-01-01 09:30:00", size=100, leverage=1.0, entry_bar_idx=0, entry_commission=1.0 - FillRecord:symbol="AAPL", timestamp="2024-01-01 09:30:00", bar_idx=0, action="open", signed_quantity=100, notional=15000.0, execution_price=150.0, fee=1.0, margin=15000.0, reason="signal" - TradeRecord:symbol="AAPL", direction=1, entry_price=150.0, exit_price=155.0, entry_time="2024-01-01 09:30:00", exit_time="2024-01-02 10:00:00", size=100, leverage=1.0, pnl=500.0, pnl_pct=0.0333, exit_reason="signal", holding_bars=2, commission=2.0, entry_margin=15000.0, exit_margin=15500.0 - EquitySnapshot:timestamp="2024-01-02 10:00:00", capital=985000.0, unrealized=500.0, equity=1000000.0, positions=1

[本节为概念性示例,不直接引用具体源码]

依赖关系分析

graph LR BE["BaseEngine"] --> M["models.py"] MET["metrics.py"] --> M PJ["trade_journal_parsers.py"] --> M ENT["entities/models.py"] -. 设计参考 .-> M

图表来源 - agent/backtest/engines/base.py:377-800 - agent/backtest/models.py:13-118 - agent/backtest/metrics.py:1-200 - agent/src/entities/models.py:141-397 - agent/src/tools/trade_journal_parsers.py:63-88

章节来源 - agent/backtest/engines/base.py:377-800 - agent/backtest/models.py:13-118 - agent/backtest/metrics.py:1-200 - agent/src/entities/models.py:141-397 - agent/src/tools/trade_journal_parsers.py:63-88

性能考量

[本节提供通用指导,不直接分析具体文件]

故障排查指南

常见问题与定位方法: - 价格异常(零或负数):检查 can_execute 与 prospective_fill_price 的逻辑,确认是否允许非正价格交易。 - 时间错位:核对 entry_time 与 exit_time 的顺序,确保 holding_bars 与实际K线数一致。 - 费用与保证金不一致:核对 calc_commission 与 _calc_margin 的实现,确保与交易所规则匹配。 - 指标异常:检查 metrics.py 中年化因子映射是否正确,确认 bars_per_year 与 interval/source 匹配。

章节来源 - agent/backtest/engines/base.py:377-800 - agent/backtest/metrics.py:1-200

结论

Vibe-Trading 的数据模型以不可变数据类为核心,通过 Position、FillRecord、TradeRecord、EquitySnapshot 构建起从执行到绩效的完整证据链。该设计确保了回测过程的可追溯性、可复现性与高性能。结合 BaseEngine 的运行循环与 metrics 的指标计算,形成了稳健的回测基础设施。遵循本文档的扩展指南与最佳实践,可在不破坏现有契约的前提下平滑扩展新资产类别与业务规则。

[本节总结性内容,不直接分析具体文件]

附录:扩展指南与最佳实践

章节来源 - agent/backtest/engines/base.py:377-800 - agent/src/entities/models.py:141-397 - agent/backtest/metrics.py:1-200 - agent/src/tools/trade_journal_parsers.py:63-88