Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Indicator | Rolling VWAP Deviation Regime Filter | MQL5 Code

New free code from MQL5: indicators, EAs, and scripts for traders.

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.


Protect profits effectively with the smart Trailing Stop Expert Advisor. Advanced trailing options for MT4/MT5. See it in action.

Image for Rolling VWAP Deviation Regime Filter

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.

Build better strategies with RobotFX professional tools – check them out.

78989

Best MetaTrader Indicators + Profitable Expert Advisors