Loading…

Lag Compensated Leading MA

Every simple moving average delays the trend it is tracking by a predictable, computable number of bars - its group delay. For an SMA or an EMA of period N, that delay works out to exactly (N-1)/2 bars. This is not a rough estimate; it follows directly from each filter's own weighting scheme, and it is the same number whether the market is trending or ranging. Knowing the exact delay makes it possible to compensate for it directly rather than just living with it. This indicator computes the base moving average as usual, then produces a second, projected line: Projected[i] = 2 * MA[i] - MA[i - L], L = round((N-1)/2) which is the same construction behind the well-known "zero-lag" moving average trick - take the average's current reading and add back the amount it has moved over its own known delay window, effectively extrapolating the trend forward by the filter's own lag. It is a same-bar calculation built entirely from already-closed prices; nothing about it looks ahead in time or shifts the plot forward, it just uses the shape of the recent average to estimate where the underlying trend has actually reached. Because it is an extrapolation rather than a genuine measurement, the projected line will overshoot at the moment a trend actually reverses - it assumes the recent slope continues, and a reversal is precisely the case where it does not. That overshoot is the honest cost of compensating for lag rather than a bug: the alternative, an uncompensated MA, is simply always late instead. Notes and limitations - The (N-1)/2 group delay formula is derived for SMA and EMA specifically. MODE_SMMA (Wilder smoothing) and MODE_LWMA (linear weighted) have different, less clean-form delay characteristics; the same (N-1)/2 figure is applied to them as a practical approximation, not an exact calculation - expect the projection to be somewhat less precisely lag-compensated on those two methods. - Needs InpPeriod plus the computed group delay worth of bars before it starts plotting. - Not a predictive or forecasting tool - it estimates where the underlying average's own trend has reached given its recent slope, and will overshoot at a genuine reversal rather than anticipate one. - Does not repaint - it only ever reads already-closed bars.

Indicator · MT4 · v1.0 · Free

About Lag Compensated Leading MA

Lag Compensated Leading MA is an indicator for MetaTrader 4 based on Moving Average. It has 3 settings, such as Period, Ma method and Applied price. MQL source code included.

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

Settings

SettingDefaultWhat it does
Period14Base MA period
Ma methodMODE_SMABase MA method
Applied pricePRICE_CLOSEApplied price

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 → Indicators.
  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 indicator from the Navigator onto a chart and adjust the settings.

Download the PDF guide

Tags: Moving Average · Trend · MT4 Indicator · MQL4 Source Code

MetaTrader 4 & 5 indicators · PolyMaria