Session VWAP with one- and two-sigma bands
The volume-weighted average price since the New York open, restarted every session, with bands one and two volume-weighted standard deviations away. It works on any intraday chart and draws nothing on a daily one, where a session VWAP means nothing.
The volume-weighted average price since the New York open, restarted every session, with bands one and two volume-weighted standard deviations away. It works on any intraday chart and draws nothing on a daily one, where a session VWAP means nothing.
Shows: Indicator, overlay=True, while25.interval and while25.instrument, Plot(style=, opacity=), two Fills, a fixed int warmup, None gaps in a series.
"""Session VWAP with one- and two-sigma bands
The volume-weighted average price since the New York open, restarted every
session, with bands one and two volume-weighted standard deviations away. It
works on any intraday chart and draws nothing on a daily one, where a session
VWAP means nothing.
Shows: Indicator, overlay=True, while25.interval and while25.instrument, Plot(style=, opacity=), two Fills, a fixed int warmup, None gaps in a series.
"""
from datetime import datetime
from zoneinfo import ZoneInfo
import while25
from while25 import Fill, Indicator, Plot
NY = ZoneInfo("America/New_York")
INTRADAY = ("1m", "2m", "3m", "5m", "10m", "15m", "30m", "1h", "2h", "4h")
def session_vwap(rows, tz=NY):
"""(vwap, sd) lists, restarting at each new session date in `tz`. A bar
with no volume weighs nothing; a session with no volume yet is None."""
vwap, sd = [], []
day = None
pv = pv2 = vol = 0.0
for r in rows:
d = datetime.fromtimestamp(r["time"] / 1000, tz).date()
if d != day:
day, pv, pv2, vol = d, 0.0, 0.0, 0.0
typical = (r["high"] + r["low"] + r["close"]) / 3
v = r.get("volume") or 0.0
pv += typical * v
pv2 += typical * typical * v
vol += v
if vol <= 0:
vwap.append(None)
sd.append(None)
continue
mean = pv / vol
vwap.append(mean)
sd.append(max(pv2 / vol - mean * mean, 0.0) ** 0.5)
return vwap, sd
def band(vwap, sd, k):
return [None if m is None else m + k * s for m, s in zip(vwap, sd)]
class SessionVwap(Indicator):
id = "session_vwap"
label = "Session VWAP"
overlay = True
plots = [Plot("vwap", color="#E8A817", width=2),
Plot("up1", color="#8A8279", style="dotted"),
Plot("dn1", color="#8A8279", style="dotted"),
Plot("up2", color="#8A8279", style="dashed", opacity=0.6),
Plot("dn2", color="#8A8279", style="dashed", opacity=0.6)]
fills = [Fill("up1", "dn1", color="#E8A817", opacity=0.05),
Fill("up2", "dn2", color="#8A8279", opacity=0.03)]
warmup = 1
def compute(self, ctx):
rows = while25.bars
self.times = [int(r["time"]) for r in rows]
if while25.interval not in INTRADAY:
empty = [None] * len(rows)
while25.log(f"session VWAP needs an intraday chart; {while25.instrument} "
f"is on {while25.interval}", "warn")
return {name: empty for name in ("vwap", "up1", "dn1", "up2", "dn2")}
vwap, sd = session_vwap(rows)
return {"vwap": vwap, "up1": band(vwap, sd, 1), "dn1": band(vwap, sd, -1),
"up2": band(vwap, sd, 2), "dn2": band(vwap, sd, -2)}Return z-score in its own pane
How unusual the latest N-bar return is against the returns of the recent past, in standard deviations, drawn as a histogram coloured by sign, with lines at +/-2 and the zone between +/-1 shaded. The scale is fixed so two charts read alike, and it recomputes as the bar forms.
Trend significance: is the drift in the last N closes real, or noise?
Fits a least-squares line through the last `length` closes and draws the slope's t-statistic as a histogram in its own pane, with the +/-2 line shaded. Use it to compare trend strength across symbols: a t-statistic has no units.