fix(backtest): 结果页指标全显"—" + 任务重启后404
两个根因:
1. relative_metrics 缺字段(routes.py): get_result 的 relative_fields 列表
漏了 total_return/annual_return/sharpe_ratio/max_drawdown 4 个字段, 导致
这 4 个指标永远进不了响应 → 前端 MetricCards 显"—"。补全为 11 字段。
2. 任务 task_id 不持久(runner/cta_engine): submit_cta 用 cta_{symbol}_{id(params)}
(内存地址) 作 runner id, 而 run_cta_backtest 内部另生成 cta_{uuid} 存 DB,
两者持久层不相交 → 重启后 pool 内存映射丢失, get_result(runner_id) 的
load_result_by_task_id 查不到 → /result 404 → 前端指标全 0 + 图表无数据。
改为 submit 前置生成稳定 uuid, 透传给 run_cta_backtest 复用, 使
runner-id == DB task_id (单一 id, 重启可查)。
附: Dashboard 最近任务"策略/因子"列 min-width 150→190(长策略名不再截断)
测试: test_runner task_id 断言同步新格式
This commit is contained in:
@@ -48,7 +48,7 @@ def guess_exchange(symbol: str) -> Exchange:
|
||||
return Exchange("SSE")
|
||||
|
||||
|
||||
def run_cta_backtest(strategy_class, symbol: str, params: dict, start: str, end: str, cfg, db_path: str, benchmark: str = "hs300") -> BacktestResult:
|
||||
def run_cta_backtest(strategy_class, symbol: str, params: dict, start: str, end: str, cfg, db_path: str, benchmark: str = "hs300", task_id: str | None = None) -> BacktestResult:
|
||||
"""
|
||||
Run CTA strategy backtest using vnpy_ctastrategy BacktestingEngine.
|
||||
|
||||
@@ -60,12 +60,16 @@ def run_cta_backtest(strategy_class, symbol: str, params: dict, start: str, end:
|
||||
end: Backtest end date (YYYY-MM-DD format)
|
||||
cfg: Configuration object (may contain data paths)
|
||||
db_path: SQLite database path for saving results
|
||||
benchmark: Benchmark code (hs300/zz500)
|
||||
task_id: Optional task ID from runner (reused as the persisted task_id so
|
||||
runner-id == DB task_id; if omitted a fresh uuid is generated).
|
||||
|
||||
Returns:
|
||||
BacktestResult: Result object with backtest statistics and status
|
||||
"""
|
||||
# Generate unique task ID
|
||||
task_id = f"cta_{uuid.uuid4().hex[:8]}"
|
||||
# Use runner-provided task_id (durable, single id across pool/DB/URL) or generate
|
||||
if not task_id:
|
||||
task_id = f"cta_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
try:
|
||||
# Lazy import of BacktestingEngine (local env may not have vnpy_ctastrategy)
|
||||
|
||||
Reference in New Issue
Block a user