OPEN-SOURCE SCRIPT

VWAP Z-Score Oscillator

1 834
**VWAP Z-Score Oscillator — Complete Description**

**Overview**

The indicator is a single-pane oscillator that measures how far price has moved from a rolling, volume-weighted average price. It answers one question on every bar: how far is price from its recent volume-weighted fair value, and how significant is that gap. The output can be shown either as a normalized standard-deviation reading or as the raw price distance, selectable by the user.

**Core Calculation**

- **Moving VWAP.** A rolling volume-weighted mean of price is computed over a user-set lookback. Unlike a session VWAP, it slides forward continuously and never resets at the session boundary.
- **Volume-weighted standard deviation.** The dispersion of price around that Moving VWAP is calculated as a volume-weighted variance, then square-rooted. It expands when volatility rises and contracts when the market is quiet.
- **Oscillator value.** In Z-Score mode the plotted value is the price-to-VWAP gap divided by that standard deviation, so the reading is expressed in standard deviations. In Distance mode the plotted value is the raw gap in the instrument's own price units. Zero means price is resting on the Moving VWAP; positive means above, negative means below.

**Two Display Modes**

- **Z-Score (σ).** Normalized. The band levels are read directly as standard deviations, and reference lines sit at constant levels. Because the scale is unit-free, the same levels carry the same meaning on any symbol or timeframe.
- **Distance.** Raw. The oscillator shows the actual price gap, and each band is drawn at its level multiplied by the standard deviation, so the bands widen and tighten with volatility while the raw distance stays interpretable.

**Multi-Timeframe Calculation**

A calculation-timeframe selector runs the entire computation on a chosen timeframe rather than only the chart's. The default uses the chart timeframe. Choosing a higher timeframe recomputes the Moving VWAP, the standard deviation, and the oscillator on that timeframe's bars and pulls the result back through a single combined data request. The request runs without look-ahead, so no future higher-timeframe data leaks into historical bars; the current higher-timeframe value continues updating until that bar closes.

**Visual Output**

- The oscillator plots as a histogram, colored by which side of the Moving VWAP price is on.
- The histogram fades across a user-defined transparency range: faint near the VWAP, intensifying to full color as price stretches to a set distance, then holding solid beyond it.
- A solid zero line marks the Moving VWAP.
- Three standard-deviation bands are drawn above and below zero, each with its own toggle, level, and color.

**User Inputs**

*Calculation Timeframe*
- **Calculation Timeframe** — the timeframe the oscillator is computed on. Options are labeled in plain language across seconds, minutes, hours, days, weeks, and a month, plus Chart. Chart uses the current chart timeframe. Higher timeframes give a broader context; lower-than-chart selections are not recommended, and seconds-based options only work on charts that support second-level data.

*Settings*
- **Oscillator Mode** — switches between Z-Score (σ), the normalized standard-deviation reading, and Distance, the raw price gap with volatility-scaled bands.
- **Source** — the price series feeding both the Moving VWAP and the distance measurement. Defaults to the average of high, low, and close; a close-only source reacts faster.
- **Moving VWAP Length** — the number of bars in the rolling window for both the VWAP and its standard deviation. Larger values are smoother and slower; smaller values are more reactive.
- **Fallback to MA on no-volume symbols** — when enabled, symbols that report no volume use a simple moving average and plain standard deviation so the oscillator still functions; when disabled, such symbols show nothing.

*Standard Deviation Bands (three identical rows: Band 1, Band 2, Band 3)*
- **Toggle** — shows or hides that band's upper and lower lines independently.
- **Level (Σ)** — that band's distance from zero, in standard deviations, adjustable in fine steps. Defaults are 1, 2, and 3.
- **Color** — the color applied to both the upper and lower line of that band.
- Band 2's level additionally sets the threshold used by the two stretch alerts.

*Colors*
- **Above VWAP** — histogram color when the oscillator is positive.
- **Below VWAP** — histogram color when the oscillator is negative.
- **Transparency at VWAP** — histogram transparency when price sits on the Moving VWAP; the faint end of the color range.
- **Transparency at Full Color** — histogram transparency once price reaches the full-color distance; the solid end of the range.
- **Full-Color Distance (σ)** — the standard-deviation distance over which the histogram fades from faint to solid; beyond it, the color holds solid.

**Data Window Outputs**

Two values are exposed for inspection or use by other scripts: the Moving VWAP and the standard deviation. The oscillator value itself is also readable from the histogram plot. These do not draw on the chart pane.

**Alerts**

Four alert conditions are available: price crossing above the Moving VWAP, crossing below it, stretching beyond Band 2's upper level, and stretching beyond Band 2's lower level. The stretch alerts follow Band 2's configured level in both modes.

**Behavioral Notes**

- In Z-Score mode the oscillator is unbounded. During a violent move it can spike past four or five standard deviations, which reflects genuine dislocation rather than a display error.
- The Style tab is intentionally empty; all appearance and behavior controls live in the Inputs tab, so nothing there can override the code-driven output.
- The indicator's sole basis is price distance from the Moving VWAP; there is no market-structure logic in the calculation.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน