- 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.
231 lines
7.4 KiB
Python
231 lines
7.4 KiB
Python
"""
|
||
FLEXIBLE_ZONE 参数效果演示
|
||
|
||
展示不同 FLEXIBLE_ZONE 值对候选枢轴点检测的影响
|
||
"""
|
||
|
||
import numpy as np
|
||
import sys
|
||
import os
|
||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
|
||
from converging_triangle import pivots_fractal, pivots_fractal_hybrid
|
||
|
||
|
||
def demonstrate_flexible_zone():
|
||
"""演示 FLEXIBLE_ZONE 参数的实际效果"""
|
||
|
||
print("=" * 80)
|
||
print("FLEXIBLE_ZONE 参数效果演示")
|
||
print("=" * 80)
|
||
|
||
# 创建120天的测试数据
|
||
n = 120
|
||
k = 15
|
||
|
||
high = np.ones(n) * 50
|
||
low = np.ones(n) * 40
|
||
|
||
# 在不同位置设置明显的高点
|
||
high[50] = 70 # 中间(索引50) - 标准模式可检测
|
||
high[90] = 75 # 中后(索引90) - 标准模式可检测
|
||
high[105] = 78 # 边缘(索引105) - 标准模式不可检测
|
||
high[110] = 80 # 后部(索引110) - 灵活区域(flex≥10)
|
||
high[115] = 82 # 后部(索引115) - 灵活区域(flex≥5)
|
||
high[117] = 85 # 末端(索引117) - 灵活区域(flex≥3)
|
||
high[119] = 88 # 当前(索引119) - 灵活区域(flex≥1)
|
||
|
||
print(f"\n测试配置:")
|
||
print(f" 窗口大小: {n}天")
|
||
print(f" 枢轴参数 k: {k}")
|
||
print(f" 标准模式可检测范围: 索引 {k} ~ {n-k-1}")
|
||
print(f"")
|
||
print(f"设置的关键高点:")
|
||
print(f" 索引 50 (价格70): 标准模式可检测 [OK]")
|
||
print(f" 索引 90 (价格75): 标准模式可检测 [OK]")
|
||
print(f" 索引 105 (价格78): 标准模式不可检测 [X]")
|
||
print(f" 索引 110 (价格80): 需要 flex>=10")
|
||
print(f" 索引 115 (价格82): 需要 flex>=5")
|
||
print(f" 索引 117 (价格85): 需要 flex>=3")
|
||
print(f" 索引 119 (价格88): 需要 flex>=1")
|
||
|
||
# 标准模式基准
|
||
print(f"\n{'='*80}")
|
||
print("标准模式(基准)")
|
||
print("="*80)
|
||
ph_std, pl_std = pivots_fractal(high, low, k=k)
|
||
print(f"检测到高点枢轴: {len(ph_std)}个")
|
||
print(f" 索引: {ph_std}")
|
||
print(f" 最高点价格: {high[ph_std].max() if len(ph_std) > 0 else 0:.0f}")
|
||
print(f" 遗漏的高点: 索引105(78), 110(80), 115(82), 117(85), 119(88)")
|
||
|
||
# 测试不同的 FLEXIBLE_ZONE
|
||
print(f"\n{'='*80}")
|
||
print("实时模式 - 不同 FLEXIBLE_ZONE 对比")
|
||
print("="*80)
|
||
|
||
flex_zones = [1, 3, 5, 7, 10, 15]
|
||
|
||
for flex in flex_zones:
|
||
ph_conf, pl_conf, ph_cand, pl_cand = \
|
||
pivots_fractal_hybrid(high, low, k=k, flexible_zone=flex)
|
||
|
||
total_ph = len(ph_conf) + len(ph_cand)
|
||
|
||
# 找出捕获的关键点
|
||
captured = []
|
||
for idx in [110, 115, 117, 119]:
|
||
if idx in ph_cand:
|
||
captured.append(f"{idx}({high[idx]:.0f})")
|
||
|
||
max_price = 0
|
||
if len(ph_conf) > 0:
|
||
max_price = high[ph_conf].max()
|
||
if len(ph_cand) > 0:
|
||
max_price = max(max_price, high[ph_cand].max())
|
||
|
||
print(f"\nFLEXIBLE_ZONE = {flex:2d}:")
|
||
print(f" 灵活区域范围: 索引 {max(k, n-flex)} ~ {n-1}")
|
||
print(f" 确认高点: {len(ph_conf)}个")
|
||
print(f" 候选高点: {len(ph_cand)}个 {ph_cand if len(ph_cand) > 0 else ''}")
|
||
print(f" 总高点: {total_ph}个")
|
||
print(f" 捕获的关键点: {', '.join(captured) if captured else '无'}")
|
||
print(f" 最高点价格: {max_price:.0f}")
|
||
|
||
print(f"\n{'='*80}")
|
||
print("观察与建议")
|
||
print("="*80)
|
||
print("""
|
||
观察:
|
||
1. FLEXIBLE_ZONE 越大,捕获的候选点越多
|
||
2. 但候选点质量下降(右边确认数据不足)
|
||
3. FLEXIBLE_ZONE=5 是平衡点(最后5天)
|
||
|
||
建议:
|
||
保守: FLEXIBLE_ZONE=3 - 只捕获最近3天,质量高
|
||
平衡: FLEXIBLE_ZONE=5 - 最近5天,推荐配置 ★
|
||
激进: FLEXIBLE_ZONE=10 - 最近10天,信号多但噪音大
|
||
|
||
实际效果:
|
||
flex=3: 捕获索引117-119,提前发现3天内的高点
|
||
flex=5: 捕获索引115-119,提前发现5天内的高点 ★
|
||
flex=10: 捕获索引110-119,提前发现10天内的高点
|
||
""")
|
||
print("="*80)
|
||
|
||
|
||
def demonstrate_realworld_scenario():
|
||
"""实际场景演示:突破中的三角形"""
|
||
|
||
print("\n\n")
|
||
print("=" * 80)
|
||
print("实际场景:股票正在突破三角形")
|
||
print("=" * 80)
|
||
|
||
n = 120
|
||
k = 15
|
||
|
||
# 模拟对称三角形 + 突破
|
||
high = np.zeros(n)
|
||
low = 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
|
||
|
||
# 最后几天突破
|
||
high[115] = 52 # 5天前
|
||
high[117] = 58 # 3天前
|
||
high[119] = 65 # 今天(突破!)
|
||
|
||
print(f"\n场景描述:")
|
||
print(f" 形态: 对称三角形")
|
||
print(f" 当前状态: 正在向上突破")
|
||
print(f" 关键高点: 索引115(52), 117(58), 119(65)")
|
||
|
||
# 标准模式
|
||
ph_std, pl_std = pivots_fractal(high, low, k=k)
|
||
print(f"\n标准模式检测:")
|
||
print(f" 检测到高点: {len(ph_std)}个")
|
||
if len(ph_std) > 0:
|
||
last_idx = ph_std[-1]
|
||
print(f" 最近高点: 索引{last_idx} (价格{high[last_idx]:.1f})")
|
||
print(f" 距离当前: {n-1-last_idx}天前")
|
||
print(f" 问题: 遗漏了索引115-119的突破高点 [X]")
|
||
|
||
# 不同 FLEXIBLE_ZONE 的效果
|
||
print(f"\n实时模式对比:")
|
||
|
||
for flex in [3, 5, 10]:
|
||
ph_conf, pl_conf, ph_cand, pl_cand = \
|
||
pivots_fractal_hybrid(high, low, k=k, flexible_zone=flex)
|
||
|
||
all_ph = np.concatenate([ph_conf, ph_cand]) if len(ph_cand) > 0 else ph_conf
|
||
|
||
captured = []
|
||
for idx in [115, 117, 119]:
|
||
if idx in ph_cand:
|
||
captured.append(idx)
|
||
|
||
print(f"\n FLEXIBLE_ZONE={flex}:")
|
||
if len(all_ph) > 0:
|
||
last_idx = all_ph[-1]
|
||
is_candidate = last_idx in ph_cand
|
||
print(f" 最近高点: 索引{last_idx} (价格{high[last_idx]:.1f})")
|
||
print(f" 类型: {'候选枢轴点' if is_candidate else '确认枢轴点'}")
|
||
print(f" 捕获突破点: {captured}")
|
||
|
||
if 119 in ph_cand:
|
||
print(f" [OK] 能检测到当日突破!")
|
||
elif 117 in ph_cand:
|
||
print(f" [OK] 能检测到近期突破(滞后2天)")
|
||
elif 115 in ph_cand:
|
||
print(f" [!] 能检测到较早突破(滞后4天)")
|
||
|
||
print(f"\n结论:")
|
||
print(f" FLEXIBLE_ZONE=3: 滞后2天,但质量高")
|
||
print(f" FLEXIBLE_ZONE=5: 捕获当日突破,推荐 ★")
|
||
print(f" FLEXIBLE_ZONE=10: 捕获更多,但可能有噪音")
|
||
print("="*80)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 1. 基础演示
|
||
demonstrate_flexible_zone()
|
||
|
||
# 2. 实际场景
|
||
demonstrate_realworld_scenario()
|
||
|
||
print("\n\n")
|
||
print("=" * 80)
|
||
print("总结")
|
||
print("=" * 80)
|
||
print("""
|
||
FLEXIBLE_ZONE 参数理解:
|
||
|
||
1. 含义:
|
||
最近几天可以"降低标准"检测枢轴点
|
||
|
||
2. 作用:
|
||
- 控制实时模式的激进程度
|
||
- 平衡质量和实时性
|
||
|
||
3. 推荐值:
|
||
FLEXIBLE_ZONE = 5 (最近5天)
|
||
|
||
4. 权衡:
|
||
值越小 → 质量越高,但提前发现能力弱
|
||
值越大 → 提前发现,但误报风险高
|
||
|
||
5. 配置位置:
|
||
scripts/triangle_config.py
|
||
|
||
6. 只在实时模式下生效:
|
||
REALTIME_MODE = True 时才使用
|
||
""")
|
||
print("=" * 80)
|
||
|