This commit is contained in:
zhangkun9038@dingtalk.com 2026-02-03 21:21:57 +08:00
parent 49270fbe02
commit c8599a3e11

View File

@ -279,15 +279,30 @@ class FreqaiPrimer(IStrategy):
return 1.0
try:
# 直接计算波动系数
# 获取当前时间戳
current_time = datetime.now().timestamp()
# 检查缓存:如果距离上次计算时间小于更新间隔,则直接返回缓存值
if (pair in self._volatility_cache and
pair in self._volatility_timestamp and
current_time - self._volatility_timestamp[pair] < self._volatility_update_interval):
return self._volatility_cache[pair]
# 直接计算当前波动系数基于最近200根1h K线
current_volatility_coef = self._calculate_current_volatility_coef(pair)
# 更新缓存和时间戳
self._volatility_cache[pair] = current_volatility_coef
self._volatility_timestamp[pair] = current_time
self.strategy_log(f"波动系数计算完成 {pair}: 系数={current_volatility_coef:.4f} (基于最近200根1h K线)")
return current_volatility_coef
except Exception as e:
logger.warning(f"计算波动系数时出错 {pair}: {str(e)}")
# 如果出错返回默认值1.0
return 1.0
# 如果出错,尝试返回缓存值,否则返回默认值1.0
return self._volatility_cache.get(pair, 1.0)
def is_backtest_mode(self) -> bool:
"""
@ -1780,10 +1795,11 @@ class FreqaiPrimer(IStrategy):
return exit_ratio
def custom_roi(self, pair: str, trade: 'Trade', current_time: datetime, **kwargs) -> Dict:
def custom_roi(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float,
current_profit: float, **kwargs) -> Tuple[float, float]:
"""
自定义ROI函数根据入场类型和市场环境动态调整ROI
返回一个字典键为时间(分钟)值为对应的ROI阈值
返回 (时间, ROI阈值) 的元组表示在指定时间后达到指定ROI则退出
"""
# 获取入场类型信息从entry_tag解析
entry_tag = getattr(trade, 'enter_tag', '')
@ -1889,18 +1905,7 @@ class FreqaiPrimer(IStrategy):
f"趋势时间调整={trend_time_multiplier:.2f}, 调整后时长={adjusted_time}min"
)
# 返回ROI字典格式为 {时间: ROI阈值}
# ROI字典应该从最短时间到最长时间ROI值逐渐降低或保持
# 为了确保交易能够持续我们需要设置一个合理的ROI曲线
roi_dict = {}
roi_dict[0] = adjusted_roi # 立即生效的ROI
roi_dict[int(adjusted_time)] = adjusted_roi # 在指定时间后保持该ROI水平
roi_dict[1440] = 0.01 # 24小时后至少1%的ROI安全网
# 确保字典按照时间升序排列
sorted_roi_dict = dict(sorted(roi_dict.items()))
return sorted_roi_dict
return adjusted_time, adjusted_roi
def adjust_trade_position(self, trade: 'Trade', current_time, current_rate: float,