mirror of
https://github.com/ZhuLinsen/daily_stock_analysis
synced 2026-09-20 10:53:33 +08:00
Add a complete backtest/evaluation system that measures the accuracy of
AI-generated stock analysis recommendations against actual market outcomes.
Backend:
- Backtest engine (src/core/backtest_engine.py) with direction inference,
stop-loss/take-profit simulation, and outcome classification (win/loss/neutral)
- Repository layer (src/repositories/backtest_repo.py) with SQLite persistence
for backtest_results and backtest_summaries tables
- Service layer (src/services/backtest_service.py) orchestrating evaluation runs
with configurable window days, neutral band, and min-age filters
- REST API endpoints: POST /run, GET /results, GET /performance, GET /performance/{code}
- Pydantic schemas for request/response validation
Frontend (apps/dsa-web):
- New Backtest page with performance dashboard sidebar showing direction
accuracy, win rate, simulated returns, SL/TP trigger rates, and W/L/N counts
- Paginated results table with outcome badges, direction indicators, and
color-coded return percentages
- Stock code filter and one-click "Run Backtest" trigger
- Full TypeScript types and API client matching backend schemas
Direction mapping fix:
- "Wait/observe" (观望) advice now maps to direction_expected="down" instead
of "flat", correctly reflecting that "wait" means "stay out due to downside
risk" rather than predicting a flat market
Tests:
- 21 unit tests covering engine logic, service orchestration, and summary
aggregation (all passing)
Docs:
- Updated README, full-guide, and translations with backtest feature docs
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Unit tests for BacktestEngine.compute_summary()."""
|
|
|
|
import unittest
|
|
from dataclasses import dataclass
|
|
|
|
from src.core.backtest_engine import BacktestEngine
|
|
|
|
|
|
@dataclass
|
|
class FakeRow:
|
|
eval_status: str = "completed"
|
|
position_recommendation: str = "long"
|
|
outcome: str = "win"
|
|
direction_correct: bool | None = True
|
|
stock_return_pct: float | None = 1.0
|
|
simulated_return_pct: float | None = 1.0
|
|
hit_stop_loss: bool | None = False
|
|
hit_take_profit: bool | None = False
|
|
first_hit: str | None = "neither"
|
|
first_hit_trading_days: int | None = None
|
|
operation_advice: str | None = "买入"
|
|
|
|
|
|
class BacktestSummaryTestCase(unittest.TestCase):
|
|
def test_trigger_rates_use_applicable_denominators(self) -> None:
|
|
# One row has stop-loss configured, one row doesn't.
|
|
rows = [
|
|
FakeRow(hit_stop_loss=True, hit_take_profit=None, first_hit="stop_loss"),
|
|
FakeRow(hit_stop_loss=None, hit_take_profit=True, first_hit="take_profit"),
|
|
]
|
|
|
|
summary = BacktestEngine.compute_summary(
|
|
results=rows,
|
|
scope="stock",
|
|
code="600519",
|
|
eval_window_days=3,
|
|
engine_version="v1",
|
|
)
|
|
|
|
# stop_loss_trigger_rate denominator should be 1 (only applicable row)
|
|
self.assertEqual(summary["stop_loss_trigger_rate"], 100.0)
|
|
|
|
# take_profit_trigger_rate denominator should be 1 (only applicable row)
|
|
self.assertEqual(summary["take_profit_trigger_rate"], 100.0)
|
|
|
|
# ambiguous_rate denominator should be 2 (any target applicable)
|
|
self.assertEqual(summary["ambiguous_rate"], 0.0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|
|
|