Attachments forums

Re: MT5 Moving Averages (MAs)

AlgoAlex811, Mon Sep 28, 2026 6:54 pm

AVWAP with ATR pivot anchoring

I started using and studying the XU Sema3 VWAP but I was not happy with the explicit repaint issue. I know a lot of Xard indis are made this way, but for my automated trading this is not acceptable. So I tried to take a different path.
The original anchors its VWAP segments to ZigZag pivots, and that ZigZag is a classic repainter:
The Backstep loop zeroes pivots that were already written (Low_Buffer[i+j] = 0).
A second pass, driven by static variables (last_highpos, last_lowtwo…), deletes older pivots whenever a more extreme one appears, at any distance in the past. Repaint_Guard_Bars only skips the newest bars. It does not protect pivots already drawn.
Each VWAP segment is anchored to a pivot, so every deleted or moved pivot rewrites the VWAP behind it.
Using static state also made history, visual tester and live results diverge.

A ZigZag can't be made non-repainting by patching it, because its definition of "the swing" depends on future bars. Instead, my engine is a small state machine where each bar looks only at the previous bar's state:

Code: Select all

tracking a LOW:
   if low < extreme          -> extreme = low            (trend continues)
   elif high > extreme + ATR(14) * K
                             -> reversal confirmed:
                                anchor = bar before the confirming bar
                                new AVWAP segment starts, zone flips to BUY
                                now tracking a HIGH (mirror logic)
  • The reversal threshold is adaptive (ATR × multiplier, default 5.0), so it works on forex, gold, crypto and indices without retuning points.
  • All state (ATR, direction, extreme, anchor, cumulative sums) lives in hidden INDICATOR_CALCULATIONS buffers. No static, no globals, forward loop only.
  • The AVWAP is anchored where the reversal is confirmed, not at the exact extreme. That is the price of stability: the flip lags, and higher multipliers lag more.
The recalculation window is the actual anti-repaint rule

On every call only rates_total − 2 onward is recomputed: the last closed bar and the forming bar. Anything older is never touched again.
  • When bar i confirms a reversal, the anchor is written on bar i−1, while i is still forming.
  • Once bar i closes, nothing before it changes.
  • On the live bar the confirmation can appear intrabar and, on a rare outside bar, disappear again before the close. At the close, the live result equals a full recalculation.

I also added volume-weighted SD bands at ±1 and ±2 SD: they use Σ(V·P²)/ΣV − VWAP². This needs one extra running sum, so they are computed incrementally and follow the same write-once rule. Only the bands of the active zone are drawn.

I believe this is the best compromise between anchoring and non-repainting.
All files in topic