ZeroLag MACD_v1
Download scriptThis tool is designed to enhance market analysis by providing a more responsive version of the Moving Average Convergence Divergence (MACD) indicator. While standard technical indicators often suffer from "lag"—meaning they react to price changes only after the move has already occurred—this script utilizes a specific mathematical logic to minimize that delay, helping traders identify momentum shifts more quickly.
How the Logic Works
The tool operates through a multi-layered calculation process to refine the relationship between fast and slow price trends:
1. Establishing the Foundation The script first identifies two primary trends by calculating Exponential Moving Averages (EMAs) based on user-defined periods (the "Fast" and "Slow" settings). These averages smooth out the daily price noise to show the overall direction of the market.
2. The "ZeroLag" Transformation This is the core innovation of the script. Instead of simply plotting the standard EMAs, the tool applies a compensatory formula to both the fast and slow averages. It calculates a secondary moving average of the existing data and uses it to adjust the primary values. By subtracting this secondary average from twice the current value, the script effectively "pulls" the indicator forward in time. This mathematical adjustment helps neutralize the typical delay found in traditional moving averages.
3. Calculating the MACD Line Once the fast and slow trends have been "ZeroLag" adjusted, the script subtracts the adjusted slow trend from the adjusted fast trend. The resulting difference creates the main MACD histogram. Because both components of this subtraction have been adjusted for lag, the resulting histogram provides a much more immediate reflection of price momentum.
4. The Signal Line Smoothing To make the data even easier to read, the script performs a final "ZeroLag" calculation on the resulting MACD line itself. This creates a "Signal Line." By applying the same lag-reduction logic to the signal, it ensures that the relationship between the main histogram and its signal remains synchronized and highly responsive to current market conditions.
Summary for the User
In plain terms, this script takes standard trend-following data and mathematically "speeds up" its reaction time. It aims to provide a visual representation of market momentum that stays as close to real-time price action as possible, allowing for potentially faster identification of trend reversals and strength compared to standard MACD tools.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| ZeroLag MACD.mq4 |
//| RD |
//| marynarz15@wp.pl |
//+------------------------------------------------------------------+
#property copyright "RD"
#property link "marynarz15@wp.pl"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Magenta
#property indicator_color2 Orange
//---- input parameters
extern int FastEMA=12;
extern int SlowEMA=24;
extern int SignalEMA=9;
//---- buffers
double MACDBuffer[];
double SignalBuffer[];
double FastEMABuffer[];
double SlowEMABuffer[];
double SignalEMABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(5);
SetIndexBuffer(0,MACDBuffer);
SetIndexBuffer(1,SignalBuffer);
SetIndexBuffer(2,FastEMABuffer);
SetIndexBuffer(3,SlowEMABuffer);
SetIndexBuffer(4,SignalEMABuffer);
SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,2);
SetIndexStyle(1,DRAW_LINE,EMPTY,2);
SetIndexDrawBegin(0,SlowEMA);
SetIndexDrawBegin(1,SlowEMA);
IndicatorShortName("ZeroLag MACD("+FastEMA+","+SlowEMA+","+SignalEMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
double EMA,ZeroLagEMAp,ZeroLagEMAq;
for(int i=0; i<limit; i++)
{
FastEMABuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i);
SlowEMABuffer[i]=iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
}
for(i=0; i<limit; i++)
{
EMA=iMAOnArray(FastEMABuffer,Bars,FastEMA,0,MODE_EMA,i);
ZeroLagEMAp=FastEMABuffer[i]+FastEMABuffer[i]-EMA;
EMA=iMAOnArray(SlowEMABuffer,Bars,SlowEMA,0,MODE_EMA,i);
ZeroLagEMAq=SlowEMABuffer[i]+SlowEMABuffer[i]-EMA;
MACDBuffer[i]=ZeroLagEMAp - ZeroLagEMAq;
}
for(i=0; i<limit; i++)
SignalEMABuffer[i]=iMAOnArray(MACDBuffer,Bars,SignalEMA,0,MODE_EMA,i);
for(i=0; i<limit; i++)
{
EMA=iMAOnArray(SignalEMABuffer,Bars,SignalEMA,0,MODE_EMA,i);
SignalBuffer[i]=SignalEMABuffer[i]+SignalEMABuffer[i]-EMA;
}
return(0);
}
//+------------------------------------------------------------------+
Comments