ZerolagStochs_editNKparam
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform, specifically aimed at identifying market trends with reduced delay. It utilizes a sophisticated variation of the "Stochastic Oscillator," blending multiple timeframes and weighting factors to create a more stable trend signal than a standard oscillator would provide alone.
Core Logic Overview
The primary goal of this script is to solve the common problem of "lag" in trading indicators—where the signal appears only after a move has already happened. It achieves this through a multi-layered mathematical approach:
1. Multi-Layered Stochastic Aggregation Instead of relying on a single calculation, the script processes five different Stochastic oscillators simultaneously. Each one operates on different periods (ranging from very fast to relatively slow).
- The script takes these five independent values.
- It applies specific "Factors" (weights) to each one.
- It then sums these weighted results together into a single consolidated value.
By combining multiple time horizons this way, the indicator attempts to capture both immediate price fluctuations and broader long-term momentum at the same time.
2. Dynamic Smoothing Mechanism Once the aggregated "Fast Trend" is calculated, the script applies a secondary smoothing process. This is designed to filter out market noise (random price "jitter") without sacrificing the responsiveness of the trend line. It uses a recursive calculation where the current value is blended with the previous value based on a user-defined smoothing parameter.
3. Visual Output The script generates two distinct lines on a separate window in your trading platform:
- Fast Trend Line: Represents the primary aggregated momentum of the five combined oscillators.
- Low Trend Line: Represents a smoothed version of that trend, intended to provide a cleaner, more stable visual path for identifying sustained market direction.
How it Works in Practice
When you view this indicator, it provides a composite "pulse" of the market. Because it pulls data from various periods (short-term and long-term), the resulting lines are intended to react quickly to new price action while remaining anchored to the overall trend. The user can adjust the "Slowing" factors and "Smoothing" values to customize how sensitive or stable the lines appear on their specific charts.
// Ðåäàêòèðîâàíèå è èñïðàâëåíèÿ 22.09.2008 Íèêîëàé Êîñèöèí
//+X================================================================X+
//| ZerolagStochs_.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+X================================================================X+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "perky_z@yahoo.com"
//---- îòðèñîâêà èíäèêàòîðà â îòäåëüîì îêíå
#property indicator_separate_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ
#property indicator_buffers 2
//---- öâåòà èíäèêàòîðà
#property indicator_color1 Blue
#property indicator_color2 Red
//----+ +----------------------------+
//---- ÂÕÎÄÍÛÅ ÏÀÐÀÌÅÒÐÛ ÈÍÄÈÊÀÒÎÐÀ |
//----+ +----------------------------+
extern int Slowing = 3;
extern int MA_Method = MODE_SMA;
extern int smoothing = 15;
//----
extern double Factor1 = 0.05;
extern int Kperiod1 = 8;
extern int Dperiod1 = 3;
//----
extern double Factor2 = 0.10;
extern int Kperiod2 = 21;
extern int Dperiod2 = 5;
//----
extern double Factor3 = 0.16;
extern int Kperiod3 = 34;
extern int Dperiod3 = 8;
//----
extern double Factor4 = 0.26;
extern int Kperiod4 = 55;
extern int Dperiod4 = 13;
//----
extern double Factor5 = 0.43;
extern int Kperiod5 = 89;
extern int Dperiod5 = 21;
//---- Îáúÿâëåíèå èíäèêàòîðíûõ áóôåðîâ
double FastTrendBuffer[];
double LowTrendBuffer[];
//---- Îáúÿâëåíèå öåëûõ êîíñòàíò
int StartBar;
//---- Îáúÿâëåíèå êîíñòàíò ñ ïëàâàþùåé òî÷êîé
double smoothConst;
//+X================================================================X+
//| ZerolagStochs() function |
//+X================================================================X+
double ZerolagStochs(string symbol, int timeframe, int MA_MODE, int LINE_MODE, int SLOWING, int bar)
{
//----+
double Stoch1, Stoch2, Stoch3, Stoch4, Stoch5;
//----
Stoch1 = Factor1 * iStochastic(symbol, timeframe, Kperiod1, Dperiod1, SLOWING, MA_MODE, NULL, LINE_MODE, bar);
Stoch2 = Factor2 * iStochastic(symbol, timeframe, Kperiod2, Dperiod2, SLOWING, MA_MODE, NULL, LINE_MODE, bar);
Stoch3 = Factor3 * iStochastic(symbol, timeframe, Kperiod3, Dperiod3, SLOWING, MA_MODE, NULL, LINE_MODE, bar);
Stoch4 = Factor4 * iStochastic(symbol, timeframe, Kperiod4, Dperiod4, SLOWING, MA_MODE, NULL, LINE_MODE, bar);
Stoch5 = Factor5 * iStochastic(symbol, timeframe, Kperiod5, Dperiod5, SLOWING, MA_MODE, NULL, LINE_MODE, bar);
//----
return(Stoch1 + Stoch2 + Stoch3 + Stoch4 + Stoch5);
//----+
}
//+X================================================================X+
//| ZerolagStochs indicator initialization function |
//+X================================================================X+
int init()
{
//---+
//---- 2 èíäèêàòîðíûõ áóôåðà èñïîëüçîâàíû äëÿ ñ÷¸òà
SetIndexBuffer(0, FastTrendBuffer);
SetIndexBuffer(1, LowTrendBuffer );
//---- îïðåäåëåíèå ñòèëÿ èñïîëíåíèÿ ãðàôèêà
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
//---- Óñòàíîâêà ôîðìàòà òî÷íîñòè îòîáðàæåíèÿ èíäèêàòîðà
IndicatorDigits(Digits);
//---- Èìÿ èíäèêàòîðà â ñóáúîêíå
IndicatorShortName("Zero Lag Stochs_ ");
//---- Ëýéáû äëÿ èíäèêàòîðíûõ ëèíèé
SetIndexLabel(0, "FastTrendLine");
SetIndexLabel(1, "LowTrendLine");
//---- Èíèöèàëèçàöèÿ êîíñòàíò
smoothConst = (smoothing - 1.0) / smoothing;
//----
int PeriodBuffer[5];
//---- Ðàñ÷¸ò ñòàðòîâîãî áàðà
PeriodBuffer[0] = Kperiod1 + Dperiod1;
PeriodBuffer[1] = Kperiod2 + Dperiod2;
PeriodBuffer[2] = Kperiod3 + Dperiod3;
PeriodBuffer[3] = Kperiod4 + Dperiod4;
PeriodBuffer[4] = Kperiod5 + Dperiod5;
//----
StartBar = ArrayMaximum(PeriodBuffer, WHOLE_ARRAY, 0) + 1;
//---- óñòàíîâêà íîìåðà áàðà,
//íà÷èíàÿ ñ êîòîðîãî áóäåò îòðèñîâûâàòüñÿ èíäèêàòîð
SetIndexDrawBegin(0, StartBar - 1);
SetIndexDrawBegin(1, StartBar);
//---- Çàâåðøåíèå èíèöèàëèçàöèè
return(0);
//---+
}
//+X================================================================X+
//| ZerolagStochs indicator start function |
//+X================================================================X+
int start()
{
//---+
//---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷¸òà
if (Bars < StartBar)
return(0);
//---- Îáúÿâëåíèå ñòàòè÷åñêèõ ïåðåìåííûõ
static bool TrueStart;
//---- Îáúÿâëåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double FastTrend, LowTrend;
double Stoch1, Stoch2, Stoch3, Stoch4, Stoch5;
//----+ Îáúÿâëåíèå öåëûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîñ÷èòàííûõ áàðîâ
int limit, MaxBar, bar, counted_bars = IndicatorCounted();
//---- ïðîâåðêà íà âîçìîæíûå îøèáêè
if (counted_bars < 0)
return(-1);
//---- ïîñëåäíèé ïîñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí
if (counted_bars > 0)
counted_bars--;
//---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà,
//íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò âñåõ áàðîâ
MaxBar = Bars - 1 - StartBar;
//---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà,
//íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò òîëüêî íîâûõ áàðîâ
limit = (Bars - 1 - counted_bars);
//---- èíèöèàëèçàöèÿ íóëÿ
if (limit >= MaxBar || !TrueStart)
{
limit = MaxBar;
TrueStart = true;
//----
for (bar = Bars - 1; bar > MaxBar; bar--)
{
FastTrendBuffer[bar] = 0.0;
LowTrendBuffer [bar] = 0.0;
}
FastTrend = ZerolagStochs(Symbol(), 0, MA_Method, MODE_MAIN, Slowing, MaxBar + 1);
LowTrendBuffer[MaxBar + 1] = FastTrend / smoothing;
}
//---+ ÎÑÍÎÂÍÎÉ ÖÈÊË ÐÀÑרÒÀ ÈÍÄÈÊÀÒÎÐÀ
for (bar = limit; bar>= 0; bar--)
{
FastTrend = ZerolagStochs(Symbol(), 0, MA_Method, MODE_MAIN, Slowing, bar);
//----
LowTrend = FastTrend / smoothing + LowTrendBuffer[bar + 1] * smoothConst;
//----
FastTrendBuffer[bar] = FastTrend;
LowTrendBuffer [bar] = LowTrend;
}
//---+
return(0);
//---+
}
//+X----------------------------------------------------------------X+
Comments