The confluence engine had nothing to work with. Daily moving averages were the only level source, and they sat 163 to 697 points from price, so every cluster had exactly one member and no alert could ever fire. Two new sources, chosen for having a real following — the engine is a bet that many participants watch the same price, which is what makes a level hold: - Prior day high/low/close, from the last *closed* daily bar so mid-session the levels do not silently switch to today's own developing range. Full daily weight rather than the 0.75 average discount: a traded high is structure, not a derived average. - Session VWAP, anchored to the 18:00 ET open like the daily bars. Institutional execution is benchmarked against it, and zero-volume overnight minutes are skipped rather than dividing by zero. Both are stamped 1d, so they get their own colours to stay distinguishable from the daily averages. Prior-day levels draw as price lines, which span the chart and label the axis instead of relying on bar-index interpolation. VWAP re-prices every minute while a daily average carries hundreds of points and changes once a session, so broadcasting the whole level set on the VWAP cadence would have pushed the entire history every minute. Levels now go out as a delta that clients merge by id. Adding the levels then exposed two defects that had been invisible while nothing could cluster: - Cluster identity was sha1(side + round(center / tolerance)), and tolerance derives from ATR, so it changed every bar. The same zone was continually issued a new id, never matched the cooldown table, and the cooldown did nothing. Identity is now the set of converging levels. - Alert suppression keyed on that identity, so a level drifting in or out of a group read as a new zone. It now suppresses by proximity: two zones within an ATR are the same zone, and the strongest is the one reported. Over six replayed sessions at threshold 28 that is 247 alerts, then 54, then 40; raising the cooldown to 4h — which only affects repeats of the same area, never a genuinely new zone — gives 17 total with a worst session of 9. calibrate_alerts.py now sweeps threshold and cooldown together in one pass, since the threshold turns out to be quantised and nearly useless as a control. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
91 lines
3.1 KiB
Python
91 lines
3.1 KiB
Python
from dataclasses import dataclass
|
|
from hashlib import sha1
|
|
from typing import Any
|
|
|
|
from app.analysis.levels import Level, Side
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Cluster:
|
|
id: str
|
|
side: Side
|
|
low: float
|
|
high: float
|
|
center: float
|
|
score: float
|
|
members: list[Level]
|
|
distance: float
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
# Built field by field rather than via asdict(), which would deep-copy
|
|
# every member's point history before we replaced it with summaries.
|
|
return {
|
|
"id": self.id,
|
|
"side": self.side.value,
|
|
"low": self.low,
|
|
"high": self.high,
|
|
"center": self.center,
|
|
"score": self.score,
|
|
"members": [member.summary() for member in self.members],
|
|
"distance": self.distance,
|
|
}
|
|
|
|
|
|
def cluster_levels(
|
|
levels: list[Level], current_t: int, current_price: float, atr15: float
|
|
) -> list[Cluster]:
|
|
tolerance = 0.4 * atr15
|
|
if tolerance <= 0:
|
|
return []
|
|
groups: list[list[tuple[float, Level]]] = []
|
|
positioned = [
|
|
(level.price_at(current_t), level)
|
|
for level in levels
|
|
if not level.hidden and (level.cutoff_t is None or current_t <= level.cutoff_t)
|
|
]
|
|
for positional_side in (Side.SUPPORT, Side.RESISTANCE):
|
|
side_levels = sorted(
|
|
(
|
|
item
|
|
for item in positioned
|
|
if (Side.RESISTANCE if item[0] >= current_price else Side.SUPPORT)
|
|
is positional_side
|
|
),
|
|
key=lambda item: item[0],
|
|
)
|
|
side_groups: list[list[tuple[float, Level]]] = []
|
|
for item in side_levels:
|
|
if not side_groups or item[0] - side_groups[-1][-1][0] > tolerance:
|
|
side_groups.append([item])
|
|
else:
|
|
side_groups[-1].append(item)
|
|
groups.extend(side_groups)
|
|
|
|
clusters: list[Cluster] = []
|
|
for group in groups:
|
|
score = sum(level.weight for _, level in group)
|
|
if len(group) < 2 and score < 8:
|
|
continue
|
|
low, high = group[0][0], group[-1][0]
|
|
center = (low + high) / 2
|
|
side = Side.RESISTANCE if center >= current_price else Side.SUPPORT
|
|
# Identity is the set of levels converging, not a price bucket. The
|
|
# bucket was sized by tolerance, which is derived from ATR and so moves
|
|
# every bar — the same zone kept being issued a new id, the alert
|
|
# engine never recognised it as already fired, and the cooldown was
|
|
# silently defeated.
|
|
members = ",".join(sorted(level.id for _, level in group))
|
|
identity = sha1(f"{side.value}:{members}".encode()).hexdigest()[:12]
|
|
clusters.append(
|
|
Cluster(
|
|
id=f"cl_{identity}",
|
|
side=side,
|
|
low=low,
|
|
high=high,
|
|
center=center,
|
|
score=score,
|
|
members=[level for _, level in group],
|
|
distance=center - current_price,
|
|
)
|
|
)
|
|
return sorted(clusters, key=lambda cluster: abs(cluster.distance))
|