Skip to content

Latest commit

 

History

History
383 lines (278 loc) · 9.86 KB

File metadata and controls

383 lines (278 loc) · 9.86 KB

NostalgiaForInfinityX7 Hyperopt优化实施指南

📋 概述

本指南提供了NostalgiaForInfinityX7策略的完整Hyperopt参数优化系统。通过4阶段渐进式优化方法,在24个月历史数据上优化买入条件、卖出条件、保护参数和ROI表。

目标: 最大化总收益率,同时控制风险和提升稳定性。

🎯 4阶段优化计划

阶段1: 保护参数优化 (优先级: 最高)

目标: 优化止损阈值,建立风险控制基线

参数 (6个):

  • stop_threshold_spot: 现货止损阈值 (0.05-0.20, 默认0.10)
  • stop_threshold_futures: 期货止损阈值 (0.05-0.20, 默认0.10)
  • stop_threshold_rapid_spot: 快速模式现货止损 (0.10-0.30, 默认0.20)
  • stop_threshold_rapid_futures: 快速模式期货止损 (0.10-0.30, 默认0.20)
  • stop_threshold_scalp_spot: Scalp模式现货止损 (0.10-0.30, 默认0.20)
  • stop_threshold_scalp_futures: Scalp模式期货止损 (0.10-0.30, 默认0.20)

配置:

  • Epochs: 200
  • 损失函数: SharpeHyperOptLossDaily
  • 优化空间: protection
  • 预计时间: 2-4小时

执行:

bash scripts/run_hyperopt_phase1.sh

阶段2: Grinding参数优化 (优先级: 高)

目标: 优化DCA分层加仓机制,提升收益

参数 (24个):

  • Grind 1-6: 每级包含
    • stop_grinds: 停止磨削阈值 (-0.80至-0.30, 默认-0.50)
    • profit_threshold: 利润目标 (0.010-0.030, 默认0.018)

配置:

  • Epochs: 500
  • 损失函数: OnlyProfitHyperOptLoss
  • 优化空间: grinding
  • 预计时间: 8-12小时

执行:

bash scripts/run_hyperopt_phase2.sh

阶段3: 入场信号优化 (优先级: 中)

目标: 优化信号组合,提升胜率

参数 (35个):

  • 27个多头条件开关: long_entry_condition_{1-6,21,41-46,61-63,101-104,120,121,141-145,161-163}_enable
  • 8个空头条件开关: short_entry_condition_{501,502,541-546}_enable

配置:

  • Epochs: 300
  • 损失函数: SharpeHyperOptLossDaily
  • 优化空间: buy sell
  • 预计时间: 6-8小时

执行:

bash scripts/run_hyperopt_phase3.sh

阶段4: ROI表优化 (优先级: 低)

目标: 微调退出时机

配置:

  • Epochs: 100
  • 损失函数: SharpeHyperOptLossDaily
  • 优化空间: roi
  • 预计时间: 1-2小时
  • 注意: X7使用 ignore_roi_if_entry_signal=True,ROI影响较小

执行:

bash scripts/run_hyperopt_phase4.sh

📁 文件结构

NostalgiaForInfinity/
├── NostalgiaForInfinityX7Hyperopt.py          # ✨ 新建: Hyperopt策略类
├── configs/
│   └── hyperopt-x7.json                       # ✨ 新建: Hyperopt配置
├── scripts/
│   ├── run_hyperopt_phase1.sh                 # ✨ 新建: 阶段1执行脚本
│   ├── run_hyperopt_phase2.sh                 # ✨ 新建: 阶段2执行脚本
│   ├── run_hyperopt_phase3.sh                 # ✨ 新建: 阶段3执行脚本
│   └── run_hyperopt_phase4.sh                 # ✨ 新建: 阶段4执行脚本
└── user_data/
    └── hyperopts/
        ├── __init__.py                        # ✨ 新建
        └── loss_functions/
            ├── __init__.py                    # ✨ 新建
            └── profit_drawdown_ratio.py       # ✨ 新建: 自定义损失函数
    └── hyperopt_results/
        ├── phase1_results/                    # ✨ 新建: 阶段1结果
        ├── phase2_results/                    # ✨ 新建: 阶段2结果
        ├── phase3_results/                    # ✨ 新建: 阶段3结果
        └── phase4_results/                    # ✨ 新建: 阶段4结果

🚀 快速开始

前置要求

  1. Freqtrade环境: 已安装并配置
  2. 历史数据: 下载24个月数据 (2024-01-01 至 2026-01-01)
  3. 交易对列表: 配置 configs/pairlist-backtest-static-binance-spot-usdt.json

执行步骤

步骤1: 执行阶段1优化

bash scripts/run_hyperopt_phase1.sh

完成后,导出最佳参数:

freqtrade hyperopt-show --best --print-json > user_data/hyperopt_results/phase1_results/phase1_best_params.json

步骤2: 执行阶段2优化

bash scripts/run_hyperopt_phase2.sh

完成后,导出最佳参数:

freqtrade hyperopt-show --best --print-json > user_data/hyperopt_results/phase2_results/phase2_best_params.json

步骤3: 执行阶段3优化

bash scripts/run_hyperopt_phase3.sh

完成后,导出最佳参数:

freqtrade hyperopt-show --best --print-json > user_data/hyperopt_results/phase3_results/phase3_best_params.json

步骤4: 执行阶段4优化

bash scripts/run_hyperopt_phase4.sh

完成后,导出最佳参数:

freqtrade hyperopt-show --best --print-json > user_data/hyperopt_results/phase4_results/phase4_best_params.json

📊 验证和回测

完整回测验证

使用最终优化参数进行完整回测:

freqtrade backtesting \
  --strategy NostalgiaForInfinityX7 \
  --config configs/exampleconfig.json \
  --config user_data/hyperopt_results/phase4_results/phase4_best_params.json \
  --timerange 20240101-20250101 \
  --breakdown month

Walk-Forward验证

分割数据进行Walk-Forward分析,验证参数在未见数据上的表现:

# 训练期: 2024-01 至 2024-12 (已用于优化)
# 测试期: 2025-01 至 2026-01 (未见数据)

freqtrade backtesting \
  --strategy NostalgiaForInfinityX7 \
  --config configs/exampleconfig.json \
  --config user_data/hyperopt_results/phase4_results/phase4_best_params.json \
  --timerange 20250101-20260101 \
  --breakdown month

✅ 成功标准

优化成功的判断标准:

  • ✅ 所有4个阶段成功完成
  • ✅ 每阶段至少找到50个有效交易的参数组合
  • ✅ 最终回测总收益率 > 50%
  • ✅ Sharpe比率 > 1.5
  • ✅ Walk-Forward测试期收益率 > 训练期的70%
  • ✅ 最大回撤 < 30%
  • ✅ 参数在合理范围内(无极端值)

🔍 关键指标

回测指标

指标 目标值 说明
总收益率 > 50% 24个月总收益
Sharpe比率 > 1.5 风险调整收益
最大回撤 < 30% 最大亏损幅度
胜率 > 45% 盈利交易比例
交易次数 > 200 足够的样本量
平均交易时长 - 监控持仓时间

参数约束

  1. Spot/Futures对称性: 期货止损不应超过现货1.5倍
  2. Grinding阈值递减: sub_thresholds必须递减
  3. Stakes总和限制: 单个Grind的stakes总和 ≤ 2.0
  4. 最小交易次数: 每个优化至少50笔交易

⚠️ 风险和注意事项

技术风险

  1. Freqtrade未安装: 需要先安装Freqtrade环境
  2. 历史数据缺失: 需要下载24个月完整数据
  3. 计算资源: 全部4阶段预计需要20-30小时计算时间
  4. 内存占用: 大规模Hyperopt可能需要16GB+内存

优化陷阱

  1. 过拟合: 参数过度拟合历史数据,未来表现差

    • 缓解: Walk-Forward验证,保守参数范围
  2. 维度灾难: 同时优化过多参数导致搜索空间爆炸

    • 缓解: 分4阶段渐进优化
  3. 数据窥探: 多次优化同一数据集

    • 缓解: 保留独立测试集
  4. 市场环境变化: 历史最优参数未来失效

    • 缓解: 定期重新优化(每季度)

📈 预期结果

性能提升目标

  • 总收益率提升: 20-40%
  • Sharpe比率提升: 0.3-0.5
  • 最大回撤降低: 5-10%
  • 胜率提升: 3-5%

输出文件

  1. 最佳参数JSON: 每阶段的最优参数配置
  2. Hyperopt日志: 完整的优化过程记录
  3. 回测报告: 优化前后对比报告
  4. 参数分析: 参数敏感性分析图表

🔄 后续维护

定期重优化

  • 频率: 每季度或市场环境显著变化时
  • 数据窗口: 滚动使用最近12-24个月数据
  • 增量优化: 仅优化表现下降的参数组

参数监控

  • 监控实盘参数与优化参数的偏离
  • 跟踪关键指标(收益率、回撤、胜率)
  • 设置预警阈值,触发重优化

版本管理

  • 为每次优化创建Git分支
  • 标记优化日期和数据范围
  • 保留历史最佳参数配置

📚 常用命令

查看Hyperopt结果

# 查看最佳结果 (前10个)
freqtrade hyperopt-show --best -n 10

# 查看所有结果
freqtrade hyperopt-show

# 查看特定结果
freqtrade hyperopt-show --best -n 1 --print-json

导出参数

# 导出最佳参数到JSON
freqtrade hyperopt-show --best --print-json > best_params.json

# 导出特定Epoch的参数
freqtrade hyperopt-show --epoch 42 --print-json > epoch_42_params.json

清理Hyperopt数据

# 删除所有Hyperopt结果
rm -rf user_data/hyperopt_results/

# 删除Hyperopt数据库
rm -f user_data/hyperopt_results.pickle

🆘 故障排除

问题1: Freqtrade命令未找到

解决方案:

# 激活Freqtrade虚拟环境
source /path/to/freqtrade/venv/bin/activate

# 或使用完整路径
/path/to/freqtrade/venv/bin/freqtrade hyperopt ...

问题2: 历史数据不足

解决方案:

# 下载历史数据
freqtrade download-data \
  --exchange binance \
  --pairs USDT_BTC USDT_ETH \
  --timerange 20240101-20260101 \
  --timeframe 5m

问题3: 内存不足

解决方案:

  • 减少 --jobs 参数值
  • 减少 --epochs 参数值
  • 使用更小的交易对列表

问题4: Hyperopt速度慢

解决方案:

  • 增加 --jobs 参数值(使用更多CPU核心)
  • 减少 --epochs 参数值
  • 使用更快的损失函数

📞 支持和反馈

如有问题或建议,请参考:

📝 更新日志

v1.0 (2026-01-27)

  • ✨ 初始实施
  • 🎯 4阶段优化框架
  • 📊 自定义损失函数
  • 🚀 自动化执行脚本

最后更新: 2026-01-27 版本: 1.0 状态: 生产就绪 ✅