i_ZigZag_NK
Download scriptThis script is designed to create a ZigZag indicator on a trading chart. In simple terms, its purpose is to filter out the "noise" of constant price fluctuations and highlight the significant structural points of a market—specifically the major highs and lows.
How it Works
The logic follows a three-step process to ensure the lines on your chart accurately represent the trend without cluttering the screen with every minor price movement:
1. Initial Filtering (Depth and Deviation) The script looks at a specific window of recent price history (defined by the "Depth" setting). It identifies the highest peak and lowest valley within that window. However, it doesn't immediately draw a line to every peak; it checks if the price movement is large enough to be considered significant (the "Deviation"). If a move is too small, the script ignores it.
2. The "Backstep" Verification To ensure smooth lines, the script uses a "Backstep" logic. This means that once a new high or low is identified, the script looks slightly backward to see if any other points were even higher or lower in that immediate area. If it finds a more extreme point nearby, it selects that one instead of the current one. This prevents the indicator from creating "jagged" lines for tiny price wobbles.
3. Trend Consolidation (The Peak-to-Valley Logic) This is the core of the ZigZag behavior. The script maintains a memory of the most recent "confirmed" high and low points. It follows these rules:
- If the price creates a new high that is higher than the previous peak, it updates the peak.
- If the price starts moving down from a peak and establishes a new low, it connects the two.
- If the price continues to move in one direction (e.g., making a series of higher highs), the script waits until the trend actually reverses before finalizing the line.
Visual Outcome
For the user, this logic results in a clean, continuous line that connects significant turning points. It effectively "skips" minor retracements, allowing you to see the underlying structure of the market—such as identifying support levels, resistance zones, and overall trend direction—much more clearly than by looking at raw price bars alone.
//+------------------------------------------------------------------+
//| ZigZag_NK.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//---- îòðèñîâêà èíäèêàòîðà â îñíîâíîì îêíå
#property indicator_chart_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôôåðîâ
#property indicator_buffers 2
//---- öâåò èíäèêàòîðà
#property indicator_color1 Aqua
#property indicator_color2 Aqua
//---- òîëùèíà èíäèêàòîðíîé ëèíèè
#property indicator_width1 0
#property indicator_width2 0
//---- ñòèëü èíäèêàòîðíîé ëèíèè
#property indicator_style1 1
#property indicator_style2 1
//---- ÂÕÎÄÍÛÅ ÏÀÐÀÌÅÒÐÛ ÈÍÄÈÊÀÒÎÐÀ
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
//----
//---- èíäèêàòîðíûå áóôôåðû
double LowestBuffer[],HighestBuffer[];
//----+ Ââåäåíèå öåëûõ ïåðåìåííûõ ïàìÿòè äëÿ ïåðåñ÷¸òà èíäèêàòîðà òîëüêî
// íà íåïîäñ÷èòàííûõ áàðàõ
int MaxBar,time2,LASTLOWPOS,LASTHIGHPOS,LHTIME,LLTIME,size;
//----+ Ââåäåíèå ïåðåìåííûõ ïàìÿòè ñ ïëàâàþùåé òî÷êîé äëÿ ïåðåñ÷¸òà
// èíäèêàòîðà òîëüêî íà íåïîäñ÷èòàííûõ áàðàõ
double LowestMEMORY[1],HighestMEMORY[1],LASTLOW0,LASTLOW1,LASTHIGH0,
LASTHIGH1,LASTHIGHEST,LASTLOWEST;
static datetime PrevTimeCalc=0;
//+------------------------------------------------------------------+
//| ZigZag initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- äâà èíäèêàòîðíûõ áóôôåðà èñïîëüçîâàí äëÿ ñ÷¸òà
SetIndexBuffer(0,LowestBuffer);
SetIndexBuffer(1,HighestBuffer);
//---- Ñòèëü èñïîëíåíèÿ ãðàôèêà âèäå ëîìàííîé ZigZag
SetIndexStyle(0,DRAW_ZIGZAG);
SetIndexStyle(1,DRAW_ZIGZAG);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
//---- èìåíà äëÿ îêîí äàííûõ è ëýéáû äëÿ ñóáúîêîí
SetIndexLabel(0,"Low" );
SetIndexLabel(1,"High");
IndicatorShortName("ZigZag (ExtDepth="+ExtDepth+", ExtDeviation="+
ExtDeviation+", ExtBackstep="+ExtBackstep+" )");
//---- îïðåäåëåíèå ðàçìåðîâ âðåìåííûõ áóôôåðîâ
size=ExtBackstep+1;
//---- èçìåíåíèå ðàçìåðîâ âðåìåííûõ áóôôåðîâ
if(ArrayResize(LowestMEMORY,size)!=size)return(-1);
if(ArrayResize(HighestMEMORY,size)!=size)return(-1);
//---- çàâåðøåíèå èíèöèàëèçàöèè
return(0);
}
//+------------------------------------------------------------------+
//| ZigZag iteration function |
//+------------------------------------------------------------------+
int start()
{
if (PrevTimeCalc==Time[0]) return;
PrevTimeCalc=Time[0];
//----+ ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ êîððåêòíîãî
// ðàñ÷¸òà èíäèêàòîðà
if (Bars-1<ExtDepth)return(0);
//----+ Ââåäåíèå öåëûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîäñ÷èòàííûõ áàðîâ
int limit,iii,counted_bars=IndicatorCounted();
//---- ïðîâåðêà íà âîçìîæíûå îøèáêè
if (counted_bars<0)return(-1);
//---- ïîñëåäíèé ïîäñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí
if (counted_bars>0) counted_bars--;
//----+ Ââåäåíèå ïåðåìåííûõ
int bar,back,lasthighpos,lastlowpos;
double curlow,curhigh,lasthigh0,lastlow0,lasthigh1,lastlow1,val,res;
//---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, íà÷èíàÿ ñ êîòîðîãî
// áóäåò ïðîèçåä¸í ïîëûé ïåðåñ÷¸ò âñåõ áàðîâ
MaxBar=Bars-ExtDepth;
//---- îïðåäåëåíèå íîìåðà ñòàðòîâîãî áàðà â öèêëå, íà÷èíàÿ ñ êîòîðîãî
// áóäåò ïðîèçîäèòüñÿ ïåðåñ÷¸ò íîâûõ áàðîâ
limit=Bars-counted_bars-1;
//+--- âîññòàíîâëåíèå çíà÷åíèé ïåðåìåííûõ +======+
int Tnew=Time[limit+1];
if (limit<MaxBar)
{
if (Tnew==time2)
{
lastlow0=LASTLOW0;
lasthigh0=LASTHIGH0;
//+---
lastlow1=LASTLOW1;
lasthigh1=LASTHIGH1;
//+---
lastlowpos=iBarShift(NULL,0,LLTIME,true);
lasthighpos=iBarShift(NULL,0,LHTIME,true);
//+--- ïðîâåðêà ïåðåìåííûõ lasthighpos è lastlowpos íà
// êîððåêòíîñòü äëÿ äàëüíåéøåãî ðàñ÷¸òà
if ((lasthighpos<0)||(lastlowpos<0))
{
Print("Îøèáêà âîññòàíîâëåíèÿ ïåðåìåííûõ!!!" +
" Ïîòåðÿíû çíà÷åíèÿ ïåðåìåííûõ lasthighpos è lastlowpos");
Print("Áóäåò ïðîèçâåä¸í ïåðåñ÷¸ò èíäèêàòîðà íà âñåõ áàðàõ!");
return(-1);
}
//+---
LowestBuffer[lastlowpos]=LASTLOWEST;
HighestBuffer[lasthighpos]=LASTHIGHEST;
//+---
for(iii=size-1; iii>=0; iii--)
{
LowestBuffer[iii+limit+1]=LowestMEMORY[iii];
HighestBuffer[iii+limit+1]=HighestMEMORY[iii];
}
//+---
}
else
{
if (Tnew>time2)
Print("Îøèáêà âîññòàíîâëåíèÿ ïåðåìåííûõ!!! Tnew>time2");
else Print("Îøèáêà âîññòàíîâëåíèÿ ïåðåìåííûõ!!! Tnew<time2");
Print("Áóäåò ïðîèçâåä¸í ïåðåñ÷¸ò èíäèêàòîðà íà âñåõ áàðàõ!");
return(-1);
}
}
//+--- +==========================================+
//---- èíèöèàëèçàöèÿ íóëÿ
if (limit>=MaxBar)
{
for (bar=Bars-1; bar>=MaxBar;bar--)
{
LowestBuffer [bar]=0.0;
HighestBuffer[bar]=0.0;
}
lastlow1=-1;
lasthigh1=-1;
lastlowpos=-1;
lasthighpos=-1;
limit=MaxBar;
}
//----+ <<< íà÷àëî ïåðâîãî áîëüøîãî öèêëà >>> -----------+
for(bar=limit; bar>=0; bar--)
{
//+--- Ñîõðàíåíèå çíà÷åíèé ïåðåìåííûõ +=========+
if (bar==1)
{
if(((limit==1)&&(time2==Time[2]))||(limit>1))
{
LASTLOW0=lastlow0;
LASTHIGH0=lasthigh0;
//+---
for(iii=size-1; iii>=0; iii--)
{
LowestMEMORY[iii]=LowestBuffer[iii+2];
HighestMEMORY[iii]=HighestBuffer[iii+2];
}
}
}
//+---+=========================================+
//--- low
val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,bar)];
if(val==lastlow0) val=0.0;
else
{
lastlow0=val;
if((Low[bar]-val)>(ExtDeviation*Point))val=0.0;
else
{
for(back=1; back<=ExtBackstep; back++)
{
res=LowestBuffer[bar+back];
if((res!=0)&&(res>val))LowestBuffer[bar+back]=0.0;
}
}
}
LowestBuffer[bar]=val;
//--- high
val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,bar)];
if(val==lasthigh0) val=0.0;
else
{
lasthigh0=val;
if((val-High[bar])>(ExtDeviation*Point))val=0.0;
else
{
for(back=1; back<=ExtBackstep; back++)
{
res=HighestBuffer[bar+back];
if((res!=0)&&(res<val))HighestBuffer[bar+back]=0.0;
}
}
}
HighestBuffer[bar]=val;
}
//----+ êîíåö ïåðâîãî áîëüøîãî öèêëà
//----+ <<< íà÷àëî âòîðîãî áîëüøîãî öèêëà >>> -----------+
for(bar=limit; bar>=0; bar--)
{
//+--- Ñîõðàíåíèå çíà÷åíèé ïåðåìåííûõ +====+
if (bar==1)
{
if(((limit==1)&&(time2==Time[2]))||(limit>1))
{
time2=Time [2];
LASTLOW1=lastlow1;
LASTHIGH1=lasthigh1;
//+---
LLTIME=Time[lastlowpos];
LHTIME=Time[lasthighpos];
//+---
LASTLOWEST=LowestBuffer[lastlowpos];
LASTHIGHEST=HighestBuffer[lasthighpos];
}
}
//+---+====================================+
curlow=LowestBuffer[bar];
curhigh=HighestBuffer[bar];
//---
if((curlow==0)&&(curhigh==0))continue;
//---
if(curhigh!=0)
{
if(lasthigh1>0)
{
if(lasthigh1<curhigh)HighestBuffer[lasthighpos]=0;
else HighestBuffer[bar]=0;
}
//---
if(lasthigh1<curhigh || lasthigh1<0)
{
lasthigh1=curhigh;
lasthighpos=bar;
}
lastlow1=-1;
}
//----
if(curlow!=0)
{
if(lastlow1>0)
{
if(lastlow1>curlow) LowestBuffer[lastlowpos]=0;
else LowestBuffer[bar]=0;
}
//---
if((curlow<lastlow1)||(lastlow1<0))
{
lastlow1=curlow;
lastlowpos=bar;
}
lasthigh1=-1;
}
}
//----+ êîíåö âòîðîãî áîëüøîãî öèêëà
//+--------------------------------------------------------+
//---- çàâåðøåíèå âû÷èñëåíèé çíà÷åíèé èíäèêàòîðà
return(0);
}
//+-----------------------------------------------------------+
Comments