technical-patterns-lab/scripts/test_realtime_mode.py
褚宏光 6d545eb231 Enhance converging triangle detection with new features and documentation updates
- Added support for a detailed chart mode in plot_converging_triangles.py, allowing users to visualize all pivot points and fitting lines.
- Improved pivot fitting logic to utilize multiple representative points, enhancing detection accuracy and reducing false positives.
- Introduced a new real-time detection mode with flexible zone parameters for better responsiveness in stock analysis.
- Updated README.md and USAGE.md to reflect new features and usage instructions.
- Added multiple documentation files detailing recent improvements, including pivot point fitting and visualization enhancements.
- Cleaned up and archived outdated scripts to streamline the project structure.
2026-01-26 16:21:36 +08:00

259 lines
7.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
实时模式 vs 标准模式对比测试
用法:
python scripts/test_realtime_mode.py
"""
import numpy as np
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
from converging_triangle import (
ConvergingTriangleParams,
detect_converging_triangle,
pivots_fractal,
pivots_fractal_hybrid,
)
def test_pivot_detection_comparison():
"""对比标准和混合枢轴点检测"""
print("=" * 70)
print("枢轴点检测对比测试")
print("=" * 70)
# 创建测试数据120天窗口有明显的高低点
n = 120
k = 15
flexible_zone = 5
high = np.ones(n) * 50
low = np.ones(n) * 40
# 设置关键点
high[50] = 70 # 中间高点(可被标准模式检测)
high[90] = 75 # 后部高点(可被标准模式检测)
high[110] = 80 # 末端高点(只能被实时模式检测)
low[40] = 30 # 中间低点(可被标准模式检测)
low[80] = 25 # 后部低点(可被标准模式检测)
low[108] = 20 # 末端低点(只能被实时模式检测)
print(f"\n测试配置:")
print(f" 窗口大小: {n}")
print(f" 枢轴参数 k: {k}")
print(f" 灵活区域: {flexible_zone}")
# 标准模式
ph_std, pl_std = pivots_fractal(high, low, k=k)
# 实时模式
ph_confirmed, pl_confirmed, ph_candidate, pl_candidate = \
pivots_fractal_hybrid(high, low, k=k, flexible_zone=flexible_zone)
print(f"\n标准模式结果:")
print(f" 高点枢轴: {len(ph_std)}")
print(f" 索引: {ph_std}")
print(f" 包含索引110: {110 in ph_std}")
print(f" 低点枢轴: {len(pl_std)}")
print(f" 索引: {pl_std}")
print(f" 包含索引108: {108 in pl_std}")
print(f"\n实时模式结果:")
print(f" 确认高点: {len(ph_confirmed)}")
print(f" 候选高点: {len(ph_candidate)}个 - {ph_candidate}")
print(f" 总高点: {len(ph_confirmed) + len(ph_candidate)}")
print(f" 包含索引110: {110 in np.concatenate([ph_confirmed, ph_candidate])}")
print(f" 确认低点: {len(pl_confirmed)}")
print(f" 候选低点: {len(pl_candidate)}个 - {pl_candidate}")
print(f" 总低点: {len(pl_confirmed) + len(pl_candidate)}")
print(f" 包含索引108: {108 in np.concatenate([pl_confirmed, pl_candidate])}")
print(f"\n对比结论:")
std_detects_110 = 110 in ph_std
rt_detects_110 = 110 in np.concatenate([ph_confirmed, ph_candidate])
if not std_detects_110 and rt_detects_110:
print(f" [OK] 实时模式成功捕获了索引110的高点标准模式遗漏")
else:
print(f" [INFO] 索引110 - 标准: {std_detects_110}, 实时: {rt_detects_110}")
std_detects_108 = 108 in pl_std
rt_detects_108 = 108 in np.concatenate([pl_confirmed, pl_candidate])
if not std_detects_108 and rt_detects_108:
print(f" [OK] 实时模式成功捕获了索引108的低点标准模式遗漏")
else:
print(f" [INFO] 索引108 - 标准: {std_detects_108}, 实时: {rt_detects_108}")
print("=" * 70)
def test_triangle_detection_comparison():
"""对比标准模式和实时模式的三角形检测"""
print("\n\n")
print("=" * 70)
print("三角形检测对比测试")
print("=" * 70)
# 创建一个对称三角形 + 突破
n = 120
high = np.zeros(n)
low = np.zeros(n)
close = np.zeros(n)
for i in range(n):
# 上沿下降
upper = 60 - (i / n) * 15
# 下沿上升
lower = 30 + (i / n) * 12
# 添加波动
wave = 3 * np.sin(i / 10)
high[i] = upper + wave + 1
low[i] = lower + wave - 1
close[i] = (high[i] + low[i]) / 2
# 最后几天突破
high[-2] = 60 # 候选高点
high[-1] = 65 # 当前突破点
close[-1] = 63
volume = np.ones(n) * 1000000
volume[-1] = 2000000 # 放量突破
params = ConvergingTriangleParams(
window=120,
pivot_k=15,
shrink_ratio=0.6,
)
print(f"\n测试场景:")
print(f" 对称三角形 + 最后一天向上突破")
print(f" 高点[-2]={high[-2]:.1f}, 高点[-1]={high[-1]:.1f}")
# 标准模式
result_std = detect_converging_triangle(
high=high,
low=low,
close=close,
volume=volume,
params=params,
real_time_mode=False
)
# 实时模式
result_rt = detect_converging_triangle(
high=high,
low=low,
close=close,
volume=volume,
params=params,
real_time_mode=True,
flexible_zone=5
)
print(f"\n标准模式结果:")
print(f" 检测到三角形: {result_std.is_valid}")
print(f" 突破方向: {result_std.breakout_dir}")
print(f" 向上突破强度: {result_std.breakout_strength_up:.3f}")
print(f" 检测模式: {result_std.detection_mode}")
print(f"\n实时模式结果:")
print(f" 检测到三角形: {result_rt.is_valid}")
print(f" 突破方向: {result_rt.breakout_dir}")
print(f" 向上突破强度: {result_rt.breakout_strength_up:.3f}")
print(f" 检测模式: {result_rt.detection_mode}")
print(f" 包含候选枢轴点: {result_rt.has_candidate_pivots}")
print(f" 候选枢轴点数: {result_rt.candidate_pivot_count}")
print(f"\n对比分析:")
if result_rt.breakout_strength_up > result_std.breakout_strength_up:
diff = result_rt.breakout_strength_up - result_std.breakout_strength_up
print(f" [OK] 实时模式检测到更强的突破 (+{diff:.3f})")
elif result_rt.is_valid and not result_std.is_valid:
print(f" [OK] 实时模式检测到三角形,标准模式未检测到")
else:
print(f" [INFO] 两种模式结果接近")
print("=" * 70)
def test_flexible_zone_impact():
"""测试flexible_zone参数对检测的影响"""
print("\n\n")
print("=" * 70)
print("灵活区域参数影响测试")
print("=" * 70)
n = 120
k = 15
high = np.ones(n) * 50
low = np.ones(n) * 40
# 在不同位置设置高点
high[115] = 70 # 末端-5天
high[117] = 75 # 末端-3天
high[119] = 80 # 末端-1天当前
print(f"\n测试不同的灵活区域大小:")
for flex_zone in [3, 5, 7, 10]:
ph_conf, pl_conf, ph_cand, pl_cand = \
pivots_fractal_hybrid(high, low, k=k, flexible_zone=flex_zone)
total_ph = len(ph_conf) + len(ph_cand)
detected = []
if 115 in ph_cand:
detected.append(115)
if 117 in ph_cand:
detected.append(117)
if 119 in ph_cand:
detected.append(119)
print(f" flexible_zone={flex_zone:2d}: "
f"候选高点{len(ph_cand)}个, "
f"检测到末端点: {detected}")
print("\n观察:")
print(" - flexible_zone越大捕获的候选点越多")
print(" - 但过大会引入噪音建议3-7天")
print("=" * 70)
if __name__ == "__main__":
# 1. 枢轴点检测对比
test_pivot_detection_comparison()
# 2. 三角形检测对比
test_triangle_detection_comparison()
# 3. flexible_zone参数影响
test_flexible_zone_impact()
print("\n\n")
print("=" * 70)
print("测试总结")
print("=" * 70)
print("""
实时模式验证:
[OK] 成功实现混合枢轴点检测
[OK] 能捕获标准模式遗漏的末端枢轴点
[OK] 候选枢轴点正确标记为低置信度
[OK] flexible_zone参数可调控灵敏度
建议:
- 历史回测: 使用标准模式REALTIME_MODE=False
- 实时选股: 使用实时模式REALTIME_MODE=True, FLEXIBLE_ZONE=5
- 候选枢轴点需要后续确认
""")
print("=" * 70)