feat(backtest): add backtest engine with evaluation pipeline and WebUI (#269)

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
This commit is contained in:
[ZE]
2026-02-08 07:49:50 +01:00
committed by GitHub
parent 428f5371d2
commit dbe1cd7cae
27 changed files with 2972 additions and 11 deletions

View File

@@ -38,6 +38,7 @@
| 分析 | 多維度分析 | 技術面 + 籌碼分布 + 輿情情報 + 實時行情 |
| 市場 | 全球市場 | 支援 A股、港股、美股 |
| 復盤 | 大盤復盤 | 每日市場概覽、板塊漲跌、北向資金 |
| 回測 | AI 回測驗證 | 自動評估歷史分析準確率,方向勝率、止盈止損命中率 |
| 推送 | 多渠道通知 | Telegram、Discord、郵件、企業微信、飛書等 |
| 自動化 | 定時運行 | GitHub Actions 定時執行,無需伺服器 |
@@ -203,6 +204,7 @@
- 📝 **配置管理** - 查看/修改自選股列表
- 🚀 **快速分析** - 通過 API 接口觸發分析
- 📊 **實時進度** - 分析任務狀態實時更新,支持多任務並行
- 📈 **回測驗證** - 評估歷史分析準確率,查詢方向勝率與模擬收益
### API 接口
@@ -212,6 +214,10 @@
| `/api/v1/analysis/tasks` | GET | 查詢任務列表 |
| `/api/v1/analysis/status/{task_id}` | GET | 查詢任務狀態 |
| `/api/v1/history` | GET | 查詢分析歷史記錄 |
| `/api/v1/backtest/run` | POST | 觸發回測 |
| `/api/v1/backtest/results` | GET | 查詢回測結果(分頁) |
| `/api/v1/backtest/performance` | GET | 獲取整體回測表現 |
| `/api/v1/backtest/performance/{code}` | GET | 獲取單股回測表現 |
| `/api/health` | GET | 健康檢查 |
## 項目結構