- 删除 11 个残留文件: continuation.md, init_plan.md, reasonix.toml, djapi/continuation.md, djapi/.serena/, djapi/.claude/, djapi/.mcp.json, .claude/skills/, docs/usage.html, docs/db_schema.md, docs/report_db_design.md - 7 个 CLAUDE-*.md 移入 docs/ 并重命名去 CLAUDE- 前缀 - 新增 4 个文档: architecture.md, development.md, api.md, deployment.md - 重写 usage.md, README.md - 修复所有过时引用和交叉链接
60 lines
2.3 KiB
Markdown
60 lines
2.3 KiB
Markdown
# 回测引擎 + 参数优化
|
||
|
||
## 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。
|