technical-patterns-lab/scripts/triangle_config.py
褚宏光 3538b214ba Enhance stock analysis features with K线图 and daily best reporting
- Upgraded charting functionality from line graphs to K线图 for improved technical analysis.
- Introduced a new daily best stocks report, outputting the top-performing stocks over the last 500 days.
- Implemented automatic logging of execution details for better traceability.
- Updated the .gitignore to include new output files related to the K线图 and logs.

Files modified:
- scripts/plot_converging_triangles.py: Enhanced to support K线图 rendering.
- scripts/run_converging_triangle.py: Added logging and daily best reporting features.
- README.md: Updated to reflect new features and usage instructions.
- New files: docs/K线图说明.md for detailed K线图 usage and features.
2026-01-29 09:09:29 +08:00

227 lines
7.9 KiB
Python
Raw 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.

"""
收敛三角形检测参数配置
所有相关脚本共享此配置,确保参数一致性:
- run_converging_triangle.py (批量检测)
- report_converging_triangles.py (报告生成)
- plot_converging_triangles.py (图表绘制)
- pipeline_converging_triangle.py (一键流水线)
"""
import os
import sys
# 让脚本能找到 src/ 下的模块
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
from converging_triangle import ConvergingTriangleParams
# ============================================================================
# 核心检测参数
# ============================================================================
# 严格模式:更严格的收敛和突破要求(当前激活)
DETECTION_PARAMS = ConvergingTriangleParams(
# 基础窗口
window=240, # 检测窗口大小(交易日)
pivot_k=15, # 枢轴点检测周期
# 边界拟合
boundary_n_segments=2, # 边界线分段数
boundary_source="full", # 边界拟合数据源: "full"(全部) 或 "pivot"(仅枢轴点)
fitting_method="anchor", # 🆕 拟合方法: "iterative" | "lp" | "quantile" | "anchor"
# anchor方法固定绝对极值点为锚点二分搜索最优斜率使95%点在正确一侧
# 斜率约束(严格收敛三角形)
upper_slope_max=0, # 上沿必须向下或水平≤0
lower_slope_min=0, # 下沿必须向上或水平≥0
# 注意:算法会自动过滤"同向通道"(上下沿都向上或都向下)
# 只保留真正的收敛形态(上下沿相向运动)
# 触碰检测
touch_tol=0.10, # 触碰容差10%以内算触碰)
touch_loss_max=0.10, # 平均触碰误差上限
# 收敛度要求
shrink_ratio=0.45, # 🔧 更严格末端≤45%起始宽度
# 突破检测
break_tol=0.005, # 🔧 更明显的突破0.5%
# 成交量确认
vol_window=20, # 成交量移动窗口
vol_k=1.5, # 🔧 更强的放量要求
# 假突破过滤
false_break_m=5, # 假突破回看天数
)
# 默认模式(备用)
DEFAULT_PARAMS = ConvergingTriangleParams(
window=120,
pivot_k=15,
boundary_n_segments=2,
boundary_source="full",
upper_slope_max=0.10,
lower_slope_min=-0.10,
touch_tol=0.10,
touch_loss_max=0.10,
shrink_ratio=0.8,
break_tol=0.001,
vol_window=20,
vol_k=1.3,
false_break_m=5,
)
# ============================================================================
# 数据范围配置
# ============================================================================
# 计算范围None = 全部历史,具体数字 = 最近N天
RECENT_DAYS = 500 # 建议: 500天(默认) | 250天(快速) | 100天(极速)
# 显示范围:图表中显示的交易日数
DISPLAY_WINDOW = 500
# ============================================================================
# 实时模式配置
# ============================================================================
# 是否启用实时模式(混合策略)
REALTIME_MODE = True # True=实时模式(默认), False=标准模式
# 灵活区域大小(仅在实时模式下生效)
FLEXIBLE_ZONE = 15 # 最近15天使用降低标准与pivot_k一致
# 说明: 设为与pivot_k相同确保窗口末端的所有潜在枢轴点都能被识别
# 实时模式说明:
# - 实时模式(推荐):使用确认+候选枢轴点(允许右边数据不完整),适合实时选股
# - 优点:无滞后,能捕获最近的突破
# - 缺点:候选枢轴点置信度低,可能随后续数据变化
# - 标准模式仅使用确认枢轴点完整左右k天数据适合历史回测
# - 有15天确认滞后但枢轴点质量高
# ============================================================================
# 输出控制
# ============================================================================
# 是否只输出有效三角形
ONLY_VALID = True
# 是否显示详细日志
VERBOSE = True
# 图表详细模式(显示枢轴点、分段线等调试信息)
SHOW_CHART_DETAILS = False # False=简洁模式默认True=详细模式
# 简洁模式:仅显示收盘价、上沿线、下沿线
# 详细模式:额外显示所有枢轴点、拟合点、分段线等调试信息
# ============================================================================
# 性能优化配置
# ============================================================================
# 是否使用v2优化版本预计算枢轴点
USE_V2_OPTIMIZATION = False # True=v2优化False=v1原版推荐
# v2优化通过预计算整个时间序列的枢轴点避免滑动窗口重复计算
# 预期加速3-5x
# 注意v2版本暂不支持实时模式REALTIME_MODE会自动回退到标准模式
# ============================================================================
# 推荐参数预设(备选方案)
# ============================================================================
# 宽松模式:捕获更多潜在形态
LOOSE_PARAMS = ConvergingTriangleParams(
window=120,
pivot_k=15,
boundary_n_segments=2,
boundary_source="full",
upper_slope_max=0.15, # 允许更陡峭的斜率
lower_slope_min=-0.15,
touch_tol=0.12,
touch_loss_max=0.12,
shrink_ratio=0.85, # 更宽松的收敛要求
break_tol=0.001,
vol_window=20,
vol_k=1.2,
false_break_m=5,
)
# ============================================================================
# 使用说明
# ============================================================================
def get_params(mode: str = "strict") -> ConvergingTriangleParams:
"""
获取检测参数
Args:
mode: 参数模式,可选值:
- "strict": 严格模式(当前默认,推荐)
- "default": 默认参数
- "loose": 宽松模式
Returns:
ConvergingTriangleParams 实例
"""
if mode == "default":
return DEFAULT_PARAMS
elif mode == "loose":
return LOOSE_PARAMS
else:
return DETECTION_PARAMS # strict
def get_realtime_config() -> tuple:
"""
获取实时模式配置
Returns:
(real_time_mode, flexible_zone)
"""
return REALTIME_MODE, FLEXIBLE_ZONE
if __name__ == "__main__":
print("=" * 70)
print("收敛三角形检测参数配置")
print("=" * 70)
print("\n[当前激活: 严格模式]")
print(f" 检测窗口: {DETECTION_PARAMS.window} 个交易日")
print(f" 收敛比例: ≤ {DETECTION_PARAMS.shrink_ratio} (末端宽度/起始宽度)")
print(f" 突破阈值: {DETECTION_PARAMS.break_tol * 100:.2f}%")
print(f" 放量倍数: ≥ {DETECTION_PARAMS.vol_k}x")
print("\n[数据范围]")
print(f" 计算范围: 最近 {RECENT_DAYS} 个交易日")
print(f" 图表显示: {DISPLAY_WINDOW} 个交易日")
print("\n[实时模式]")
print(f" 实时模式: {'开启' if REALTIME_MODE else '关闭'}")
if REALTIME_MODE:
print(f" 灵活区域: {FLEXIBLE_ZONE}")
print("\n[性能优化]")
print(f" v2优化: {'开启' if USE_V2_OPTIMIZATION else '关闭(推荐)'}")
print(f" - v1: 稳定版本支持实时模式性能优秀2.5秒/54k点")
print(f" - v2: 预计算枢轴点,不支持实时模式(实验性)")
print("\n[可选模式]")
print(" - strict: 严格模式(当前使用,高质量)")
print(" - default: 默认参数(较宽松)")
print(" - loose: 宽松模式(更多候选)")
print("\n[关键差异]")
print(" 参数 | 严格模式 | 默认模式 | 宽松模式")
print(" ------------|---------|---------|----------")
print(" 收敛比例 | ≤0.45 | ≤0.8 | ≤0.85")
print(" 突破阈值 | 0.5% | 0.1% | 0.1%")
print(" 放量倍数 | ≥1.5x | ≥1.3x | ≥1.2x")
print("=" * 70)