freqai同时优化回归和分类参数

This commit is contained in:
zhangkun9038@dingtalk.com 2025-08-17 02:42:04 +08:00
parent 2728881fe3
commit e3d26a9bd2
3 changed files with 30 additions and 20 deletions

View File

@ -481,17 +481,12 @@ class FreqaiPrimer(IStrategy):
k_sell = self.linear_map(market_trend_score, 0, 100, 1.5, 1.0)
# 处理分类模型的预测值
classification_columns = [col for col in dataframe.columns if "&*-optimal_first_length" in col or "optimal_first_length" in col]
if classification_columns:
for col in classification_columns:
if col in dataframe.columns:
# 重命名分类预测列以便后续使用
dataframe["optimal_first_length_pred"] = dataframe[col]
logger.info(f"[{pair}] 找到分类模型预测列: {col}, 重命名为 optimal_first_length_pred")
logger.info(f"[{pair}] 分类预测值统计: {dataframe[col].value_counts().to_dict()}")
break
if "&*-optimal_first_length" in dataframe.columns:
dataframe["optimal_first_length_pred"] = dataframe["&*-optimal_first_length"]
logger.info(f"[{pair}] ✅ 找到并复制分类模型预测列: &*-optimal_first_length -> optimal_first_length_pred")
logger.info(f"[{pair}] 分类预测值统计: {dataframe['&*-optimal_first_length'].value_counts().to_dict()}")
else:
logger.warning(f"[{pair}] 未找到分类模型预测列,将使用默认值")
logger.warning(f"[{pair}] 未找到分类模型预测列 &*-optimal_first_length")
self.buy_threshold = labels_mean - k_buy * labels_std
self.sell_threshold = labels_mean + k_sell * labels_std
@ -1363,17 +1358,29 @@ class FreqaiPrimer(IStrategy):
if "optimal_first_length_pred" in dataframe.columns:
predicted_value = dataframe["optimal_first_length_pred"].iloc[-1]
if pd.notna(predicted_value):
optimal_length_class = int(predicted_value)
# 处理浮点预测值,四舍五入到最近的整数类别
optimal_length_class = int(round(float(predicted_value)))
length_mapping = {0: 2, 1: 4, 2: 6, 3: 8, 4: 10}
first_length = length_mapping.get(optimal_length_class, 2)
logger.info(f"[{pair}] ✅ 分类模型预测成功: first_length={first_length} (类别: {optimal_length_class})")
logger.info(f"[{pair}] ✅ 分类模型预测成功: first_length={first_length} (原始值: {predicted_value}, 类别: {optimal_length_class})")
else:
first_length = 2 # 保持默认值为2
first_length = 2
logger.warning(f"[{pair}] ⚠️ 分类模型预测值为NaN使用默认值: {first_length}")
elif "&*-optimal_first_length" in dataframe.columns:
# 直接检查原始列
predicted_value = dataframe["&*-optimal_first_length"].iloc[-1]
if pd.notna(predicted_value):
optimal_length_class = int(round(float(predicted_value)))
length_mapping = {0: 2, 1: 4, 2: 6, 3: 8, 4: 10}
first_length = length_mapping.get(optimal_length_class, 2)
logger.info(f"[{pair}] ✅ 使用原始分类列: first_length={first_length} (原始值: {predicted_value}, 类别: {optimal_length_class})")
else:
first_length = 2
logger.warning(f"[{pair}] ⚠️ 原始分类列值为NaN使用默认值: {first_length}")
else:
first_length = 2 # 保持默认值为2
logger.warning(f"[{pair}] ⚠️ 分类模型列不存在,使用默认值: {first_length}")
first_length = 2
logger.warning(f"[{pair}] ⚠️ 未找到任何分类模型列,使用默认值: {first_length}")
# 根据 first_length 动态调整其他段长
second_length = first_length * 3
third_length = first_length * 5

View File

@ -7,9 +7,12 @@ echo "🚀 启动混合模型策略测试..."
echo "📊 使用模型: freqai_primer_mixed"
echo "📁 配置文件: test_mixed_models_config.json"
# 设置环境变量
export FREQTRADE__STRATEGY_NAME="freqaiprimer"
export FREQTRADE__CONFIG_PATH="/Users/zhangkun/myTestFreqAI/test_mixed_models_config.json"
# 清理旧模型和缓存
echo "🧹 清理旧模型和缓存..."
rm -rf /Users/zhangkun/myTestFreqAI/user_data/models/test58/
rm -rf /Users/zhangkun/myTestFreqAI/user_data/models/test_mixed_models/
rm -rf /Users/zhangkun/myTestFreqAI/user_data/models/freqai_primer_mixed
rm -rf /Users/zhangkun/myTestFreqAI/user_data/data/test_mixed_models
# 启动策略
python -m freqtrade trade \

View File

@ -26,7 +26,7 @@
],
"freqai": {
"enabled": true,
"identifier": "test_mixed_models",
"identifier": "freqai_primer_mixed",
"purge_old_models": 2,
"train_period_days": 15,
"backtest_period_days": 7,