ZigZag_new_nen1

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
ZigZag_new_nen1

This script is designed to create a "ZigZag" indicator, which visually simplifies price movements on a trading chart by connecting significant highs and lows. Instead of showing every minor fluctuation in price, it identifies the major structural turning points, helping traders see the overarching trend more clearly.

Core Logic and Functionality

The logic follows a structured process to filter out "market noise" and identify meaningful price swings based on three main user-defined parameters: Depth (how far back to look for a point), Deviation (the minimum price change required to register a new point), and Backstep (a small buffer used to ensure distinct points aren't placed too close together).

1. Identifying Candidate Points The script first scans the historical price data to find potential "peaks" (highs) and "valleys" (lows). It looks for specific areas where the price has moved significantly enough to meet the user-defined deviation. If a movement is too small, the script ignores it as minor noise.

2. Filtering for Structural Significance Once candidate highs and lows are identified, the script performs a series of internal "cleansing" passes. It compares these points against one another to ensure they represent actual reversals. For example:

  • If two high points are found in a row, it determines which one is the true peak and removes the smaller one.
  • It ensures that the indicator doesn't create a series of "flat" lines or redundant points by checking the distance between consecutive turns.

3. Trend Verification In its final stages, the script applies a logic check to ensure the path of the ZigZag remains logical. It looks at the last four identified points to check for consistency. If it detects an impossible sequence—such as three consecutive points moving in a straight line or overlapping in a way that defies standard price movement—it removes the redundant data points.

4. Dynamic Updating The script is designed to be efficient. Rather than recalculating every single historical bar on every tick (which would slow down your platform), it intelligently identifies which parts of the chart need updating. It focuses primarily on the most recent data while retaining the integrity of the established historical points.

Summary for the User

In plain terms, this script acts as a "filter" for your charts. It ignores the tiny up-and-down vibrations of the market and draws a clear, continuous line connecting the most important price reversals. This allows you to see the "skeleton" of the price action, making it easier to identify support levels, resistance zones, and major trend changes.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
ZigZag_new_nen1
//+------------------------------------------------------------------+
//|                                                       ZigZag.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  1
#property indicator_color1 Red
#property indicator_width1 0
#property indicator_style1 1
//---- 
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
//---- 
//---- 
double ZigZagBuffer[];
int timeFirstBar=0;
//+------------------------------------------------------------------+
//| ZigZag initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   SetIndexBuffer(0,ZigZagBuffer); 
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexEmptyValue(0,0.0);
   IndicatorShortName("ZigZag("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- 
   return(0);
  }
//+------------------------------------------------------------------+
//|  ZigZag iteration function                                       |
//+------------------------------------------------------------------+
int start()
  {
   //----+ ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ êîððåêòíîãî ðàñ÷¸òà èíäèêàòîðà
   if (Bars-1<ExtDepth)return(0);
   //----+ Ââåäåíèå öåëûõ ïåðåìåííûõ ïàìÿòè äëÿ ïåðåñ÷¸òà èíäèêàòîðà òîëüêî íà íåïîäñ÷èòàííûõ áàðàõ
   static int time2,time3,time4;  
   //----+ Ââåäåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé äëÿ ïåðåñ÷¸òà èíäèêàòîðà òîëüêî íà íåïîäñ÷èòàííûõ áàðàõ
   static  double ZigZag2,ZigZag3,ZigZag4;
   //----+ Ââåäåíèå öåëûõ ïåðåìåííûõ äëÿ ïåðåñ÷¸òà èíäèêàòîðà òîëüêî íà íåïîäñ÷èòàííûõ áàðàõ è ïîëó÷åíèå óæå ïîäñ÷èòàííûõ áàðîâ
   int MaxBar,limit,supr2_bar,supr3_bar,supr4_bar,counted_bars=IndicatorCounted();
   //---- ïðîâåðêà íà âîçìîæíûå îøèáêè
   if (counted_bars<0)return(-1);
   //---- ïîñëåäíèé ïîäñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí
   if (counted_bars>0) counted_bars--;
   //----+ Ââåäåíèå ïåðåìåííûõ    
   int    shift, back,lasthighpos,lastlowpos;
   double val,res,TempBuffer[1];
   double curlow,curhigh,lasthigh,lastlow;
   //---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïîëûé ïåðåñ÷¸ò âñåõ áàðîâ
   MaxBar=Bars-ExtDepth; 
   //---- îïðåäåëåíèå íîìåðà ñòàðòîâîãî  áàðà â öèêëå, íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåäèòüñÿ  ïåðåñ÷¸ò íîâûõ áàðîâ
   if (counted_bars==0 || Bars-counted_bars>2)
     {
      limit=MaxBar;
     }
   else 
     {
      //----
      supr2_bar=iBarShift(NULL,0,time2,TRUE);
      supr3_bar=iBarShift(NULL,0,time3,TRUE);
      supr4_bar=iBarShift(NULL,0,time4,TRUE);
      //----
      limit=supr3_bar;      
      if ((supr2_bar<0)||(supr3_bar<0)||(supr4_bar<0))
         {
          limit=MaxBar;
          Print("Ñòàðòîâûé áàð íå íàéäåí, áóäåí ïðîèçâåä¸í ïåðåñ÷¸ò èíäèêàòîðà íà âñåõ áàðàõ" );
         }
     }
     
   //---- èíèöèàëèçàöèÿ íóëÿ
   if (limit>=MaxBar || timeFirstBar!=Time[Bars-1]) 
     {
      timeFirstBar=Time[Bars-1];
      for (shift=Bars-1; shift>0;shift--)ZigZagBuffer [shift]=0.0; 
      limit=MaxBar; 
     } 
   //----  
   //---- èçìåíåíèå ðàçìåðà âðåìåííîãî áóôåðà
   if(ArrayResize(TempBuffer,limit+ExtBackstep+1)!=limit+ExtBackstep+1)return(-1);
     
   //----+-------------------------------------------------+ 
   
   //----+ íà÷àëî ïåðâîãî áîëüøîãî öèêëà
   for(shift=limit; shift>=0; shift--)
     {
      //--- 
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else 
        { 
         lastlow=val; 
         if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ZigZagBuffer[shift+back];
               if((res!=0)&&(res>val)) ZigZagBuffer[shift+back]=0.0; 
              }
           }
        }
      if (Low[shift]==val) ZigZagBuffer[shift]=val;
      //--- 
      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else 
        {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=TempBuffer[shift+back];
               if((res!=0)&&(res<val)) TempBuffer[shift+back]=0.0; 
              } 
           }
        }
      if (High[shift]==val) TempBuffer[shift]=val;
     }
   //----+ êîíåö ïåðâîãî áîëüøîãî öèêëà 
      
   // final cutting 
      lasthigh=-1; lasthighpos=-1;
      lastlow= -1; lastlowpos= -1;
   //----+-------------------------------------------------+
   
   //----+ íà÷àëî âòîðîãî áîëüøîãî öèêëà

   for(shift=limit; shift>=0; shift--)
     {
      curlow=ZigZagBuffer[shift];
      curhigh=TempBuffer[shift];
      if((curlow==0)&&(curhigh==0)) continue;
      //---
      if(curhigh!=0)
        {
         if(lasthigh>0) 
           {
            if(lasthigh<curhigh) TempBuffer[lasthighpos]=0;
            else TempBuffer[shift]=0;
           }
         //---
         if(lasthigh<curhigh || lasthigh<0)
           {
            lasthigh=curhigh;
            lasthighpos=shift;
           }
         lastlow=-1;
        }
      //----
      if(curlow!=0)
        {
         if(lastlow>0)
           {
            if(lastlow>curlow) ZigZagBuffer[lastlowpos]=0;
            else ZigZagBuffer[shift]=0;
           }
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           } 
         lasthigh=-1;
        }
     }
   //----+ êîíåö âòîðîãî áîëüøîãî öèêëà
     
   //----+-------------------------------------------------+
   
   //----+ íà÷àëî òðåòüåãî öèêëà
   for(shift=limit; shift>=0; shift--)
     {
       res=TempBuffer[shift];
       if(res!=0.0) ZigZagBuffer[shift]=res;
     }
     //----+ êîíåö òðåòüåãî öèêëà
     
   //+--- Âîññòàíîâëåíèå çíà÷åíèé èíäèêàòîðíîãî áóôôåðà, êîòîðûå ìîãëè áûòü óòåðÿíû 
   if (limit<MaxBar)
     {
      ZigZagBuffer[supr2_bar]=ZigZag2; 
      ZigZagBuffer[supr3_bar]=ZigZag3; 
      ZigZagBuffer[supr4_bar]=ZigZag4; 
      for(int qqq=supr4_bar-1; qqq>supr3_bar; qqq--)ZigZagBuffer[qqq]=0; 
      for(int ggg=supr3_bar-1; ggg>supr2_bar; ggg--)ZigZagBuffer[ggg]=0;
     }
   //+---+============================================+
  
   //+--- èñïðàâëåíèå âîçíèêàþùèõ ãîðáîâ 
   double vel1, vel2, vel3, vel4;
   int bar1, bar2, bar3, bar4;
   int count;
   if (limit==MaxBar)supr4_bar=MaxBar;
   for(int bar=supr4_bar; bar>=0; bar--)
    {
     if (ZigZagBuffer[bar]!=0)
      {
       count++;
       vel4=vel3;bar4=bar3;
       vel3=vel2;bar3=bar2;
       vel2=vel1;bar2=bar1;
       vel1=ZigZagBuffer[bar];bar1=bar;
       if (count<3)continue; 
       if ((vel3<vel2)&&(vel2<vel1))ZigZagBuffer[bar2]=0;
       if ((vel3>vel2)&&(vel2>vel1))ZigZagBuffer[bar2]=0;
       if ((vel2==vel1)&&(vel1!=0 ))ZigZagBuffer[bar1]=0;      
     }
    } 
   //+--- çàïîìèíàíèå âðåìåíè òð¸õ ïîñëåäíèõ ïåðåãèáîâ Çèãçàãà è çíà÷åíèé èíäèêàòîðà â ýòèõ òî÷êàõ 
   time2=Time[bar2];
   time3=Time[bar3];
   time4=Time[bar4];
   ZigZag2=vel2;  
   ZigZag3=vel3; 
   ZigZag4=vel4; 
   //+---          
 //---- çàâåðøåíèå âû÷èñëåíèé çíà÷åíèé èíäèêàòîðà
return(0);
}
 //---+ +---------------------------------------------------------------------+

Comments