7 Sprints 全部完成: Sprint 0: 基础设施 (DataManager + MariaDB) Sprint 1: 因子引擎 (34因子/12分类) Sprint 2: VectorBT 回测 (5策略+截面) Sprint 3: Optuna 优化 (+Walk-Forward) Sprint 4: ML 模型 (LightGBM+CatBoost) Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐) Sprint 6: Agent 系统 (4Agent+日报.md/.html) 生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping, save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复, RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4, CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化, indexDatas API修正 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
60 lines
2.3 KiB
Markdown
60 lines
2.3 KiB
Markdown
# CLAUDE-backtest.md — 回测引擎 + 参数优化
|
||
|
||
## VectorBTEngine (`finance/backtest/vectorbt/engine.py`)
|
||
|
||
只做多,10万/万三。
|
||
|
||
```python
|
||
from backtest.vectorbt.engine import VectorBTEngine
|
||
engine_bt = VectorBTEngine(initial_capital=100_000, commission=0.0003)
|
||
|
||
report = engine_bt.run(strategy, price_df, factor_df)
|
||
# → BacktestReport
|
||
report = engine_bt.run_cross_section(strategy, price_univ, factor_univ)
|
||
```
|
||
|
||
信号流:`1=buy, 0=sell, -1=hold` → `_signals_to_entries` → vbt.Portfolio.from_signals(direction="longonly")。
|
||
|
||
## 策略 (`finance/backtest/strategies/`)
|
||
|
||
| 策略 | 参数 | 逻辑 |
|
||
|------|------|------|
|
||
| `SMACrossStrategy` | fast=5, slow=20 | 金叉买/死叉卖 |
|
||
| `RSIMeanRevertStrategy` | oversold=30, overbought=70 | 超卖买/超买卖 |
|
||
| `MomentumBreakoutStrategy` | lookback=20, exit=10 | 新高买/跌破卖 |
|
||
| `FactorCrossStrategy` | factor_column, buy/sell_threshold | 阈值交叉(通用) |
|
||
| `FactorRotationStrategy` | factor_name, top_n=5 | 排序选股 |
|
||
|
||
## 自定义策略
|
||
|
||
继承 `backtest/base.py:BaseStrategy`,实现 `generate_signals(factor_df) → pd.Series`。
|
||
|
||
## 信号工具 (`backtest/signal.py`)
|
||
|
||
```python
|
||
factor_to_threshold_signal(series, buy, sell, direction)
|
||
cross_signal(fast, slow) # 金叉/死叉
|
||
factor_to_quantile_signal(...) # 分位数信号
|
||
```
|
||
|
||
## BacktestReport (`backtest/report.py`)
|
||
|
||
字段:total_return, cagr, max_drawdown, sharpe_ratio, calmar_ratio, annual_volatility, win_rate, profit_factor, total_trades, avg_hold_days, best/worst_trade_pct, equity_curve, drawdown_curve, monthly_returns, trades_df, stats_dict。`summary()` 一行摘要。
|
||
|
||
## OptunaEngine (`finance/optimizer/engine.py`)
|
||
|
||
```python
|
||
from optimizer.engine import OptunaEngine
|
||
from optimizer.space import rsi_revert_space
|
||
|
||
opt = OptunaEngine(bt_engine)
|
||
result = opt.optimize(StrategyClass, space, price_df, factor_df, metric="sharpe", n_trials=200)
|
||
# → OptimizationResult(best_params, best_value, best_report, trial_df, param_importance)
|
||
|
||
wf = opt.optimize_walk_forward(StrategyClass, space, price_df, factor_df,
|
||
train_window=756, test_window=252)
|
||
```
|
||
|
||
预置空间:`sma_cross_space`, `rsi_revert_space`, `momentum_breakout_space`, `factor_cross_space`(`optimizer/space.py`)。
|
||
目标指标:sharpe/cagr/calmar/total_return/return_over_dd。
|