feat(backtest): 接真实 CTA 策略跑通端到端回测(DoubleMaStrategy on 600000)

修复 cta_engine 在真数据上的多个 bug(Phase 2 未在真数据验证):
- interval "1d" -> "d"(vnpy Interval.DAILY.value)
- capital 0 -> 1_000_000(0 致首笔交易即爆仓,统计全 0)
- statistics 改用 calculate_statistics(df)(旧代码误用 calculate_result 拿 DataFrame)
- statistics JSON-safe(vnpy 可能含 Timestamp)
- test_cta_engine mock 匹配新流程(calculate_statistics 返回统计字典)

验证:diag_cta.py 真实回测 DoubleMaStrategy on 600000 (2024H1, 111 天)
→ 真实统计 total_return -0.017% / sharpe -1.03 / max_drawdown -2.17 / 1 trade
容器 79 tests passed。
This commit is contained in:
2026-07-06 23:33:24 +08:00
parent 1174063d54
commit cb220619ef
3 changed files with 67 additions and 6 deletions
+5 -2
View File
@@ -15,9 +15,11 @@ class TestRunCtaBacktest:
mock_strategy_class = Mock()
mock_strategy_class.__name__ = "TestStrategy"
# Mock BacktestingEngine
# Mock BacktestingEngine — calculate_result() returns daily_df (DataFrame),
# calculate_statistics(df) returns the stats dict (vnpy API, matches cta_engine)
mock_engine = MagicMock()
mock_engine.calculate_result.return_value = {
mock_engine.calculate_result.return_value = MagicMock(name="daily_df")
mock_engine.calculate_statistics.return_value = {
"total_return": 0.15,
"sharpe_ratio": 1.2,
"max_drawdown": -0.08,
@@ -68,6 +70,7 @@ class TestRunCtaBacktest:
mock_engine.load_data.assert_called_once()
mock_engine.run_backtesting.assert_called_once()
mock_engine.calculate_result.assert_called_once()
mock_engine.calculate_statistics.assert_called_once()
def test_run_cta_backtest_handles_exception(self, temp_db_path):
"""Test that exceptions during backtest are handled properly."""