From 5616ae4c324ea6866b422bd0e30874ca5ce0d75e Mon Sep 17 00:00:00 2001 From: jackyu66git Date: Tue, 9 Dec 2025 20:42:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86trend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TF_DF.py | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/TF_DF.py b/TF_DF.py index 5d88479..fe4b12e 100644 --- a/TF_DF.py +++ b/TF_DF.py @@ -167,10 +167,15 @@ class TF_DF(): price = getattr(klc, 'close', None) ema24 = getattr(klc, 'ema24', None) ema52 = getattr(klc, 'ema52', None) - macd = getattr(klc, 'macd', 0) if getattr(klc, 'macd', None) is not None else 0 - signal = getattr(klc, 'signal', 0) if getattr(klc, 'signal', None) is not None else 0 - hist = getattr(klc, 'macdhist', 0) if getattr(klc, 'macdhist', None) is not None else 0 + macd_raw = getattr(klc, 'macd', None) + signal_raw = getattr(klc, 'signal', None) + hist_raw = getattr(klc, 'macdhist', None) + macd = macd_raw if macd_raw is not None else 0 + signal = signal_raw if signal_raw is not None else 0 + hist = hist_raw if hist_raw is not None else 0 rsi = getattr(klc, 'rsi', None) + macd_ready = macd_raw is not None and signal_raw is not None + hist_ready = hist_raw is not None trend = Chan_PRICE_TREND.UNKNOWN score = 0 try: @@ -181,18 +186,24 @@ class TF_DF(): # 多因子投票 # 1) 均线结构 + 价位 - if ema24_valid and ema52_valid: - score += 1 if ema24 > ema52 else -1 - if price_valid and ema24_valid: - score += 1 if price > ema24 else -1 - if price_valid and ema52_valid: - score += 1 if price > ema52 else -1 + if ema24_valid or ema52_valid: + ma_votes = 0 + if ema24_valid and ema52_valid: + ma_votes += 1 if ema24 > ema52 else -1 + if price_valid and ema24_valid: + ma_votes += 1 if price > ema24 else 0 + if price_valid and ema52_valid: + ma_votes += 1 if price > ema52 else -1 + # 限幅,避免相关因子重复计分 + score += max(-2, min(2, ma_votes)) # 2) MACD结构 - score += 1 if macd >= signal else -1 - if hist != 0: - score += 1 if hist > 0 else -1 + if macd_ready: + score += 1 if macd >= signal else -1 + if hist_ready and hist != 0: + score += 1 if hist > 0 else -1 # 3) 动量与均线差分斜率 pre = getattr(klc, 'pre', None) + pre_hist = getattr(pre, 'macdhist', None) if pre else None if pre: pre_close = getattr(pre, 'close', None) if price_valid and pre_close is not None: @@ -204,8 +215,7 @@ class TF_DF(): spread_pre = pre_ema24 - pre_ema52 score += 1 if spread_now >= spread_pre else -1 # 3.1) MACD柱体动量趋势:考虑 macdhist 的斜率与过零 - pre_hist = getattr(pre, 'macdhist', None) - if pre_hist is not None and hist is not None: + if hist_ready and pre_hist is not None: # 柱体斜率:上升加分,下降减分 if hist > pre_hist: score += 1 @@ -266,8 +276,8 @@ class TF_DF(): if wk_low <= wk_ema52 or abs(wk_low - wk_ema52) / abs(wk_ema52) <= rej_tol: recent_down_rejects += 1 # 定义 MACD 的方向偏好 - macd_bias_up = (macd >= signal) and (hist is None or pre_hist is None or hist >= pre_hist) - macd_bias_down = (macd <= signal) and (hist is None or pre_hist is None or hist <= pre_hist) + macd_bias_up = macd_ready and (macd >= signal) and (not hist_ready or pre_hist is None or hist >= pre_hist) + macd_bias_down = macd_ready and (macd <= signal) and (not hist_ready or pre_hist is None or hist <= pre_hist) # 若多次上拒绝且 MACD 偏空,则更偏向下行;若多次下拒绝且 MACD 偏多,则更偏向上行 if recent_up_rejects >= 2 and macd_bias_down: score -= 2 @@ -307,7 +317,11 @@ class TF_DF(): near_flat = False if price_valid and ema52_valid: near_ema52 = abs(price - ema52) / abs(ema52) <= 0.0005 # 0.05% - near_macd = abs(macd - signal) <= (abs(price) * 0.00005 if price_valid else 0) + if macd_ready: + macd_scale = max(abs(macd), abs(signal), 1e-6) + near_macd = abs(macd - signal) / macd_scale <= 0.05 + else: + near_macd = False near_flat = near_ema52 and near_macd # 7) 动态阈值 + 趋势记忆(更强粘滞:趋势中容忍小幅反分) # 引入过去 N 根KLC 的趋势延续性来动态调整翻转阈值,并结合 EMA52 支撑/阻力触碰强化门槛 @@ -352,8 +366,8 @@ class TF_DF(): # 7.1) 复合拐头信号:MACD/Signal 同向拐头 + hist 连续减弱 + 多次未能越过 EMA52 pre_macd = getattr(pre, 'macd', None) if pre else None pre_signal = getattr(pre, 'signal', None) if pre else None - macd_slope = (macd - pre_macd) if (pre_macd is not None and macd is not None) else 0 - signal_slope = (signal - pre_signal) if (pre_signal is not None and signal is not None) else 0 + macd_slope = (macd - pre_macd) if (macd_ready and pre_macd is not None) else 0 + signal_slope = (signal - pre_signal) if (macd_ready and pre_signal is not None) else 0 # hist 连续减弱(绝对值缩小) hist_seq = [] for wk in prev_klcs[-2:]: