diff --git a/freqtrade/templates/freqaiprimer.py b/freqtrade/templates/freqaiprimer.py index f9289a54..8abce4b3 100644 --- a/freqtrade/templates/freqaiprimer.py +++ b/freqtrade/templates/freqaiprimer.py @@ -228,10 +228,14 @@ class FreqaiPrimer(IStrategy): entry_interval_minutes = IntParameter(20, 200, default=42, optimize=True, load=True, space='buy') # ML 审核官:entry_signal 拒绝入场的阈值(越高越宽松,越低越严格) - ml_entry_signal_threshold = DecimalParameter(0.05, 0.85, decimals=2, default=0.78, optimize=True, load=True, space='buy') - - # ML 审核官:exit_signal 拒绝出场的阈值(越高越宽松,越低越严格) - ml_exit_signal_threshold = DecimalParameter(0.05, 0.85, decimals=2, default=0.68, optimize=True, load=True, space='buy') + # ml_entry_signal_threshold = DecimalParameter(0.05, 0.85, decimals=2, default=0.90, optimize=True, load=True, space='buy') + # + # # ML 审核官:exit_signal 拒绝出场的阈值(越高越宽松,越低越严格) + # ml_exit_signal_threshold = DecimalParameter(0.05, 0.85, decimals=2, default=0.68, optimize=True, load=True, space='buy') + + # ML 审核官阈值已改为根据市场状态动态调整,不再使用固定参数 + # strong_bull: 入场0.15/出场0.85, weak_bull: 0.325/0.675, neutral: 0.50/0.50 + # weak_bear: 0.675/0.325, strong_bear: 0.85/0.15 # FreqAI 标签定义:entry_signal 的洛底上涨幅度(%) freqai_entry_up_percent = DecimalParameter(0.3, 2.0, decimals=2, default=0.5, optimize=True, load=True, space='buy') @@ -809,7 +813,32 @@ class FreqaiPrimer(IStrategy): except Exception as e: logger.error(f"[{pair}] 剧烈拉升检测过程中发生错误: {str(e)}") return False - + + def get_ml_threshold_by_market_state(self, market_state: str, threshold_type: str = 'entry') -> float: + """ + 根据市场状态动态获取 ML 审核官阈值 + + Args: + market_state: 市场状态 (strong_bull, weak_bull, neutral, weak_bear, strong_bear) + threshold_type: 阈值类型 ('entry' 或 'exit') + + Returns: + float: 动态计算的阈值 + """ + # 市场状态到阈值的映射 + thresholds_map = { + 'strong_bull': {'entry': 0.15, 'exit': 0.85}, + 'weak_bull': {'entry': 0.325, 'exit': 0.675}, + 'neutral': {'entry': 0.50, 'exit': 0.50}, + 'weak_bear': {'entry': 0.675, 'exit': 0.325}, + 'strong_bear': {'entry': 0.85, 'exit': 0.15} + } + + # 默认值(如果市场状态未知) + default_threshold = 0.50 + + return thresholds_map.get(market_state, {}).get(threshold_type, default_threshold) + def confirm_trade_entry( self, pair: str, @@ -917,7 +946,8 @@ class FreqaiPrimer(IStrategy): if entry_prob is not None: # 确保概率在 [0, 1] 范围内(分类器输出可能有浮点误差) entry_prob = max(0.0, min(1.0, entry_prob)) - entry_threshold = self.ml_entry_signal_threshold.value + # 根据市场状态动态获取入场阈值 + entry_threshold = self.get_ml_threshold_by_market_state(market_state, 'entry') # 记录entry_signal值用于调试 self.strategy_log(f"[{pair}] ML 审核官检查: entry_signal={entry_prob:.2f}, 阈值={entry_threshold:.2f}, 市场状态={market_state}") if entry_prob < entry_threshold: @@ -997,8 +1027,7 @@ class FreqaiPrimer(IStrategy): current_profit = float(kwargs.get('current_profit', 0.0)) # 获取出场一字基础阈值 - base_threshold = self.ml_exit_signal_threshold.value - + base_threshold = self.get_ml_threshold_by_market_state(market_state, 'exit') # 计算持仓时长(分钟) try: trade_age_minutes = max(0.0, (current_time - trade.open_date_utc).total_seconds() / 60.0) diff --git a/tools/backtest.sh b/tools/backtest.sh index 3d1a07ba..aa086035 100755 --- a/tools/backtest.sh +++ b/tools/backtest.sh @@ -201,7 +201,6 @@ docker-compose run --rm freqtrade backtesting $PAIRS_FLAG \ --logfile /freqtrade/user_data/logs/freqtrade.log \ --freqaimodel LightGBMRegressorMultiTarget \ --config /freqtrade/config_examples/$CONFIG_FILE \ - --config /freqtrade/config_examples/live.json \ --config /freqtrade/templates/freqaiprimer.json \ --strategy-path /freqtrade/templates \ --enable-protections \