technical-patterns-lab/docs/2026-01-26_相向收敛约束改进.md
褚宏光 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

200 lines
4.6 KiB
Markdown
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.

# 相向收敛约束改进
**日期**: 2026-01-26
**问题**: 下降通道被误判为收敛三角形
**解决方案**: 增加"相向收敛"检查,过滤同向运动的通道形态
---
## 问题描述
原算法仅检查斜率范围,允许:
- 上沿斜率 ≤ 0.10(可向上、水平、向下)
- 下沿斜率 ≥ -0.10(可向下、水平、向上)
**导致问题**:上下沿都向下的"下降通道"被识别为三角形
### 误判案例
股票SZ300530 领湃科技
日期20260120
```
上沿:向下倾斜(斜率 < 0
下沿:向下倾斜(斜率 < 0
```
这是典型的**下降通道**,不是收敛三角形,但通过了原检测。
---
## 解决方案
### 新增约束:相向收敛检查
`src/converging_triangle.py``detect_converging_triangle()` 函数中,
斜率检查后增加:
```python
# 相向收敛检查:确保上下沿是相向运动的(关键约束)
slope_tolerance = 0.01 # 斜率容差,用于判断"接近水平"
# 检查是否同向运动
both_descending = (a_u < -slope_tolerance) and (a_l < -slope_tolerance) # 都向下
both_ascending = (a_u > slope_tolerance) and (a_l > slope_tolerance) # 都向上
if both_descending or both_ascending:
# 这是通道形态,不是收敛三角形
return invalid_result
```
### 判定逻辑
#### ✅ 通过(真正的收敛三角形)
1. **对称三角形**:上沿向下 + 下沿向上
2. **上升三角形**:上沿水平 + 下沿向上
3. **下降三角形**:上沿向下 + 下沿水平
#### ❌ 拒绝(通道形态)
1. **下降通道**:上沿向下 + 下沿向下(同向↓)
2. **上升通道**:上沿向上 + 下沿向上(同向↑)
### 斜率判定
使用 `slope_tolerance = 0.01` 作为阈值:
- 斜率 < -0.01 向下
- 斜率 > +0.01 → 向上
- -0.01 ~ +0.01 → 水平
---
## 测试验证
运行测试脚本:
```bash
python scripts/test_slope_constraint.py
```
### 测试结果
| 形态 | 上沿斜率 | 下沿斜率 | 预期 | 实际 | 结果 |
|------|---------|---------|------|------|------|
| 对称三角形 | -0.050 | +0.050 | PASS | PASS | ✓ |
| 下降三角形 | -0.080 | +0.001 | PASS | PASS | ✓ |
| 上升三角形 | +0.001 | +0.080 | PASS | PASS | ✓ |
| 下降通道 | -0.050 | -0.080 | REJECT | REJECT | ✓ |
| 上升通道 | +0.050 | +0.080 | REJECT | REJECT | ✓ |
| 微弱收敛 | -0.005 | +0.005 | PASS | PASS | ✓ |
| 水平矩形 | +0.001 | -0.001 | PASS | PASS | ✓ |
**全部测试通过**
---
## 预期影响
### 对检测结果的影响
- **减少误报**:过滤掉下降/上升通道
- **提高精度**:只保留真正的收敛三角形
- **候选数量**:预计减少 10%-20%(取决于市场走势)
### 对现有数据的影响
重新运行检测后:
- 之前识别的下降通道(如 SZ300530将被过滤
- 对称/上升/下降三角形不受影响
- 需要重新生成报告和图表
---
## 重新运行检测
```bash
# 激活环境
.\.venv\Scripts\Activate.ps1
# 重新运行完整流水线
python scripts/pipeline_converging_triangle.py
```
或分步运行:
```bash
# 1. 重新检测
python scripts/run_converging_triangle.py
# 2. 生成报告
python scripts/report_converging_triangles.py
# 3. 绘制图表
python scripts/plot_converging_triangles.py
```
---
## 相关文件
- **核心改进**: `src/converging_triangle.py` (第449-467行)
- **参数配置**: `scripts/triangle_config.py` (增加注释说明)
- **测试脚本**: `scripts/test_slope_constraint.py` (新增)
- **本文档**: `docs/2026-01-26_相向收敛约束改进.md`
---
## 附录:三角形形态分类
### 标准收敛三角形Converging Triangle
```
对称三角形 (Symmetrical Triangle)
/\
/ \↓ 上沿向下
/ \
/ \
/___↑____\ 下沿向上
上升三角形 (Ascending Triangle)
————————— 上沿水平
/
/
/_____↑_\ 下沿向上
下降三角形 (Descending Triangle)
\↓
\ 上沿向下
\
_______\ 下沿水平
```
### 非收敛形态(应被过滤)
```
下降通道 (Descending Channel)
\↓
\ 上沿向下
\
\↓ 下沿向下(同向)
上升通道 (Ascending Channel)
/↑ 上沿向上(同向)
/
/↑
/ 下沿向上
```
---
## 总结
通过增加"相向收敛"约束,算法现在能够正确区分:
- ✅ 收敛三角形(相向运动,符合技术分析定义)
- ❌ 上升/下降通道(同向运动,不是三角形)
这一改进提高了检测的准确性和可靠性,使其更符合技术分析的标准定义。