ZeroLAG_MA
Download scriptThis tool is designed to provide traders with a more responsive way to track market trends by minimizing the "lag" typically associated with standard moving averages. In many cases, traditional indicators can feel like they are looking at the past because the current price action takes time to influence the average; this script uses a specific mathematical approach to bring that information closer to the present moment.
How the Logic Works
The indicator operates using a two-step calculation process to smooth out market "noise" while maintaining high sensitivity to recent price changes:
1. The Primary Baseline First, the script calculates a standard moving average based on your preferred settings (such as the time period and price type). This establishes the foundational trend of the asset.
2. The Secondary Smoothing Layer Instead of just showing you that first baseline, the script performs a second calculation on top of it. It treats the results of the first moving average as a new data set and calculates another moving average from that data.
3. The "Zero-Lag" Adjustment This is where the unique logic occurs. To eliminate the delay caused by the second smoothing step, the script performs a specific subtraction and addition formula. It takes the primary baseline, subtracts the secondary smoothed value, and adds the difference back in.
The Final Result
By combining these steps, the indicator produces a line that reacts much faster to sudden price shifts than a standard moving average would. It effectively "pulls" the trend line closer to the actual current price, allowing you to see changes in market direction more quickly while still benefiting from the smoothness of a calculated average.
//+------------------------------------------------------------------+
//| 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