70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
"""
|
|
BB(10, 2.5) 均值回归策略回测 — 2026/02/23 ~ 现在,本金 100U
|
|
完全复现 bb_trade.py 的参数: BB(10,2.5) | 50x | 1%权益/单 | 递增加仓 +1%/次 max=3
|
|
"""
|
|
import sys
|
|
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parents[1]))
|
|
|
|
import numpy as np
|
|
from pathlib import Path
|
|
from datetime import datetime, timedelta
|
|
|
|
from strategy.bb_backtest import BBConfig, run_bb_backtest
|
|
from strategy.data_loader import load_klines
|
|
|
|
# 加载 2026-02-23 至今天的数据 (end_date 不包含,用明天确保含今天)
|
|
today = datetime.now().strftime("%Y-%m-%d")
|
|
END_DATE = (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d")
|
|
df = load_klines('5m', '2026-02-23', END_DATE)
|
|
print(f"数据: 2026-02-23 ~ {today}, 共 {len(df):,} 根 5 分钟 K 线")
|
|
|
|
# 配置:完全匹配 bb_trade.py D方案
|
|
cfg = BBConfig(
|
|
bb_period=10,
|
|
bb_std=2.5,
|
|
leverage=50,
|
|
initial_capital=100.0, # 本金 100U
|
|
margin_pct=0.01, # 1% 权益/单
|
|
pyramid_enabled=True,
|
|
pyramid_step=0.01, # 递增加仓 +1%/次
|
|
pyramid_max=3,
|
|
max_daily_loss=50.0,
|
|
fee_rate=0.0005,
|
|
rebate_rate=0.0,
|
|
rebate_pct=0.90,
|
|
rebate_hour_utc=0,
|
|
)
|
|
|
|
r = run_bb_backtest(df, cfg)
|
|
|
|
# 结果
|
|
d = r.daily_stats
|
|
pnl = d["pnl"].astype(float)
|
|
eq = d["equity"].astype(float)
|
|
final = float(eq.iloc[-1])
|
|
dd = float((eq - eq.cummax()).min())
|
|
ret_pct = (final - cfg.initial_capital) / cfg.initial_capital * 100
|
|
nt = len(r.trades)
|
|
wr = sum(1 for t in r.trades if t.net_pnl > 0) / max(nt, 1) * 100
|
|
|
|
print("\n" + "=" * 60)
|
|
print(" 回测结果 (BB 均值回归 | 2026/02/23 ~ 现在 | 本金 100U)")
|
|
print("=" * 60)
|
|
print(f" 最终权益: {final:,.2f} U")
|
|
print(f" 收益: {final - cfg.initial_capital:+,.2f} U ({ret_pct:+.1f}%)")
|
|
print(f" 最大回撤: {dd:+,.2f} U")
|
|
print(f" 交易次数: {nt}")
|
|
print(f" 胜率: {wr:.1f}%")
|
|
print(f" 总手续费: {r.total_fee:.2f} U")
|
|
print(f" 总返佣: {r.total_rebate:.2f} U")
|
|
print("=" * 60)
|
|
|
|
# 打印每日收益
|
|
if len(d) > 1:
|
|
print("\n每日收益:")
|
|
for idx, row in d.iterrows():
|
|
day_str = idx.strftime("%Y-%m-%d") if hasattr(idx, 'strftime') else str(idx)[:10]
|
|
p = row["pnl"]
|
|
e = row["equity"]
|
|
print(f" {day_str}: PnL {p:+.2f} U, 权益 {e:.2f} U")
|