:生成的特征可能不够稳定,导致新数据与训练数据差异过大
Some checks are pending
Update Docker Hub Description / dockerHubDescription (push) Waiting to run

This commit is contained in:
zhangkun9038@dingtalk.com 2025-04-28 13:01:27 +08:00
parent c3e4a73eb3
commit e3ffdd92e0

View File

@ -30,7 +30,7 @@ class FreqaiExampleStrategy(IStrategy):
# FreqAI 配置
freqai_info = {
"model": "LightGBMRegressor",
"model": "XGBoostRegressor", # 改用XGBoost
"feature_parameters": {
"include_timeframes": ["5m", "15m", "1h"],
"include_corr_pairlist": [],
@ -39,14 +39,25 @@ class FreqaiExampleStrategy(IStrategy):
},
"data_split_parameters": {
"test_size": 0.2,
"shuffle": False,
"shuffle": True, # 启用shuffle
},
"model_training_parameters": {
"n_estimators": 200, # 增加树的数量以提升模型复杂度
"learning_rate": 0.05, # 降低学习率以避免过拟合
"num_leaves": 50, # 增加叶子节点数量以捕捉更多细节
"verbose": -1,
"n_estimators": 100, # 减少树的数量
"learning_rate": 0.1, # 提高学习率
"max_depth": 6, # 限制树深度
"subsample": 0.8, # 添加子采样
"colsample_bytree": 0.8, # 添加特征采样
"objective": "reg:squarederror",
"eval_metric": "rmse",
"early_stopping_rounds": 20,
"verbose": 0,
},
"data_kitchen": {
"feature_parameters": {
"DI_threshold": 1.5, # 降低异常值过滤阈值
"use_DBSCAN_to_remove_outliers": False # 禁用DBSCAN
}
}
}
plot_config = {
@ -61,15 +72,20 @@ class FreqaiExampleStrategy(IStrategy):
}
def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, metadata: dict, **kwargs) -> DataFrame:
# 添加更多稳定的特征
dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period)
dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period)
dataframe["%-sma-period"] = ta.SMA(dataframe, timeperiod=period)
dataframe["%-ema-period"] = ta.EMA(dataframe, timeperiod=period)
dataframe["%-adx-period"] = ta.ADX(dataframe, timeperiod=period)
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=period, stds=2.2)
# 改进布林带计算
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=period, stds=2.0)
dataframe["bb_lowerband-period"] = bollinger["lower"]
dataframe["bb_middleband-period"] = bollinger["mid"]
dataframe["bb_upperband-period"] = bollinger["upper"]
# 添加更多相对特征
dataframe["%-bb_width-period"] = (
dataframe["bb_upperband-period"] - dataframe["bb_lowerband-period"]
) / dataframe["bb_middleband-period"]
@ -78,9 +94,26 @@ class FreqaiExampleStrategy(IStrategy):
dataframe["%-relative_volume-period"] = (
dataframe["volume"] / dataframe["volume"].rolling(period).mean()
)
# 添加更多稳定特征
dataframe["%-atr-period"] = ta.ATR(dataframe, timeperiod=period)
dataframe["%-obv-period"] = ta.OBV(dataframe)
dataframe["%-cci-period"] = ta.CCI(dataframe, timeperiod=period)
# 改进数据清理
for col in dataframe.columns:
if dataframe[col].dtype in ["float64", "int64"]:
# 使用更稳健的归一化方法
mean = dataframe[col].mean()
std = dataframe[col].std()
dataframe[col] = (dataframe[col] - mean) / std
dataframe[col] = dataframe[col].clip(-3, 3) # 限制在3个标准差内
dataframe.replace([np.inf, -np.inf], 0, inplace=True)
dataframe.ffill(inplace=True)
dataframe.fillna(0, inplace=True)
logger.info(f"特征工程完成,特征数量:{len(dataframe.columns)}")
return dataframe
def feature_engineering_expand_basic(self, dataframe: DataFrame, metadata: dict, **kwargs) -> DataFrame: