ZeroLag MACD_002
Download scriptThe ZeroLag MACD is a specialized technical analysis tool designed for the MetaTrader 4 platform. Its primary purpose is to track market momentum by calculating the difference between two moving averages, while applying a specific mathematical "ZeroLag" technique to minimize the inherent delays found in standard trend-following indicators.
Core Mechanics
The script operates through a multi-layered calculation process to ensure that the data displayed on your chart reflects current price action as accurately as possible:
- Trend Identification: The indicator starts by identifying two distinct price trends using Exponential Moving Averages (EMAs). One is designed to be "Fast" (reacting quickly to recent price changes) and the other "Slow" (representing the broader trend).
- The ZeroLag Modification: Standard moving averages often suffer from "lag," where the indicator reflects what happened in the past rather than what is happening now. This script solves this by calculating a specialized average for both the Fast and Slow components. It achieves this by taking the current value of the moving average, doubling it, and subtracting the average of the previous data points. This mathematical adjustment aims to "pull" the indicator forward in time, making it more responsive to sudden price shifts.
- MACD Calculation: The tool then subtracts the modified Slow trend from the modified Fast trend. The resulting value represents the momentum of the market. If the Fast trend is significantly higher than the Slow trend, it indicates bullish strength; if lower, it suggests bearish pressure.
- Signal Line Smoothing: Finally, the script generates a "Signal" line. This is a secondary moving average applied to the MACD result itself. By smoothing out the MACD data, it provides a clearer visual cue for traders to identify potential turning points or trend reversals.
Visual Output
The indicator populates a separate window below your main price chart:
- The Histogram: This represents the raw ZeroLag MACD value. It allows you to see the immediate strength and direction of the price movement.
- The Signal Line: A smooth line that follows the histogram. Traders typically look for "crosses" between this line and the histogram to identify potential entry or exit signals.
By combining these elements, the script provides a more reactive version of the traditional MACD, intended to give traders a clearer picture of market momentum with reduced time delay.
//+------------------------------------------------------------------+
//| 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 Silver
#property indicator_color2 Black
//---- input parameters
extern int FastEMA=5;
extern int SlowEMA=13;
extern int SignalEMA=1;
//---- 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,1);
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