FUNCTION set_freqai_targets(dataframe, metadata, config): """ 设置回归模型的目标变量,为不同币对设置动态止损和ROI阈值。 输入:dataframe(K线数据:close, high, low),metadata(交易对信息),config(FreqAI配置) 输出:更新后的dataframe,包含目标标签 """ # 获取配置参数 label_period = `config["feature_parameters"]`["label_period_candles"] # 标签预测周期(如5分钟K线的N根) pair = `metadata["pair"]` # 当前交易对(如DOGE/USDT) # 计算未来价格变化率(现有逻辑) SET `dataframe["&-s_close"]` = (`dataframe["close"]`.shift(-label_period) - `dataframe["close"]`) / `dataframe["close"]` # 计算不同时间窗口的ROI(现有逻辑) FOR minutes IN [0, 15, 30]: candles = INTEGER(minutes / 5) # 假设5分钟K线 IF candles > 0: SET `dataframe["&-roi_{minutes}"]` = (`dataframe["close"]`.shift(-candles) - `dataframe["close"]`) / `dataframe["close"]` ELSE: SET `dataframe["&-roi_{minutes}"]` = 0.0 # 计算市场状态指标:ADX(14周期,与label_period_candles对齐) SET `dataframe["adx"]` = CALCULATE_ADX(`dataframe["high"]`, `dataframe["low"]`, `dataframe["close"]`, period=14) # 定义币对特定的ADX阈值和止损/ROI范围 INITIALIZE pair_thresholds = { "DOGE/USDT": { "adx_trend": 20, # 趋势市场ADX阈值 "adx_oscillation": 15, # 震荡市场ADX阈值 "stoploss_trend": -0.08, # 趋势市场止损:-8% "stoploss_oscillation": -0.04, # 震荡市场止损:-4% "stoploss_mid": -0.06, # 中间状态止损:-6% "roi_trend": 0.06, # 趋势市场ROI:6% "roi_oscillation": 0.025, # 震荡市场ROI:2.5% "roi_mid": 0.04 # 中间状态ROI:4% }, "BTC/USDT": { "adx_trend": 25, "adx_oscillation": 20, "stoploss_trend": -0.03, "stoploss_oscillation": -0.015, "stoploss_mid": -0.02, "roi_trend": 0.03, "roi_oscillation": 0.015, "roi_mid": 0.02 }, "SOL/USDT": { "adx_trend": 22, "adx_oscillation": 18, "stoploss_trend": -0.06, "stoploss_oscillation": -0.03, "stoploss_mid": -0.045, "roi_trend": 0.045, "roi_oscillation": 0.02, "roi_mid": 0.03 }, "XRP/USDT": { "adx_trend": 22, "adx_oscillation": 18, "stoploss_trend": -0.06, "stoploss_oscillation": -0.03, "stoploss_mid": -0.045, "roi_trend": 0.045, "roi_oscillation": 0.02, "roi_mid": 0.03 } } # 动态化 &-stoploss_pred(基于市场状态和币对) INITIALIZE `dataframe["&-stoploss_pred"]` = 0.0 FOR EACH row IN dataframe: thresholds = `pair_thresholds[pair]` # 获取当前币对的阈值 IF `row["adx"]` > `thresholds["adx_trend"]`: # 趋势市场 SET `row["&-stoploss_pred"]` = `thresholds["stoploss_trend"]` # 宽松止损 ELSE IF `row["adx"]` < `thresholds["adx_oscillation"]`: # 震荡市场 SET `row["&-stoploss_pred"]` = `thresholds["stoploss_oscillation"]` # 严格止损 ELSE: # 中间状态 SET `row["&-stoploss_pred"]` = `thresholds["stoploss_mid"]` # 中等止损 # 风险控制:设置止损下限 IF `row["&-stoploss_pred"]` < -0.10: # 防止过大止损 SET `row["&-stoploss_pred"]` = -0.10 # 动态化 &-roi_0_pred(基于市场趋势和币对) INITIALIZE `dataframe["&-roi_0_pred"]` = 0.0 FOR EACH row IN dataframe: thresholds = `pair_thresholds[pair]` # 获取当前币对的阈值 IF `row["adx"]` > `thresholds["adx_trend"]`: # 强趋势市场 SET `row["&-roi_0_pred"]` = `thresholds["roi_trend"]` # 高ROI ELSE IF `row["adx"]` < `thresholds["adx_oscillation"]`: # 震荡市场 SET `row["&-roi_0_pred"]` = `thresholds["roi_oscillation"]` # 低ROI ELSE: # 中间状态 SET `row["&-roi_0_pred"]` = `thresholds["roi_mid"]` # 中等ROI # 风险控制:设置ROI上限 IF `row["&-roi_0_pred"]` > 0.10: # 防止过高目标 SET `row["&-roi_0_pred"]` = 0.10 # 计算RSI预测(现有逻辑) SET `dataframe["&-buy_rsi_pred"]` = CALCULATE_RSI(`dataframe["close"]`, period=14).ROLLING_MEAN(window=20) RETURN dataframe