ZeroLag Tema 2
Download scriptThis script introduces a sophisticated technical analysis tool designed to track market trends with high precision while minimizing the "lag" typically associated with moving averages. It accomplishes this by combining two advanced mathematical concepts: the Triple Exponential Moving Average (TEMA) and Zero-Lag processing, applied to different types of price data.
Core Concept: Reducing Delay
Standard moving averages often fall behind current market prices because they calculate averages based on past data. This tool uses a "Zero-Lag" logic that essentially corrects the price movement by comparing current trends against their own recent history. It calculates multiple layers of smoothed averages and then subtracts the "lag" component from the final result, allowing the line to stay much closer to the actual price action in real-time.
Dual-Layer Analysis
The script operates on two distinct settings simultaneously, providing a multi-layered view of the market:
- Primary Trend (Longer Term): This uses a larger period to identify the broader market direction. It can be set to look at "Typical Price" (the average of the high, low, and close) to filter out minor noise.
- Secondary Trend (Shorter Term): This uses a smaller period to capture quicker momentum. A unique feature here is the ability to use Heiken-ashi price logic. Heiken-ashi is a specialized charting style that smooths out price fluctuations, and by incorporating it into the calculation, the script can provide a cleaner, more "stable" view of price momentum without the jagged edges of standard candles.
Dynamic Price Processing
The tool is versatile in how it interprets what "price" actually is:
- Standard Mode: It calculates the moving average based on typical prices or specific market data points.
- Heiken-ashi Mode: When activated, the script reconstructs the price candles internally (calculating simulated Opens, Highs, Lows, and Closes) before performing the smoothing math. This helps traders see the "true" trend of the price movement by removing visual distractions.
How it appears on your chart
The script produces two distinct lines on your trading platform. By observing how these two lines interact—such as when they cross each other or change their slope—you can identify potential shifts in market momentum. Because of the "Zero-Lag" math, these lines react more swiftly to sudden price changes than a standard moving average would, while still remaining smooth enough to be readable.
//+------------------------------------------------------------------+
//| ZeroLag Tema.mq4 |
//| mladen |
//| |
//| original ZeroLag Heiken-ashi Tema and |
//| Zero-lag Typical price Tema for metatrader |
//| developed by Kalenzo |
//| |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link "www.forex-tsd.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SteelBlue
#property indicator_color2 Magenta
#property indicator_width1 2
//
//
//
//
//
#define PRICE_HA 7
extern string _ = "Parameters";
extern int iPerioda = 144;
extern int iPricea = PRICE_TYPICAL;
extern int iPeriodb = 21;
extern int iPriceb = PRICE_HA;
//
//
//
//
//
double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
string IndicatorFileName;
bool calcTEMA;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
calcTEMA = ( _ == "calculateTEMA" );
IndicatorBuffers(8);
SetIndexBuffer(0,buffer1);
SetIndexBuffer(1,buffer2);
SetIndexBuffer(2,buffer3);
SetIndexBuffer(3,buffer4);
SetIndexBuffer(4,buffer5);
SetIndexBuffer(5,buffer6);
SetIndexBuffer(6,buffer7);
SetIndexBuffer(7,buffer8);
if (calcTEMA) return(0);
//
//
//
//
//
IndicatorShortName("ZeroLag TEMA ("+iPerioda+","+iPeriodb+")");
SetIndexLabel(0,"ZL TEMA "+iPerioda);
SetIndexLabel(1,"ZL TEMA "+iPeriodb);
IndicatorFileName = WindowExpertName();
return(0);
}
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
double alphaa = 2.0 / (1.0 + iPerioda);
double alphab = 2.0 / (1.0 + iPeriodb);
int counted_bars = IndicatorCounted();
int i,limit;
if(counted_bars < 0) return(-1);
if(counted_bars > 0) counted_bars--;
limit = Bars-counted_bars;
//
//
//
//
//
if (calcTEMA) { calculateTEMA(limit); return(0);}
//
//
//
//
//
for (i=limit; i>=0; i--)
{
if (iPerioda > 1)
{
double tema1 = iCustom(NULL,0,IndicatorFileName,"calculateTEMA",iPerioda,iPricea,0,i);
buffer8[i] = buffer8[i+1]+alphaa*(tema1 -buffer8[i+1]);
buffer7[i] = buffer7[i+1]+alphaa*(buffer8[i]-buffer7[i+1]);
buffer6[i] = buffer6[i+1]+alphaa*(buffer7[i]-buffer6[i+1]);
buffer1[i] = tema1+(tema1-(3*buffer8[i]-3*buffer7[i]+buffer6[i]));
}
//
//
//
//
//
if (iPeriodb > 1)
{
double tema2 = iCustom(NULL,0,IndicatorFileName,"calculateTEMA",iPeriodb,iPriceb,0,i);
buffer5[i] = buffer5[i+1]+alphab*(tema2 -buffer5[i+1]);
buffer4[i] = buffer4[i+1]+alphab*(buffer5[i]-buffer4[i+1]);
buffer3[i] = buffer3[i+1]+alphab*(buffer4[i]-buffer3[i+1]);
buffer2[i] = tema2+(tema2-(3*buffer5[i]-3*buffer4[i]+buffer3[i]));
}
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
void calculateTEMA(int limit)
{
double alpha = 2.0 / (1.0 + iPerioda);
double price;
//
//
//
//
//
for (int i=limit; i>= 0; i--)
{
if (iPricea==PRICE_HA)
{
double haOpen = (buffer7[i+1] + buffer8[i+1]) / 2;
double haClose = (Open[i] + High[i] + Low[i] + Close[i]) / 4;
double haHigh = MathMax(High[i], MathMax(haOpen,haClose));
double haLow = MathMin(Low[i] , MathMin(haOpen,haClose));
if(haOpen <haClose) { buffer5[i] = haLow; buffer6[i] = haHigh; }
else { buffer5[i] = haHigh; buffer6[i] = haLow; }
buffer7[i] = haOpen;
buffer8[i] = haClose;
price = (buffer5[i] + buffer6[i] + buffer7[i] + buffer8[i])/4;
}
//
//
//
//
//
else price = iMA(NULL,0,iPerioda,0,MODE_EMA,iPricea,i);
//
//
//
//
//
buffer4[i] = buffer4[i+1]+alpha*(price -buffer4[i+1]);
buffer3[i] = buffer3[i+1]+alpha*(buffer4[i]-buffer3[i+1]);
buffer2[i] = buffer2[i+1]+alpha*(buffer3[i]-buffer2[i+1]);
buffer1[i] = 3*buffer4[i]-3*buffer3[i]+buffer2[i];
}
}
Comments