ZeroLagMA

This tool is a specialized technical analysis indicator designed to provide smoother and more responsive market trend visualizations than standard moving averages. Its primary purpose is to reduce "lag"—the delay that often occurs between a price movement and the visual signal on a chart—while maintaining a clean, readable line for traders.

How the Logic Works

The script operates by calculating two distinct types of data simultaneously to create a highly optimized "Zero Lag" effect:

1. The Foundation (Standard Smoothing) First, the tool establishes a baseline using a standard Exponential Moving Average (EMA). This provides a general sense of the market's direction by weighing recent price data more heavily than older data. However, because standard averages always trail behind current prices, the script treats this as only the first step in its calculation.

2. The Error Correction Mechanism This is where the unique logic resides. Instead of just accepting the lagged average, the script performs a high-speed "search" for the best way to correct the discrepancy between the actual market price and the calculated average.

It looks at a specific range of potential adjustments (defined by the user settings) and calculates how much "gain" is needed to bridge the gap between where the price is currently sitting and where the moving average thinks it should be. It essentially asks: "What is the most accurate adjustment I can make right now to ensure this line stays as close to the actual price as possible without overshooting it?"

3. The Final Output By selecting the "best" adjustment from that search, the script produces a second, refined line. This final line combines the stability of a moving average with a dynamic correction factor. The result is a visual trend indicator that reacts much faster to sudden price shifts while remaining smooth enough to filter out minor market noise.

Key User Controls

  • Maperiod: Determines the "memory" of the tool. A higher number looks at a longer history for smoother lines, while a lower number makes the tool more sensitive to immediate changes.
  • MAGainLimit: Defines the range of the correction logic. It dictates how aggressively the tool is allowed to "correct" itself to match the current price.
Miscellaneous
Implements a curve of type %1
4 Views
0 Downloads
0 Favorites
ZeroLagMA
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Red
#property indicator_width2 2

extern int MAPeriod    = 20;
extern int MAGainLimit = 50;

double EMABuffer[];
double ECBuffer[];

double gdAlpha = 0;

int init()
{
    SetIndexStyle(0, DRAW_LINE);
    SetIndexBuffer(0, EMABuffer);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexBuffer(1, ECBuffer);
    IndicatorShortName("ZeroLagMA("+MAPeriod+")");
    SetIndexLabel(0, "EMA");
    SetIndexLabel(1, "EC");

    gdAlpha = 2 / (MAPeriod + 1);
    return(0);
}

int start()
{
    int    i, j, iCounted, iLimit;
    double dGain, dBestGain, dError, dLeastError, dEC;
    
    iCounted = IndicatorCounted();
    if(iCounted > 0) 
      iCounted--;
    iLimit = Bars - iCounted;
    for(i = iLimit-1; i >= 0; i--)
    {
        EMABuffer[i] = gdAlpha * Close[i] + (1 - gdAlpha) * EMABuffer[i+1];
        dLeastError = 1000000;
        for(j = -MAGainLimit; j <= MAGainLimit; j++)
        {
            dGain = j / 10;
            dEC = gdAlpha * (EMABuffer[i] + dGain*(Close[i] - ECBuffer[i+1])) + (1 - gdAlpha) * ECBuffer[i+1];
            dError = Close[i] - dEC;
            if(MathAbs(dError) < dLeastError)
            {
                dLeastError = MathAbs(dError);
                dBestGain = dGain;
            }
        }
        ECBuffer[i] = gdAlpha * (EMABuffer[i] + dBestGain * (Close[i] - ECBuffer[i+1])) + (1 - gdAlpha) * ECBuffer[i+1];
    }
    return(0);
}

Comments