dryrun和实盘模式下都能正常运行
This commit is contained in:
parent
bb64aaa669
commit
065781e1a7
@ -157,6 +157,12 @@
|
||||
- **custom_stoploss**:根据入场类型、市场状态和趋势强度调整止损策略
|
||||
- **智能适配**:不同类型入场策略在不同市场环境下采用相应调整策略
|
||||
|
||||
#### 4.11 运行模式区分与适配
|
||||
- **模式检测**:新增`is_backtest_mode()`和`is_live_or_dryrun_mode()`方法
|
||||
- **回测模式**:适用于历史数据测试,移除基于系统时间的缓存机制
|
||||
- **实盘/DRY_RUN模式**:可启用基于时间的缓存和其他优化机制
|
||||
- **适配策略**:根据不同运行模式调整算法行为,确保在所有模式下正常工作
|
||||
|
||||
### 5. 移除的逻辑
|
||||
- 移除了冷启动保护逻辑,确保回测和实盘的一致性
|
||||
|
||||
|
||||
@ -270,7 +270,7 @@ class FreqaiPrimer(IStrategy):
|
||||
- 山寨币的波动系数可能大于3
|
||||
- 稳定性较高的币对(如DOT/USDT)波动系数可能小于1
|
||||
|
||||
添加了缓存机制,每3分钟更新一次,避免频繁计算
|
||||
为了兼容回测,移除了基于系统时间的缓存机制
|
||||
"""
|
||||
# 检查特殊币对
|
||||
if pair == 'USDT/USDT':
|
||||
@ -279,30 +279,28 @@ class FreqaiPrimer(IStrategy):
|
||||
return 1.0
|
||||
|
||||
try:
|
||||
# 获取当前时间戳
|
||||
current_time = datetime.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 self._volatility_cache.get(pair, 1.0)
|
||||
# 如果出错,返回默认值1.0
|
||||
return 1.0
|
||||
|
||||
def is_backtest_mode(self) -> bool:
|
||||
"""
|
||||
检查是否处于回测模式
|
||||
"""
|
||||
return self.config.get('runmode', '').lower() == 'backtest'
|
||||
|
||||
def is_live_or_dryrun_mode(self) -> bool:
|
||||
"""
|
||||
检查是否处于实盘或dryrun模式
|
||||
"""
|
||||
runmode = self.config.get('runmode', '').lower()
|
||||
return runmode in ['live', 'dry_run']
|
||||
|
||||
|
||||
def _calculate_current_volatility_coef(self, pair: str) -> float:
|
||||
@ -1777,10 +1775,10 @@ class FreqaiPrimer(IStrategy):
|
||||
|
||||
|
||||
def custom_roi(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float,
|
||||
current_profit: float, **kwargs) -> Tuple[float, float]:
|
||||
current_profit: float, **kwargs) -> Dict:
|
||||
"""
|
||||
自定义ROI函数,根据入场类型和市场环境动态调整ROI
|
||||
返回 (时间, ROI阈值) 的元组,表示在指定时间后达到指定ROI则退出
|
||||
返回一个字典,键为时间(分钟),值为对应的ROI阈值
|
||||
"""
|
||||
# 获取入场类型信息(从entry_tag解析)
|
||||
entry_tag = getattr(trade, 'enter_tag', '')
|
||||
@ -1886,7 +1884,13 @@ class FreqaiPrimer(IStrategy):
|
||||
f"趋势时间调整={trend_time_multiplier:.2f}, 调整后时长={adjusted_time}min"
|
||||
)
|
||||
|
||||
return adjusted_time, adjusted_roi
|
||||
# 返回ROI字典,格式为 {时间: ROI阈值}
|
||||
# 这里我们返回一个简单的ROI表,从0时间开始直到计算出的时间,然后维持最终的ROI值
|
||||
roi_dict = {}
|
||||
roi_dict[0] = adjusted_roi # 立即止盈
|
||||
roi_dict[int(adjusted_time)] = adjusted_roi # 在指定时间后保持该ROI水平
|
||||
|
||||
return roi_dict
|
||||
|
||||
|
||||
def adjust_trade_position(self, trade: 'Trade', current_time, current_rate: float,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user