myTestFreqAI/tools/update_hyperopt_optimize.py
zhangkun9038@dingtalk.com cb68f5f10e 重构了client.go
2025-10-28 00:46:12 +08:00

107 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
根据指定阶段更新freqaiprimer.py中属性的optimize值
确保当前阶段的属性设置为optimize=True其他阶段的属性设置为optimize=False
"""
import os
import re
import sys
# 策略文件路径
STRATEGY_FILE = os.path.expanduser('~/myTestFreqAI/freqtrade/templates/freqaiprimer.py')
# 阶段属性映射
PHASE_PROPERTIES = {
1: [
'bb_std',
'rsi_length',
'bb_lower_deviation',
'stochrsi_bull_threshold',
'volume_multiplier',
'min_condition_count'
],
2: [
'bb_length',
'rsi_oversold',
'rsi_bull_threshold',
'stochrsi_neutral_threshold',
'bb_width_threshold'
],
3: [
'h1_max_candles',
'h1_rapid_rise_threshold',
'h1_max_consecutive_candles',
'max_entry_adjustments',
'add_position_callback',
'adjust_multiplier'
],
4: [
'exit_bb_upper_deviation',
'exit_volume_multiplier',
'rsi_overbought',
'reduce_profit_base',
'reduce_coefficient',
'max_reduce_adjustments'
]
}
def update_optimize_values(phase):
"""更新指定阶段的optimize值"""
if phase not in PHASE_PROPERTIES:
print(f"错误: 无效的阶段 {phase}。阶段必须是 1-4 之间的整数。")
sys.exit(1)
# 读取文件内容
try:
with open(STRATEGY_FILE, 'r', encoding='utf-8') as f:
content = f.read()
except FileNotFoundError:
print(f"错误: 找不到文件 {STRATEGY_FILE}")
sys.exit(1)
except Exception as e:
print(f"读取文件时出错: {e}")
sys.exit(1)
# 获取当前阶段的属性列表
current_phase_properties = PHASE_PROPERTIES[phase]
# 构建所有属性的集合
all_properties = set()
for props in PHASE_PROPERTIES.values():
all_properties.update(props)
# 更新每个属性的optimize值
for prop in all_properties:
# 确定当前属性是否应该设置为optimize=True
should_optimize = prop in current_phase_properties
# 构建正则表达式来匹配属性定义行
# 匹配格式如: prop_name = TypeParameter(..., optimize=..., ...)
pattern = rf'({prop}\s*=\s*\w+Parameter\([^)]*)optimize=\w+([^)]*\))'
# 替换optimize值
replacement = rf'\1optimize={str(should_optimize).lower()}\2'
content = re.sub(pattern, replacement, content)
# 写回文件
try:
with open(STRATEGY_FILE, 'w', encoding='utf-8') as f:
f.write(content)
print(f"成功更新阶段 {phase} 的optimize值")
print(f"当前阶段属性: {', '.join(current_phase_properties)}")
except Exception as e:
print(f"写入文件时出错: {e}")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"用法: {sys.argv[0]} <阶段编号(1-4)>")
sys.exit(1)
try:
phase = int(sys.argv[1])
update_optimize_values(phase)
except ValueError:
print("错误: 阶段必须是整数")
sys.exit(1)