myTestFreqAI/hyperopt_config_example.py
2025-08-13 20:14:19 +08:00

106 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""
Hyperopt 配置示例,用于优化新的趋势判定参数
使用方法:
1. 将此文件保存为 hyperopt_trend_params.py
2. 运行: freqtrade hyperopt --config config.json --strategy freqaiprimer --hyperopt hyperopt_trend_params
"""
from freqtrade.optimize.hyperopt_interface import IHyperOpt
from typing import Dict, Any
import logging
logger = logging.getLogger(__name__)
class TrendParamsHyperopt(IHyperOpt):
"""
优化趋势判定参数的 Hyperopt 类
"""
@staticmethod
def generate_roi_table(params: Dict) -> Dict[int, float]:
"""生成ROI表格"""
return {
0: params['roi_p1'] + params['roi_p2'] + params['roi_p3'],
params['roi_t3']: params['roi_p1'] + params['roi_p2'],
params['roi_t3'] + params['roi_t2']: params['roi_p1'],
params['roi_t3'] + params['roi_t2'] + params['roi_t1']: 0,
}
@staticmethod
def roi_space():
"""ROI参数空间"""
return {
'roi_p1': [0.01, 0.08],
'roi_p2': [0.01, 0.08],
'roi_p3': [0.01, 0.08],
'roi_t1': [10, 120],
'roi_t2': [10, 60],
'roi_t3': [10, 60],
}
@staticmethod
def stoploss_space():
"""止损参数空间"""
return {
'stoploss': [-0.5, -0.05],
}
@staticmethod
def trailing_space():
"""追踪止损参数空间"""
return {
'trailing_stop': [True, False],
'trailing_stop_positive': [0.005, 0.05],
'trailing_stop_positive_offset_p1': [0.005, 0.05],
'trailing_only_offset_is_reached': [True, False],
}
def buy_params_space(self):
"""买入参数空间,包含新的趋势判定阈值"""
return {
# 原有参数
'buy_threshold': [-0.05, -0.001],
'volume_z_score_min': [0.1, 2.0],
'rsi_max': [20, 60],
# 新的趋势判定阈值
'trend_final_bullish_threshold': [50, 90], # 上涨趋势阈值
'trend_final_bearish_threshold': [10, 50], # 下跌趋势阈值
}
def sell_params_space(self):
"""卖出参数空间"""
return {
'sell_threshold': [0.001, 0.08],
'rsi_min': [60, 90],
'stochrsi_max': [60, 95],
}
def generate_estimator(self, dimensions: list, **kwargs) -> Any:
"""生成优化器"""
from skopt import Optimizer
from skopt.space import Real, Integer
# 定义参数空间
space = [
Real(-0.5, -0.05, name='stoploss'),
Real(0.001, 0.08, name='sell_threshold'),
Real(-0.05, -0.001, name='buy_threshold'),
Real(0.1, 2.0, name='volume_z_score_min'),
Integer(20, 60, name='rsi_max'),
Integer(60, 90, name='rsi_min'),
Integer(60, 95, name='stochrsi_max'),
Integer(50, 90, name='trend_final_bullish_threshold'),
Integer(10, 50, name='trend_final_bearish_threshold'),
]
return Optimizer(space, base_estimator='ET', acq_func='EI', n_initial_points=10)
# 使用示例配置
if __name__ == "__main__":
print("趋势参数优化配置已加载")
print("支持的优化参数:")
print("- trend_final_bullish_threshold: 50-90")
print("- trend_final_bearish_threshold: 10-50")