technical-patterns-lab/docs/突破强度计算方法.md
褚宏光 22582851a1 Enhance converging triangle detection with new features and documentation updates
- Introduced an interactive HTML stock viewer for visualizing strength scores and filtering stocks based on user-defined thresholds.
- Added `--all-stocks` parameter to generate charts for all 108 stocks, including those not meeting convergence criteria.
- Implemented a new scoring system for breakout strength, incorporating fitting adherence to improve accuracy.
- Updated multiple documentation files, including usage instructions and feature overviews, to reflect recent enhancements.
- Improved error handling and file naming conventions to ensure compatibility across platforms.
2026-01-27 16:17:28 +08:00

10 KiB
Raw Permalink Blame History

突破强度计算方法

最后更新2026-01-27
版本v3.0(四维度加权求和 + tanh 非线性归一化)

概述

突破强度是一个 0~1 的连续分数,用于衡量收敛三角形突破的有效性。
分数越高,表示突破越强势、越可信。


设计演进

v1.0 乘法组合(已弃用)

# 旧公式
strength = price_score × 5 × (1 + convergence_bonus × 0.5) × (1 + vol_bonus × 0.5)

问题:乘法组合 + 高乘数×5导致 73-76% 的突破都是满分 1.0,无法有效区分突破质量。

v2.0 加权求和 + tanh 归一化(已升级)

# v2.0 公式(三维度)
strength = 0.60 × tanh(突破幅度% × 15) +  # 价格分 (60%)
           0.25 × (1 - width_ratio) +        # 收敛分 (25%)
           0.15 × vol_bonus                   # 成交量分 (15%)

改进效果

指标 v1.0 v2.0
满分比例(>0.9) 73-76% 0.9-6.0%
平均强度 ~0.12 ~0.59
最大强度 1.0000 0.9928
区分度 差(大量满分) 好(均匀分布)

v3.0 四维度评分(当前版本)

# v3.0 公式(四维度)
strength = 0.50 × tanh(突破幅度% × 15) +  # 价格分 (50%)
           0.20 × (1 - width_ratio) +        # 收敛分 (20%)
           0.15 × vol_bonus +                 # 成交量分 (15%)
           0.15 × fitting_adherence          # 拟合贴合度 (15%)

新增维度:拟合贴合度,衡量枢轴点到拟合线的距离,反映形态纯度和标准程度。


当前计算公式v3.0

公式结构

def calc_breakout_strength(close, upper_line, lower_line, volume_ratio, width_ratio, fitting_adherence):
    import math
    
    # 权重配置
    W_PRICE = 0.50       # 突破幅度权重
    W_CONVERGENCE = 0.20 # 收敛度权重
    W_VOLUME = 0.15      # 成交量权重
    W_FITTING = 0.15     # 拟合贴合度权重
    TANH_SCALE = 15.0    # tanh 缩放因子
    
    # 1. 价格突破分数tanh 非线性归一化)
    pct_up = max(0, (close - upper_line) / upper_line)
    price_score_up = math.tanh(pct_up * TANH_SCALE)
    
    # 2. 收敛分数
    convergence_score = max(0, min(1, 1 - width_ratio))
    
    # 3. 成交量分数
    vol_score = min(1, max(0, volume_ratio - 1))
    
    # 4. 拟合贴合度分数
    fitting_score = max(0, min(1, fitting_adherence))
    
    # 5. 加权求和
    strength_up = (W_PRICE * price_score_up + 
                   W_CONVERGENCE * convergence_score + 
                   W_VOLUME * vol_score + 
                   W_FITTING * fitting_score)
    
    return min(1.0, strength_up)

四个分量详解

1. 价格突破分数(权重 50%

使用 tanh 函数 进行非线性归一化,避免大幅突破导致的满分堆积。

price_score = tanh(突破幅度% × 15)

突破幅度映射表

突破幅度 price_score 贡献分数 (×0.5)
0.5% 0.07 0.04
1% 0.15 0.08
2% 0.29 0.15
3% 0.42 0.21
5% 0.64 0.32
8% 0.83 0.42
10% 0.91 0.46
15% 0.97 0.49

设计考量

  • A股涨跌停限制 10%,常见突破在 1-5% 范围
  • tanh 函数使小幅突破有区分,大幅突破趋于收敛
  • 系数 15 使得 3% 突破 ≈ 0.42 分5% 突破 ≈ 0.64 分

2. 收敛分数(权重 20%

三角形收敛程度越高,突破越有效。

convergence_score = max(0, 1 - width_ratio)
width_ratio 收敛程度 convergence_score 贡献分数 (×0.20)
0.8 较弱 0.20 0.04
0.6 中等 0.40 0.08
0.4 较强 0.60 0.12
0.2 很强 0.80 0.16
0.1 极强 0.90 0.18
0.05 极度收敛 0.95 0.19

width_ratio = 三角形末端宽度 / 起始宽度


3. 成交量分数(权重 15%

放量突破更可信。

vol_score = min(1, max(0, volume_ratio - 1))
volume_ratio 成交量状态 vol_score 贡献分数 (×0.15)
0.8 缩量 0 0
1.0 平量 0 0
1.5 放量 50% 0.5 0.075
2.0 放量 100% 1.0 0.15
3.0 放量 200% 1.0 0.15 (上限)

volume_ratio = 当日成交量 / 近 N 日均量


4. 拟合贴合度(权重 15%

枢轴点到拟合线的贴合程度越高,说明形态越标准、越纯净。

# 计算单条线的贴合度
def calc_fitting_adherence(pivot_indices, pivot_values, slope, intercept):
    fitted_values = slope * pivot_indices + intercept
    rel_errors = abs(pivot_values - fitted_values) / abs(fitted_values)
    mean_rel_error = mean(rel_errors)
    adherence_score = exp(-mean_rel_error * 20)
    return adherence_score

# 综合上下沿贴合度
fitting_adherence = (adherence_upper + adherence_lower) / 2

贴合度映射表scale_factor = 20

平均相对误差 adherence_score 贡献分数 (×0.15) 形态评价
0% 1.00 0.15 完美拟合
2% 0.67 0.10 良好拟合
3% 0.55 0.08 较好拟合
5% 0.37 0.06 一般拟合
8% 0.20 0.03 较差拟合
10% 0.14 0.02 差拟合

设计考量

  • 使用指数衰减函数 exp(-error * 20) 进行归一化
  • 上下沿贴合度分别计算后取平均,综合评估形态质量
  • 贴合度高表示枢轴点整齐排列在拟合线上,形态"纯净"
  • 贴合度低表示枢轴点散乱分布,可能存在噪音或形态不标准

拟合贴合度 = (上沿贴合度 + 下沿贴合度) / 2


计算示例

示例 1强势突破领湃科技 2026-01-20

输入:
  突破幅度 ≈ 8%     (close 大幅高于 upper_line)
  width_ratio = 0.0465  (极度收敛)
  volume_ratio > 1.5    (放量确认)
  fitting_adherence ≈ 0.85  (假设形态标准)

计算:
  price_score = tanh(0.08 × 15) = tanh(1.2) ≈ 0.83
  convergence_score = 1 - 0.0465 = 0.9535
  vol_score = min(1, 1.5 - 1) = 0.5
  fitting_score = 0.85

  strength = 0.50 × 0.83 + 0.20 × 0.9535 + 0.15 × 0.5 + 0.15 × 0.85
           = 0.415 + 0.191 + 0.075 + 0.128
           = 0.809

实际结果: 约 0.80-0.95 (取决于实际拟合贴合度)

示例 2中等突破五芳斋 2026-01-20

输入:
  突破幅度 ≈ 3%
  width_ratio = 0.2090
  volume_ratio ≈ 1.0  (未放量)
  fitting_adherence ≈ 0.60  (假设形态一般)

计算:
  price_score = tanh(0.03 × 15) = tanh(0.45) ≈ 0.42
  convergence_score = 1 - 0.2090 = 0.791
  vol_score = 0
  fitting_score = 0.60

  strength = 0.50 × 0.42 + 0.20 × 0.791 + 0.15 × 0 + 0.15 × 0.60
           = 0.210 + 0.158 + 0 + 0.090
           = 0.458

实际结果: 约 0.45-0.55 (取决于实际拟合贴合度)

示例 3弱势突破康华生物 2026-01-20

输入:
  突破幅度 ≈ 2%
  width_ratio = 0.1338
  volume_ratio ≈ 1.0  (未放量)
  fitting_adherence ≈ 0.70  (假设形态较好)

计算:
  price_score = tanh(0.02 × 15) = tanh(0.30) ≈ 0.29
  convergence_score = 1 - 0.1338 = 0.866
  vol_score = 0
  fitting_score = 0.70

  strength = 0.50 × 0.29 + 0.20 × 0.866 + 0.15 × 0 + 0.15 × 0.70
           = 0.145 + 0.173 + 0 + 0.105
           = 0.423

实际结果: 约 0.40-0.50 (取决于实际拟合贴合度)

强度等级参考

强度范围 等级 含义 占比参考
0 ~ 0.3 微弱 假突破风险高,需谨慎 ~8%
0.3 ~ 0.5 轻度 有突破迹象,建议观察 ~27%
0.5 ~ 0.7 中度 有效突破,可作为参考 ~28%
0.7 ~ 0.9 强势 高置信度突破,值得关注 ~31%
0.9 ~ 1.0 极强 顶级突破信号 ~6%

权重选择理由

分量 权重 理由
价格突破 50% 突破幅度是最直接的信号,决定性因素
收敛程度 20% 收敛越强,蓄势越充分,突破有效性越高
成交量 15% 放量是确认信号,但非必要条件(有些有效突破不放量)
拟合贴合度 15% 形态纯度指标,贴合度高说明形态标准、噪音少

总和 = 100%,确保最终分数在合理范围内。


代码位置

src/converging_triangle.py
├── calc_fitting_adherence()     # 拟合贴合度计算函数 (第 375-428 行)
├── calc_breakout_strength()      # 突破强度计算函数 (第 430-518 行)
└── detect_converging_triangle()  # 调用位置 (第 700-727 行)

scripts/triangle_config.py        # 参数配置(严格模式/默认模式/宽松模式)

相关参数配置

参数 严格模式 默认模式 说明
window 120 120 检测窗口大小(交易日)
shrink_ratio 0.6 0.8 收敛比例阈值(越小越严格)
break_tol 0.005 0.001 突破判定容差0.5% vs 0.1%
vol_window 20 20 计算成交量均值的窗口
vol_k 1.5 1.3 成交量确认阈值

当前使用 严格模式,详见 scripts/triangle_config.py


附录:归一化函数特性

tanh 函数(用于价格突破分数)

tanh(x) = (e^x - e^-x) / (e^x + e^-x)
  • 输出范围:(-1, 1)
  • 当 x=0 时tanh(0) = 0
  • 当 x→∞ 时tanh(x) → 1
  • 单调递增,处处可导
  • 在 x=0 附近近似线性,大值时趋于饱和

选择 tanh 的原因

  1. 自然归一化到 (0, 1)
  2. 小幅突破有区分度
  3. 大幅突破不会无限增长
  4. 平滑过渡,无跳变

指数衰减函数(用于拟合贴合度)

adherence(error) = exp(-error × scale_factor)
  • 输出范围:(0, 1]
  • 当 error=0 时adherence=1完美拟合
  • 当 error→∞ 时adherence→0完全不贴合
  • 单调递减,处处可导
  • scale_factor 控制衰减速度

选择指数衰减的原因scale_factor = 20

  1. 自然归一化到 (0, 1)
  2. 误差为 0 时得满分,符合直觉
  3. 误差越大惩罚越重(指数级)
  4. 2% 误差给 0.67 分5% 误差给 0.37 分,梯度合理