Ehlers ZeroLag EMA
Download scriptThis custom indicator is designed to provide traders with a smoother, more responsive way to track market trends on a price chart. It utilizes two distinct mathematical lines to help identify the current direction of the market while attempting to minimize the "lag" often found in standard moving averages.
How it Works
The script calculates two primary data points simultaneously:
1. The Standard Trend Line (EMA) First, the tool establishes a base Exponential Moving Average. This serves as the foundation, smoothing out the minor fluctuations of price action to show the broader trend. It gives more importance to recent price movements than older data.
2. The Adjusted "Zero-Lag" Line The core logic of this script lies in its ability to self-adjust. Unlike a standard average that simply follows the price with a delay, this indicator performs a series of rapid internal calculations for every new price point:
- Error Testing: The script looks at the difference between the current market price and its own previous value.
- Optimization Loop: It simulates a variety of "gain" adjustments (mathematical offsets) to see which one most accurately aligns the indicator with the current price.
- Selection: Out of all these simulations, it identifies the specific adjustment that results in the smallest possible error.
- Application: It then applies that "best" adjustment to create a secondary line that hugs the price action more closely than a traditional average would.
What this means for your Trading
By using this logic, the indicator aims to provide a visual representation of the trend that reacts faster to sudden changes in market momentum.
- Enhanced Responsiveness: Because the script actively seeks out the smallest error margin between itself and the current price, it reduces the "delay" effect common in technical analysis.
- Visual Clarity: By displaying these two lines together, you can see the relationship between the smoothed base trend and the high-sensitivity adjusted trend, helping you identify shifts in momentum before they become obvious on standard charts.
#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.0 / (MAPeriod + 1.0);
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