ZeroLAG MA
Download scriptThis tool is designed to provide a more responsive visual guide for tracking market trends on your trading charts. In standard technical analysis, most moving averages suffer from "lag"—a delay where the line reacts to price changes only after they have already occurred. The logic behind this specific script aims to minimize that delay, offering a smoother but faster-reacting indicator.
How the Logic Works
The script processes market data through a two-stage calculation to achieve its results:
1. Primary Data Collection First, the system calculates a standard moving average based on your chosen timeframe and price type. Instead of just looking at the raw price, it creates a foundational set of values (a "work buffer") that represents the general direction of the market over a specific period.
2. The "Zero-Lag" Adjustment This is where the unique logic occurs. Rather than simply displaying that first moving average, the script performs a second calculation on the data it just created. It calculates a "secondary" moving average of the first one and then uses the difference between these two values to adjust the final line.
Essentially, by subtracting this secondary movement from the primary movement, the script cancels out much of the mathematical "drag" usually found in standard indicators.
The Final Result
The result is a red line on your chart that follows the price action more closely than a traditional moving average would. Because it uses this self-correcting calculation, it attempts to stay as close to the current price action as possible while still filtering out minor market noise. It provides traders with a visual cue that highlights trend shifts with greater immediacy, helping you see where the market is heading in real-time rather than where it has already been.
//+------------------------------------------------------------------+
//| ZeroLAG MA.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int nZLMAPeriod = 21;
extern int nZLMAShift = 0;
extern int nZLMAMethod = 1;
extern int nZLMAApplPrice = 0;
extern int nZLMASmPeriod = 21;
extern int nZLMASmShift = 0;
extern int nZLMASmMethod = 1;
//---- indicator buffers
double dZLMABuffer[];
double dWorkBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(2);
SetIndexBuffer(0, dZLMABuffer);
SetIndexBuffer(1, dWorkBuffer);
SetIndexStyle(0, DRAW_LINE);
IndicatorShortName("ZeroLAG MA(" + nZLMAPeriod + ", " + nZLMAShift + ", " +
nZLMAMethod + ", " + nZLMAApplPrice + ")");
SetIndexDrawBegin(0, nZLMAPeriod + nZLMASmPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
double dMA;
int counted_bars = IndicatorCounted();
if(counted_bars < 0)
return(-1);
if(counted_bars > 0)
counted_bars--;
limit = Bars - counted_bars;
for(int i = 0; i < limit; i++)
dWorkBuffer[i] = iMA(NULL, 0, nZLMAPeriod, nZLMAShift, nZLMAMethod, nZLMAApplPrice, i);
for(i = 0; i < limit; i++)
{
dMA = iMAOnArray(dWorkBuffer, 0, nZLMASmPeriod, nZLMASmShift, nZLMASmMethod, i);
dZLMABuffer[i] = dWorkBuffer[i] + dWorkBuffer[i] - dMA;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments