chart/tests/test_vwap.py
Chris Amow 8ca624d435 Add prior-day levels and session VWAP; fix alert repetition they exposed
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>
2026-08-10 00:36:21 -05:00

57 lines
1.8 KiB
Python

from datetime import datetime
from zoneinfo import ZoneInfo
from app.analysis.vwap import build_vwap_level
from app.bars.models import Bar, Timeframe
EASTERN = ZoneInfo("America/New_York")
def at(year: int, month: int, day: int, hour: int, minute: int = 0) -> int:
return int(datetime(year, month, day, hour, minute, tzinfo=EASTERN).timestamp())
def minute(t: int, price: float, volume: int) -> Bar:
return Bar(Timeframe.M1, t, price, price, price, price, volume, True, "ES=F", "test")
def test_vwap_is_volume_weighted_not_a_simple_mean():
bars = [minute(at(2026, 8, 10, 19), 100, 1), minute(at(2026, 8, 10, 20), 200, 3)]
level = build_vwap_level(bars)[0]
assert level.anchor_p == (100 * 1 + 200 * 3) / 4 # 175, not 150
def test_prior_session_bars_are_excluded():
bars = [
# Before Monday's 18:00 open, so part of the previous session.
minute(at(2026, 8, 10, 17), 500, 10),
minute(at(2026, 8, 10, 19), 100, 1),
minute(at(2026, 8, 10, 20), 200, 1),
]
level = build_vwap_level(bars)[0]
assert level.anchor_p == 150
assert level.first_t == at(2026, 8, 10, 19)
def test_zero_volume_minutes_do_not_divide_by_zero():
bars = [minute(at(2026, 8, 10, 19), 100, 0), minute(at(2026, 8, 10, 20), 200, 2)]
level = build_vwap_level(bars)[0]
assert level.anchor_p == 200
# The zero-volume minute contributes no point rather than a NaN.
assert len(level.points) == 1
def test_no_volume_at_all_yields_no_level():
assert build_vwap_level([minute(at(2026, 8, 10, 19), 100, 0)]) == []
def test_side_tracks_price_relative_to_vwap():
bars = [minute(at(2026, 8, 10, 19), 100, 1), minute(at(2026, 8, 10, 20), 200, 1)]
# Last close 200 sits above VWAP 150, so VWAP is support beneath price.
assert build_vwap_level(bars)[0].side.value == "support"