From 8422e93aa2066d0b1eff016afbdb5adc724a74a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Wed, 28 Jan 2026 19:05:18 +0800 Subject: [PATCH] a --- trading_system/config.py | 8 ++++++++ trading_system/strategy.py | 39 +++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/trading_system/config.py b/trading_system/config.py index 2ac3144..c1a5c74 100644 --- a/trading_system/config.py +++ b/trading_system/config.py @@ -219,6 +219,14 @@ def _get_trading_config(): 'PRIMARY_INTERVAL': '4h', # 主周期4小时,过滤噪音 'CONFIRM_INTERVAL': '1d', # 确认周期日线,看大趋势 'ENTRY_INTERVAL': '1h', # 入场周期1小时,避免太小的时间框架 + # 入场短周期(用于快速方向确认,例如15m),不要太小以免噪音太大 + 'ENTRY_SHORT_INTERVAL': '15m', + # 是否开启“短周期方向过滤”,避免在15m明显上涨时做空、明显下跌时做多 + 'ENTRY_SHORT_TREND_FILTER_ENABLED': True, + # 短周期方向过滤使用的最小趋势幅度(例如0.003=0.3%),变化太小视为震荡不过滤 + 'ENTRY_SHORT_TREND_MIN_PCT': 0.003, + # 检查最近多少根短周期K线来评估方向(例如3根15m约等于45分钟) + 'ENTRY_SHORT_CONFIRM_CANDLES': 3, 'MIN_VOLUME_24H': 30000000, # 24小时成交额≥3000万美元,过滤垃圾币 'MIN_VOLUME_24H_STRICT': 50000000, # 严格过滤≥5000万美元 'MIN_VOLATILITY': 0.03, # 最小波动率3%,过滤死币 diff --git a/trading_system/strategy.py b/trading_system/strategy.py index 14d399e..c1c1d13 100644 --- a/trading_system/strategy.py +++ b/trading_system/strategy.py @@ -444,7 +444,7 @@ class TradingStrategy: signal_strength = 0 reasons.append("❌ 逆4H趋势,拒绝交易") - # 判断是否应该交易(信号强度 >= 7 才交易,提高胜率) + # 判断是否应该交易(信号强度 >= MIN_SIGNAL_STRENGTH 才交易,提高胜率) min_signal_strength = config.TRADING_CONFIG.get('MIN_SIGNAL_STRENGTH', 7) # 强度上限归一到 0-10,避免出现 12/10 这种误导显示 try: @@ -453,6 +453,43 @@ class TradingStrategy: pass should_trade = signal_strength >= min_signal_strength and direction is not None + # ===== 15m 短周期方向过滤:避免“上涨中做空 / 下跌中做多” ===== + # 思路: + # - 使用最近 N 根 15m K 线的总涨跌幅来确认短期方向 + # - 做多(BUY):要求短期合计涨幅 >= ENTRY_SHORT_TREND_MIN_PCT + # - 做空(SELL):要求短期合计跌幅 <= -ENTRY_SHORT_TREND_MIN_PCT + # - 否则认为短期方向与计划方向不一致,降低 should_trade,并在 reason 中标记 + try: + short_filter_enabled = bool(config.TRADING_CONFIG.get('ENTRY_SHORT_TREND_FILTER_ENABLED', False)) + if short_filter_enabled and should_trade and direction in ('BUY', 'SELL'): + short_interval = config.TRADING_CONFIG.get('ENTRY_SHORT_INTERVAL', '15m') + periods = int(config.TRADING_CONFIG.get('ENTRY_SHORT_CONFIRM_CANDLES', 3) or 3) + min_pct = float(config.TRADING_CONFIG.get('ENTRY_SHORT_TREND_MIN_PCT', 0.003) or 0.003) + + # 仅在4H趋势已经明确向上/向下时启用短周期过滤;中性趋势本身就不适合自动交易 + if trend_4h in ('up', 'down'): + recent_change = await self._get_symbol_change_period(symbol, short_interval, periods) + if recent_change is not None: + # recent_change 是这段时间内的总涨跌幅比例(正为上涨,负为下跌) + if direction == 'BUY': + # 做多:要求短期是上涨或至少不明显下跌 + if recent_change < min_pct: + should_trade = False + reasons.append( + f"❌ 短周期({short_interval}×{periods})合计跌幅/涨幅不足:{recent_change*100:.2f}% " + f"(做多需≥{min_pct*100:.2f}%)" + ) + elif direction == 'SELL': + # 做空:要求短期是下跌或至少不明显上涨 + if recent_change > -min_pct: + should_trade = False + reasons.append( + f"❌ 短周期({short_interval}×{periods})合计涨幅/跌幅不足:{recent_change*100:.2f}% " + f"(做空需≤-{min_pct*100:.2f}%)" + ) + except Exception as e: + logger.debug(f"{symbol} 短周期方向过滤失败(忽略,不影响其它过滤): {e}") + # 提升胜率:4H趋势中性时不做自动交易(只保留推荐/观察) # 经验上,中性趋势下“趋势跟踪”信号更容易被来回扫损,导致胜率显著降低与交易次数激增。 allow_neutral = bool(config.TRADING_CONFIG.get("AUTO_TRADE_ALLOW_4H_NEUTRAL", False))