fix: ECR-004 威科夫区间评分硬化与 VP 绘图减负(已审)

评分选 TR、阶段最小跨度、elements_only 门闩、Top-8 VP;无币种独立参数。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
jackyu66git
2026-08-06 18:46:08 +08:00
co-authored by Cursor
parent ac6be80278
commit d3188ca83c
19 changed files with 275 additions and 89 deletions
+25 -18
View File
@@ -187,29 +187,25 @@ def build_phases(
tr: Dict[str, Any],
bias: str,
events: List[Dict[str, Any]],
min_bars: int = 3,
) -> List[Dict[str, Any]]:
"""按时间切分 AE 粗阶段。"""
"""按时间切分 AE 粗阶段;保证非重叠且每段至少 min_bars 根(空间不足则截断尾部阶段)"""
s = int(tr["abs_start_idx"])
e = int(tr["abs_end_idx"])
hi = float(tr["high"])
lo = float(tr["low"])
mid = float(tr["mid"])
tol = float(tr.get("tol") or (hi - lo) * 0.05)
n_last = len(df) - 1
min_span = max(2, min_bars - 1)
event_idx = {}
for ev in events:
# 找回 idx 近似:按时间匹配
t = ev.get("time")
for i in range(s, min(len(df), e + 20)):
if _bar_time(df, i) == t:
event_idx[ev["type"]] = i
break
# 分段点
a_end = s + max(3, (e - s) // 5)
a_end = s + max(min_bars, (e - s) // 5)
c_anchor = event_idx.get("Spring") or event_idx.get("UTAD") or (s + (e - s) // 2)
d_anchor = event_idx.get("SOS") or event_idx.get("SOW") or e
e_start = d_anchor
def _lab(phase: str) -> str:
if bias == "distribution":
@@ -218,17 +214,27 @@ def build_phases(
m = {"A": "A停止下跌", "B": "B筑底", "C": "C测试", "D": "D拉升", "E": "E离开"}
return m.get(phase, phase)
cuts = [
# 理想切点(随后再强制非重叠 + 最小跨度)
raw = [
("A", s, a_end),
("B", a_end, max(a_end + 1, c_anchor)),
("C", max(a_end + 1, c_anchor), max(c_anchor + 1, d_anchor)),
("D", max(c_anchor + 1, d_anchor), max(d_anchor + 1, min(len(df) - 1, e_start + max(3, (e - s) // 6)))),
("E", max(d_anchor, e_start), min(len(df) - 1, max(e, e_start + 5))),
("B", a_end, c_anchor),
("C", c_anchor, d_anchor),
("D", d_anchor, min(n_last, d_anchor + max(min_bars, (e - s) // 6))),
("E", min(n_last, d_anchor + max(min_bars, (e - s) // 6)), min(n_last, max(e, d_anchor + max(min_bars * 2, 8)))),
]
phases = []
for phase, a, b in cuts:
a = int(np.clip(a, 0, len(df) - 1))
b = int(np.clip(b, a, len(df) - 1))
phases: List[Dict[str, Any]] = []
cursor = s
for phase, _a, _b in raw:
if cursor >= n_last:
break
a = max(int(_a), cursor)
b = int(max(_b, a + min_span))
b = int(np.clip(b, a, n_last))
if b - a < min_span:
# 尾部空间不足:并入上一段终点并停止新增
if phases:
phases[-1]["end_time"] = _bar_time(df, n_last)
break
phases.append(
{
"phase": phase,
@@ -237,4 +243,5 @@ def build_phases(
"end_time": _bar_time(df, b),
}
)
cursor = b
return phases
+23 -3
View File
@@ -1,4 +1,4 @@
"""交易区间检测:ATR 容差下近期震荡箱。"""
"""交易区间检测:ATR 容差下按评分选取近期震荡箱。"""
from __future__ import annotations
from typing import Any, Dict, Optional
@@ -23,6 +23,20 @@ def _atr(df: pd.DataFrame, period: int = 14) -> pd.Series:
return tr.rolling(period, min_periods=max(3, period // 2)).mean()
def _score_segment(
length: int,
near_hi: int,
near_lo: int,
inside: float,
width: float,
atr: float,
) -> float:
"""触边密度 + 箱内比例 − 相对宽度;弱奖励长度以免只追最长。"""
touch_density = (near_hi + near_lo) / float(max(length, 1))
width_pen = (width / atr) if atr > 0 else width
return touch_density * 50.0 + float(inside) * 30.0 - width_pen * 3.0 + min(length / 40.0, 2.0)
def detect_trading_range(
df: pd.DataFrame,
lookback: int = 120,
@@ -33,6 +47,7 @@ def detect_trading_range(
"""
在最近 lookback 根内寻找高低点波动受控的连续段作为交易区间。
尾部预留 tail_reserve 根用于事件(Spring/SOS),不参与箱体边界计算。
在硬门槛之上按评分取最优段(非仅最长窗口)。
"""
if df is None or len(df) < min_bars + 5:
return None
@@ -54,6 +69,7 @@ def detect_trading_range(
last_atr = float(core["close"].iloc[-1]) * 0.01
best = None
best_score = float("-inf")
cn = len(core)
for length in range(min(cn, lookback), min_bars - 1, -4):
seg = core.iloc[-length:]
@@ -67,14 +83,18 @@ def detect_trading_range(
near_lo = int((seg["low"] <= lo + tol).sum())
if near_hi < 2 or near_lo < 2:
continue
inside = ((seg["close"] >= lo - tol) & (seg["close"] <= hi + tol)).mean()
inside = float(((seg["close"] >= lo - tol) & (seg["close"] <= hi + tol)).mean())
if inside < 0.75:
continue
score = _score_segment(length, near_hi, near_lo, inside, width, last_atr)
if score <= best_score:
continue
start_i = cn - length
end_i = cn - 1
mid = (hi + lo) / 2.0
last_c = float(work["close"].iloc[-1])
active = (lo - tol * 1.5) <= last_c <= (hi + tol * 1.5)
best_score = score
best = {
"start_idx": int(start_i),
"end_idx": int(end_i),
@@ -85,8 +105,8 @@ def detect_trading_range(
"atr": last_atr,
"tol": tol,
"bars": int(length),
"score": float(score),
}
break
if best is None:
return None