Loading…

Anchored VWAP Pullback System

A rolling or session-anchored VWAP resets on a fixed clock (a new session, a new day) with no relationship to where the current move actually began. An anchored VWAP fixes that by starting the volume-weighted average at a specific bar - here, the most recently confirmed swing low while the trend reads up, or swing high while it reads down - so the average genuinely represents "the volume-weighted price since this move started," and a pullback to it means something structurally, not just arithmetically. Anchor rotation is the part every other implementation glosses over, so this one states its four rules explicitly, both in the source header and here: 1. A swing pivot confirms InpSwingLookback bars after it forms, once that many bars on both sides are lower (a swing high) or higher (a swing low) than the pivot bar itself - never based on a still-forming bar. 2. A trend-direction flip clears the anchor immediately. A swing origin from the previous trend has no claim on the new one; VWAP restarts once a fresh pivot in the new direction confirms. 3. With an anchor already set, a newly confirmed pivot of the matching type only replaces it if it is a genuine structural step - a higher low in an uptrend, a lower high in a downtrend. A pivot that fails that test is rejected as noise and the existing anchor is kept. 4. An anchor older than InpMaxAnchorAgeBars is cleared outright - VWAP over an excessively long window drifts toward a plain average and stops representing "since this move began." An entry triggers when price touches or crosses the anchored VWAP intrabar and then closes back on the trend side of it (a rejection, not just a touch), with the stop placed beyond the anchor swing's own extreme plus a buffer, and the target a configurable multiple of that risk distance. Position sizing is risk-based off the symbol's own tick value and tick size, never a hardcoded contract size. Both versions are native ports of the same logic - same rotation rules, same entry test, same risk sizing - built on each platform's own trading and series-access model. Notes and limitations VWAP here uses tick volume (the only volume series available on most forex feeds on either platform - CopyTickVolume() on MQL5, Volume[] on MQL4), not traded contract volume - a reasonable proxy for relative activity level, not a literal traded-volume VWAP the way it would be on an exchange-traded instrument with real volume reporting. Recomputing VWAP from the anchor bar forward every new bar (rather than tracking a running sum) is deliberate: the anchor itself can rotate, and an incremental running total would carry stale contributions from before a rotation. This bounds the cost by InpMaxAnchorAgeBars rather than letting an old anchor's VWAP window grow without limit. Only one position per magic/symbol is held at a time. Grid, martingale and averaging are NOT used; every position is single, risk-sized and independently stopped. As noted above, the MQL5 version does not defend against a foreign position occupying the same symbol on a netting account - treat it as a hedging-account tool.

Expert advisor · MT4 · v1.0 · Free

About Anchored VWAP Pullback System

Anchored VWAP Pullback System is an expert advisor (trading robot) for MetaTrader 4 based on Moving Average. It can size trades from your balance and risk percent. It has 10 settings, such as Magic, Trend ema period and Swing lookback. MQL source code included.

Files in the download: AnchoredVwapPullbackSystem.ex4, AnchoredVwapPullbackSystem.mq4, AnchoredVwapPullbackSystem.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Magic20261302Magic number for this system's own orders
Trend ema period50EMA period used to read trend direction
Swing lookback5Bars on each side required to confirm a swing pivot
Max anchor age bars300Anchor cleared if older than this many bars (see rule 4 above)
Stop buffer pips3.0Extra pips beyond the anchor swing extreme for the stop
Reward multiple2.0Take-profit distance as a multiple of the stop distance
Risk percent1.0Risk % of balance per trade
Max slippage points5Slippage tolerance passed to OrderSend
Max retries5Attempts per order action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: VWAP · Volume · Trend · Trading Sessions · Risk & Lot Size · Moving Average · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria