release: v1.17.9 — 修复回测交易统计离谱数值(前后端口径错配 + 持仓天数跨月放大)

- 交易盈亏指标改为收益率口径(avg_win/loss/max_win/loss = pnl/cost_basis)
- 平均持仓天数改用真实日历日相减(原 YYYYMMDD 整数差跨月放大)
- 盈亏比无亏损时记 999.0(原 0.0,与 100% 胜率自相矛盾)
- 新增 Trade.cost_basis 字段 + engine 填充
- 3 个回归守卫;870 单测全绿,ruff/mypy strict/前端 vue-tsc 通过
This commit is contained in:
Justin Gu
2026-07-04 03:55:05 +08:00
parent f7cb015ec6
commit 57cad1f3db
6 changed files with 178 additions and 33 deletions
+4
View File
@@ -437,6 +437,8 @@ class BacktestEngine:
if position_size > 0:
avg_cost = position_cost / position_size
trade.pnl = (trade.price - avg_cost) * trade.size - trade.commission
# 记录本次卖出对应的持仓成本基数,用于派生单笔收益率
trade.cost_basis = avg_cost * trade.size
position_cost -= avg_cost * trade.size
position_size -= trade.size
else:
@@ -463,6 +465,7 @@ class BacktestEngine:
"commission",
"slippage",
"pnl",
"cost_basis",
"rejected",
]
)
@@ -476,6 +479,7 @@ class BacktestEngine:
"commission": t.commission,
"slippage": t.slippage,
"pnl": t.pnl,
"cost_basis": t.cost_basis,
"rejected": t.rejected,
}
for t in trades