Join our fan page
- Views:
- 77
- Published:
-
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
The idea behind it
Most VWAP implementations reset at the start of each trading session, which makes them awkward to use on markets that trade around the clock, like forex pairs. This indicator instead computes a rolling VWAP over a fixed number of bars, continuously sliding the window forward bar by bar rather than resetting at midnight or at the open. The rolling VWAP gives a volume-weighted "fair value" anchor that adapts to the most recent trading activity rather than an arbitrary calendar boundary.
From there, the indicator takes the raw deviation between the current close and the rolling VWAP and normalizes it into a Z-Score using the rolling standard deviation of that same deviation series. A Z-Score lets the same threshold mean the same thing across instruments and volatility regimes — a 1.5 reading is "1.5 standard deviations away from fair value" whether you're on EURUSD or a thinly traded index CFD. The indicator then watches for the Z-Score to stay beyond a configurable threshold for several consecutive bars before declaring a trend regime; isolated spikes that snap back immediately are treated as noise and left in the ranging regime bucket. This two-stage filter (threshold + persistence) is what separates it from a plain Bollinger-style mean-reversion band.
How to interpret it
- Z-Score line (blue) — the raw, continuously updating measure of how many standard deviations the close sits above or below the rolling VWAP. Values oscillate around zero; the further from zero, the more stretched price is relative to recent volume-weighted fair value.
- Regime histogram — mirrors the Z-Score value but is colored by the confirmed market state: green for a confirmed bull trend regime, silver/gray for a ranging/mean-reverting regime, and crimson for a confirmed bear trend regime. The color only flips once the consecutive-bar confirmation condition is met, which keeps it from flickering on every tick.
- Threshold confirmation logic — a regime is only confirmed once the Z-Score has closed beyond InpZThreshold for InpConsecutiveBars bars in a row. This dual condition (magnitude + persistence) is what filters out single-bar spikes that would otherwise generate false trend signals on a simple threshold-cross indicator.
- Practical usage — in trend-following setups, treat a fresh flip to the green or crimson histogram as a signal that the market has broken out of its recent fair-value band with enough conviction to follow; in mean-reversion setups, the gray/ranging state combined with a Z-Score approaching ±1.5 without confirming a trend can flag a stretched-but-not-trending condition worth fading back toward the VWAP.
External variables (inputs)
| Input | Default | Purpose |
|---|---|---|
| InpVWAPPeriod | 50 | Number of bars in the rolling, volume-weighted VWAP window. |
| InpStdDevPeriod | 50 | Lookback used to compute the rolling standard deviation that normalizes the deviation into a Z-Score. |
| InpZThreshold | 1.5 | Z-Score magnitude a bar must close beyond to count as a "breach" toward confirming a trend regime. |
| InpConsecutiveBars | 3 | Number of consecutive breaching bars (same direction) required before the regime flips to trending. |
| InpAppliedPrice | Typical (H+L+C)/3 | Price source used inside the VWAP calculation — Typical, Close, Median, or Weighted. |
| InpUseRealVolume | false | When true, uses real exchange volume in the VWAP weighting instead of tick volume (useful on instruments where real volume is reliably reported, such as futures). |
Recommended use
Attach the indicator to its own subwindow below the price chart and keep the rolling VWAP visible as context (the figure below shows a suggested layout, with the VWAP drawn as a dashed overlay on the price panel for reference). On lower timeframes such as M15, the defaults of a 50-bar VWAP and 50-bar standard-deviation window roughly track an intraday fair-value band; widen both periods on higher timeframes or less liquid instruments to avoid over-triggering the consecutive-bar confirmation. Combine a fresh trend-regime flip with simple price-action confirmation (a break of the prior swing high/low, or a close beyond a key level) before treating it as an entry trigger, and use the ranging/gray state as a filter to sit out or switch to a mean-reversion approach rather than trend-following.

Fig. 1. Top panel: price candles with the rolling VWAP plotted as a dashed line. Bottom panel: the Z-Score oscillator (solid line) with the ±1.5 confirmation thresholds (dashed gray), and the regime histogram shaded white for a confirmed bull trend, gray for ranging, and black for a confirmed bear trend.
AlertKit, one alert per event and bar, no alert spam
A small include class that stops indicator alerts from firing on every tick. One alert per event and bar, silent about history on start, popup, push, email or sound, and an optional log of every decision. Comes with a working range break indicator as an example
SessionEdgeTester - does a time-of-day rule beat spread and swap?
Tests "buy at hour X, exit Y hours later" on your broker's own history after spread, commission and swap, with an out-of-sample split, weekday/year breakdown and an all-hours baseline.
TradeGuard - Reliable Order Execution: No Duplicates, Safe Retries, Restart Recovery
A one-file library that handles how an EA trades: pre-trade checks, normalisation to broker limits, safe retries, one order per signal, isolation by magic number and state recovery after a restart. Lot by risk, daily loss limit and equity floor included. Verified in the Strategy Tester.
Risk Lot Sizing Library - four risk bases, tick-value scaling, margin cap, daily limit
A stop distance in points is not money until the tick size and tick value make it one. This library turns four risk bases into a lot size, floors it to the volume step, caps it against free margin and tracks a daily loss limit. Two MQL5 headers, no DLL.