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>
47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
from app.analysis.levels import Level, LevelKind, Side
|
|
from app.bars.models import Bar, Timeframe
|
|
from app.config import TIMEFRAME_WEIGHT
|
|
|
|
|
|
PRIOR_DAY_SPECS = (("high", "PDH", "Prior day high"), ("low", "PDL", "Prior day low"), ("close", "PDC", "Prior day close"))
|
|
|
|
|
|
def build_prior_day_levels(daily_bars: list[Bar], current_price: float | None) -> list[Level]:
|
|
"""Prior session high, low and close.
|
|
|
|
The newest daily bar is normally still forming, so "prior day" means the
|
|
last *closed* session. Taking the last bar outright would silently switch
|
|
the levels to today's own developing range partway through the session,
|
|
which is not what anyone means by PDH.
|
|
|
|
These carry the full daily weight rather than the moving-average discount:
|
|
an actual prior high is traded structure, not a derived average.
|
|
"""
|
|
closed = [bar for bar in daily_bars if bar.closed]
|
|
if not closed:
|
|
return []
|
|
prior = closed[-1]
|
|
reference = current_price if current_price is not None else prior.c
|
|
prices = {"high": prior.h, "low": prior.l, "close": prior.c}
|
|
|
|
return [
|
|
Level(
|
|
id=f"pd:{key}",
|
|
kind=LevelKind.HORIZONTAL,
|
|
tf=Timeframe.D1,
|
|
side=Side.SUPPORT if prices[key] <= reference else Side.RESISTANCE,
|
|
weight=TIMEFRAME_WEIGHT[Timeframe.D1],
|
|
score=1.0,
|
|
label=short,
|
|
anchor_t=prior.t,
|
|
anchor_p=prices[key],
|
|
slope=0.0,
|
|
points=None,
|
|
touches=0,
|
|
first_t=prior.t,
|
|
last_t=prior.t,
|
|
provisional=False,
|
|
hidden=False,
|
|
)
|
|
for key, short, _description in PRIOR_DAY_SPECS
|
|
]
|