From e27cde2e19875b599bed895b408ea603e02650c3 Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Tue, 29 Apr 2025 12:00:36 +0800 Subject: [PATCH] with v3 --- config_examples/config_freqai.okx.json | 9 +++-- freqtrade/templates/FreqaiExampleStrategy.py | 38 +++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/config_examples/config_freqai.okx.json b/config_examples/config_freqai.okx.json index a0d6d758..7290e2b9 100644 --- a/config_examples/config_freqai.okx.json +++ b/config_examples/config_freqai.okx.json @@ -77,7 +77,10 @@ "live_retrain_hours": 0, "feature_selection": { "method": "recursive_elimination", - "threshold": 0.01 + "threshold": 0.01, + "steps": 1, + "cv": 5, + "n_features_to_select": null }, "feature_parameters": { "include_timeframes": ["3m", "5m", "1h"], @@ -93,8 +96,8 @@ "random_state": 42 }, "model_training_parameters": { - "n_estimators": 200, - "learning_rate": 0.05, + "n_estimators": 100, + "learning_rate": 0.1, "max_depth": 5, "subsample": 0.8, "colsample_bytree": 0.8, diff --git a/freqtrade/templates/FreqaiExampleStrategy.py b/freqtrade/templates/FreqaiExampleStrategy.py index 133151b2..464495c9 100644 --- a/freqtrade/templates/FreqaiExampleStrategy.py +++ b/freqtrade/templates/FreqaiExampleStrategy.py @@ -72,18 +72,27 @@ class FreqaiExampleStrategy(IStrategy): } def featcaure_engineering_expand_all(self, dataframe: DataFrame, period: int, metadata: dict, **kwargs) -> DataFrame: - # 保留关键的技术指标 - dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14) + # 只计算必要的技术指标 + if len(dataframe) > 14: + dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14) + dataframe["ema_12"] = ta.EMA(dataframe, timeperiod=12) + dataframe["ema_26"] = ta.EMA(dataframe, timeperiod=26) + else: + dataframe["rsi"] = 50 + dataframe["ema_12"] = dataframe["close"] + dataframe["ema_26"] = dataframe["close"] # 确保 MACD 列被正确计算并保留 - try: + # 确保有足够的数据计算 MACD + if len(dataframe) >= 50: macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9) dataframe["macd"] = macd["macd"] dataframe["macdsignal"] = macd["macdsignal"] - except Exception as e: - logger.error(f"计算 MACD 列时出错:{str(e)}") - dataframe["macd"] = np.nan - dataframe["macdsignal"] = np.nan + dataframe["macdhist"] = macd["macdhist"] + else: + dataframe["macd"] = 0 + dataframe["macdsignal"] = 0 + dataframe["macdhist"] = 0 # 检查 MACD 列是否存在 if "macd" not in dataframe.columns or "macdsignal" not in dataframe.columns: @@ -148,9 +157,12 @@ class FreqaiExampleStrategy(IStrategy): label_period = self.freqai_info["feature_parameters"]["label_period_candles"] # 定义目标变量为未来价格变化百分比(连续值) - dataframe["up_or_down"] = ( - dataframe["close"].shift(-label_period) - dataframe["close"] - ) / dataframe["close"] + # 使用对数收益率作为目标变量 + dataframe["target"] = np.log( + dataframe["close"].shift(-label_period) / dataframe["close"] + ) + # 处理异常值 + dataframe["target"] = dataframe["target"].clip(-0.1, 0.1) # 数据清理:处理 NaN 和 Inf 值 dataframe["up_or_down"] = dataframe["up_or_down"].replace([np.inf, -np.inf], np.nan) @@ -225,9 +237,9 @@ class FreqaiExampleStrategy(IStrategy): # 简化动态参数生成逻辑 # 放松 buy_rsi 和 sell_rsi 的生成逻辑 - # 计算 buy_rsi_pred 并清理 NaN 值 - dataframe["buy_rsi_pred"] = dataframe["rsi"].rolling(window=10).mean().clip(30, 50) - dataframe["buy_rsi_pred"] = dataframe["buy_rsi_pred"].fillna(dataframe["buy_rsi_pred"].median()) + # 使用固定 RSI 阈值 + self.buy_rsi.value = 30 + self.sell_rsi.value = 70 # 计算 sell_rsi_pred 并清理 NaN 值 dataframe["sell_rsi_pred"] = dataframe["buy_rsi_pred"] + 20