- 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.
204 lines
6.6 KiB
Python
204 lines
6.6 KiB
Python
"""
|
||
收敛三角形检测参数配置
|
||
|
||
所有相关脚本共享此配置,确保参数一致性:
|
||
- 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"(仅枢轴点)
|
||
|
||
# 斜率约束
|
||
upper_slope_max=0.10, # 上沿最大斜率(正值,向上倾斜)
|
||
lower_slope_min=-0.10, # 下沿最小斜率(负值,向下倾斜)
|
||
# 注意:算法会自动过滤"同向通道"(上下沿都向上或都向下)
|
||
# 只保留真正的收敛形态(上下沿相向运动)
|
||
|
||
# 触碰检测
|
||
touch_tol=0.10, # 触碰容差(10%以内算触碰)
|
||
touch_loss_max=0.10, # 平均触碰误差上限
|
||
|
||
# 收敛度要求
|
||
shrink_ratio=0.6, # 🔧 更严格:末端≤60%起始宽度
|
||
|
||
# 突破检测
|
||
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
|
||
|
||
# 显示范围:图表中显示的交易日数
|
||
DISPLAY_WINDOW = 500
|
||
|
||
|
||
# ============================================================================
|
||
# 实时模式配置
|
||
# ============================================================================
|
||
|
||
# 是否启用实时模式(混合策略)
|
||
REALTIME_MODE = True # True=实时模式(默认), False=标准模式
|
||
|
||
# 灵活区域大小(仅在实时模式下生效)
|
||
FLEXIBLE_ZONE = 5 # 最近5天使用降低标准
|
||
# 建议: 3-7天,太大会引入噪音
|
||
|
||
# 实时模式说明:
|
||
# - 实时模式(推荐):使用确认+候选枢轴点(允许右边数据不完整),适合实时选股
|
||
# - 优点:无滞后,能捕获最近的突破
|
||
# - 缺点:候选枢轴点置信度低,可能随后续数据变化
|
||
# - 标准模式:仅使用确认枢轴点(完整左右k天数据),适合历史回测
|
||
# - 有15天确认滞后,但枢轴点质量高
|
||
|
||
|
||
# ============================================================================
|
||
# 输出控制
|
||
# ============================================================================
|
||
|
||
# 是否只输出有效三角形
|
||
ONLY_VALID = True
|
||
|
||
# 是否显示详细日志
|
||
VERBOSE = True
|
||
|
||
# 图表详细模式(显示枢轴点、分段线等调试信息)
|
||
SHOW_CHART_DETAILS = False # False=简洁模式(默认),True=详细模式
|
||
# 简洁模式:仅显示收盘价、上沿线、下沿线
|
||
# 详细模式:额外显示所有枢轴点、拟合点、分段线等调试信息
|
||
|
||
# ============================================================================
|
||
# 推荐参数预设(备选方案)
|
||
# ============================================================================
|
||
|
||
# 宽松模式:捕获更多潜在形态
|
||
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(" - strict: 严格模式(当前使用,高质量)")
|
||
print(" - default: 默认参数(较宽松)")
|
||
print(" - loose: 宽松模式(更多候选)")
|
||
|
||
print("\n[关键差异]")
|
||
print(" 参数 | 严格模式 | 默认模式 | 宽松模式")
|
||
print(" ------------|---------|---------|----------")
|
||
print(" 收敛比例 | ≤0.6 | ≤0.8 | ≤0.85")
|
||
print(" 突破阈值 | 0.5% | 0.1% | 0.1%")
|
||
print(" 放量倍数 | ≥1.5x | ≥1.3x | ≥1.2x")
|
||
print("=" * 70)
|