- Introduced an interactive HTML stock viewer for visualizing strength scores and filtering stocks based on user-defined thresholds. - Added `--all-stocks` parameter to generate charts for all 108 stocks, including those not meeting convergence criteria. - Implemented a new scoring system for breakout strength, incorporating fitting adherence to improve accuracy. - Updated multiple documentation files, including usage instructions and feature overviews, to reflect recent enhancements. - Improved error handling and file naming conventions to ensure compatibility across platforms.
82 lines
2.7 KiB
Markdown
82 lines
2.7 KiB
Markdown

|
||
|
||
拟合线不好,需要使用 "凸优化经典算法"。
|
||
最终是希望 上沿线或下沿线,包含大部分的 枢轴点。
|
||
|
||
---
|
||
|
||
## 已实现:凸优化拟合方法(2026-01-27)
|
||
|
||
### 新增参数
|
||
|
||
```python
|
||
fitting_method: str = "iterative" # "iterative" | "lp" | "quantile" | "anchor"
|
||
```
|
||
|
||
### 拟合方法对比
|
||
|
||
| 方法 | 说明 | 优点 | 缺点 |
|
||
|------|------|------|------|
|
||
| **iterative** | 迭代离群点移除 + 最小二乘法 | 稳定保守,已有调参经验 | 线"穿过"数据而非"包住" |
|
||
| **lp** | 线性规划凸优化 | 数学严谨,保证边界包络 | 对极端值敏感 |
|
||
| **quantile** | 分位数回归 (上95%/下5%) | 统计稳健,抗异常值 | 计算稍慢 |
|
||
| **anchor** | 绝对极值锚点 + 斜率优化 | 锚点明确,线更贴近主趋势 | 对枢轴点数量较敏感 |
|
||
|
||
### LP 方法数学原理
|
||
|
||
**上沿问题 (找"天花板",最紧的包络)**:
|
||
```
|
||
minimize Σ(a*x_i + b - y_i) 线与点的总距离
|
||
subject to y_i ≤ a * x_i + b 所有点在线下方
|
||
-0.5 ≤ a ≤ 0.5 斜率限制
|
||
```
|
||
|
||
**下沿问题 (找"地板",最紧的包络)**:
|
||
```
|
||
minimize Σ(y_i - a*x_i - b) 线与点的总距离
|
||
subject to y_i ≥ a * x_i + b 所有点在线上方
|
||
-0.5 ≤ a ≤ 0.5 斜率限制
|
||
```
|
||
|
||
这确保拟合线严格"包住"所有枢轴点,且尽量贴近数据,符合技术分析中"压力线/支撑线"的语义。
|
||
|
||
### Anchor 方法思路
|
||
|
||
**核心目标**:固定锚点,优化斜率,使大部分枢轴点在边界线正确一侧。
|
||
|
||
- 锚点:检测窗口内的绝对最高/最低点(排除最后1天用于突破判断)
|
||
- 上沿:找最“平缓”的下倾线,使 >=95% 枢轴高点在上沿线下方
|
||
- 下沿:找最“平缓”的上倾线,使 >=95% 枢轴低点在下沿线上方
|
||
- 实现:对斜率做二分搜索,满足覆盖率约束后取最贴近的一条线
|
||
|
||
### 测试验证
|
||
|
||
```
|
||
上沿 LP: slope=-0.006667, intercept=10.5333
|
||
验证(线-点): [0.033, 0.000, 0.067, 0.033, 0.000] (全>=0,线在点上方)
|
||
下沿 LP: slope=0.005000, intercept=8.0000
|
||
验证(点-线): [0.00, 0.05, 0.00, 0.05, 0.00] (全>=0,线在点下方)
|
||
```
|
||
|
||
### 使用方法
|
||
|
||
```python
|
||
from src.converging_triangle import ConvergingTriangleParams, detect_converging_triangle
|
||
|
||
# 使用凸优化/统计方法
|
||
params = ConvergingTriangleParams(
|
||
fitting_method="lp", # 或 "quantile" / "anchor"
|
||
# ... 其他参数
|
||
)
|
||
|
||
result = detect_converging_triangle(high, low, close, volume, params)
|
||
```
|
||
|
||
### 实现位置
|
||
|
||
- 参数类: `ConvergingTriangleParams.fitting_method`
|
||
- LP拟合: `fit_boundary_lp()`
|
||
- 分位数回归: `fit_boundary_quantile()`
|
||
- 锚点拟合: `fit_boundary_anchor()`
|
||
- 分发函数: `fit_pivot_line_dispatch()`
|