已经把 &s-future_volatility 的自适应阈值 做完并且同步更新了文档
This commit is contained in:
parent
5351969c4a
commit
400a466f5e
@ -232,5 +232,8 @@
|
||||
- `trend_reversal_score`
|
||||
- `protect_trend_continuation`
|
||||
- `adx_weak_threshold`
|
||||
- **FreqAI 标签与自适应阈值**:`set_freqai_targets` 中:
|
||||
- `&s-entry_signal` / `&s-exit_signal`:使用 `_auto_adjust_threshold` + ES 中 `labels_mean` 动态调整涨跌幅阈值(`freqai_entry_up_percent` / `freqai_exit_down_percent`)。
|
||||
- `&s-future_volatility`:使用 `_auto_adjust_threshold` 动态调整“未来波动率 / 当前波动率”的倍数阈值 `freqai_future_vol_ratio`,目标是让高波动标签的正样本比例维持在合理区间(约 0.3–0.6)。
|
||||
|
||||
本说明与实现保持一致,如后续调整权重或参数范围,建议同步更新本文档,便于长期维护和回顾。
|
||||
@ -379,6 +379,10 @@ class FreqaiPrimer(IStrategy):
|
||||
# 默认 0.8%,如标签过多/过少,由自适应机制再微调
|
||||
freqai_exit_down_percent = DecimalParameter(0.3, 3.0, decimals=2, default=0.8, optimize=True, load=True, space='buy')
|
||||
|
||||
# FreqAI 标签定义:future_volatility 的未来波动率倍数阈值(> 倍数则认为高波动)
|
||||
# 默认 1.20(未来波动率 > 当前 * 1.20 视为高波动),如正样本过多/过少,由自适应机制再微调
|
||||
freqai_future_vol_ratio = DecimalParameter(1.05, 2.0, decimals=2, default=1.20, optimize=True, load=True, space='buy')
|
||||
|
||||
# 定义可优化参数
|
||||
# 初始入场金额: 75.00
|
||||
|
||||
@ -466,10 +470,10 @@ class FreqaiPrimer(IStrategy):
|
||||
|
||||
# 自适应调整逻辑
|
||||
return self._calculate_adjusted_threshold(pair, entry_signal_mean, default_threshold, label_name)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
# 出错时使用默认值
|
||||
logger.warning(f"[{pair}] 自适应阈值调整失败: {e},使用默认值 {default_threshold}%")
|
||||
logger.warning(f"[{pair}] 自适应阈值调整失败: {e},使用默认值 {default_threshold}")
|
||||
return default_threshold
|
||||
|
||||
def _get_label_distribution_from_es(self, pair: str, label_name: str) -> float | None:
|
||||
@ -700,7 +704,7 @@ class FreqaiPrimer(IStrategy):
|
||||
防震荡策略:
|
||||
1. 分区调整:理想区不调整,边缘区微调(±7.5%),极端区大调(±70%)
|
||||
2. 单次限幅:最大变化幅度不超过 ±30%(防止过激调整)
|
||||
3. 硬限制边界:阈值范围锁定在 [0.3%, 3.0%]
|
||||
3. 硬限制边界:阈值范围锁定在 [0.3, 3.0]
|
||||
"""
|
||||
IDEAL_MIN = 0.40
|
||||
IDEAL_MAX = 0.60
|
||||
@ -721,9 +725,9 @@ class FreqaiPrimer(IStrategy):
|
||||
|
||||
elif IDEAL_MIN <= entry_signal_mean <= IDEAL_MAX:
|
||||
# 理想范围(40%-60%),保持不变
|
||||
self.strategy_log(f"[{pair}] 标签分布理想 ({entry_signal_mean:.1%}), 保持阈值 {default_threshold:.2f}%")
|
||||
self.strategy_log(f"[{pair}] 标签分布理想: {label_name}={entry_signal_mean:.1%}, 保持阈值 {default_threshold:.4f}")
|
||||
return default_threshold
|
||||
|
||||
|
||||
else:
|
||||
# 边缘区(35%-40% 或 60%-65%),微调
|
||||
if entry_signal_mean > IDEAL_MAX:
|
||||
@ -745,13 +749,13 @@ class FreqaiPrimer(IStrategy):
|
||||
|
||||
# === 第三步:计算新阈值并应用硬边界 ===
|
||||
new_threshold = default_threshold * adjustment_factor
|
||||
new_threshold = min(max(new_threshold, 0.3), 3.0) # 硬限制在 [0.3%, 3.0%]
|
||||
new_threshold = min(max(new_threshold, 0.3), 3.0) # 硬限制在 [0.3, 3.0]
|
||||
|
||||
# === 第四步:记录日志与追踪数据 ===
|
||||
change_percent = (new_threshold - default_threshold) / default_threshold * 100
|
||||
self.strategy_log(
|
||||
f"[{pair}] 自适应阈值调整{capped}: {adjustment_type} ({entry_signal_mean:.1%}), "
|
||||
f"阈值 {default_threshold:.2f}% → {new_threshold:.2f}% ({change_percent:+.1f}%)"
|
||||
f"[{pair}] 自适应阈值调整{capped}: {label_name} {adjustment_type} ({entry_signal_mean:.1%}), "
|
||||
f"阈值 {default_threshold:.4f} → {new_threshold:.4f} ({change_percent:+.1f}%)"
|
||||
)
|
||||
|
||||
# 记录到 Redis
|
||||
@ -848,10 +852,17 @@ class FreqaiPrimer(IStrategy):
|
||||
# 正确做法:先计算 rolling,再整体 shift
|
||||
future_volatility = dataframe["close"].pct_change().rolling(window=10, min_periods=5).std().shift(-10)
|
||||
|
||||
# 标签:未来波动率 > 当前波动率 * 1.2 则标记为高波动(趋势启动)
|
||||
# 从 ES/Redis/文件读取 &s-future_volatility 的最新标签分布,自适应调整阈值
|
||||
future_vol_ratio = self._auto_adjust_threshold(
|
||||
metadata['pair'],
|
||||
self.freqai_future_vol_ratio.value,
|
||||
label_name='&s-future_volatility',
|
||||
)
|
||||
|
||||
# 标签:未来波动率 / 当前波动率 > 自适应倍数,则标记为高波动(趋势启动)
|
||||
volatility_ratio = future_volatility / (current_volatility + 1e-8) # 避免除以0
|
||||
dataframe["&s-future_volatility"] = np.where(
|
||||
volatility_ratio > 1.2,
|
||||
volatility_ratio > future_vol_ratio,
|
||||
1, # 未来高波动(趋势启动),继续持有
|
||||
0 # 未来低波动(震荡市),快速止盈
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user