GannZigZag_002

Download script
Author:
GannZigZag_002

This script is a specialized technical analysis tool designed for the MetaTrader 4 platform. It automates the identification of significant structural shifts in price action by tracking "extremes"—specifically, local highs and lows—to determine the prevailing trend direction.

Core Logic Overview

The script functions by monitoring price movement to identify whether the market is currently trending upward or downward based on a user-defined sensitivity range. It treats the market as a series of peaks and valleys, attempting to filter out minor "noise" to highlight significant structural changes.

How it Works

1. Trend Detection & Directionality The indicator maintains an internal state that tracks whether the current trend is bullish (up) or bearish (down). It achieves this by counting consecutive occurrences of new highs and new lows.

  • If the price consistently hits new highs, the "upward" counter increases.
  • If the price consistently hits new lows, the "downward" counter increases.
  • The script uses a user-defined parameter (GSv_range) to determine how many of these movements must occur before it officially recognizes a shift in trend.

2. Extreme Identification As the script scans historical data (from the beginning of the chart toward the present), it looks for specific triggers:

  • New Extremes: When the price exceeds the previous known high or falls below the previous known low, the script updates its internal memory of those boundaries.
  • Trend Reversal: If the counts for highs and lows reach the specified range simultaneously (or meet specific criteria relative to the candle's opening and closing prices), the script identifies a "pivot point." At this moment, it switches the trend direction and marks the specific price level as a significant boundary.

3. Visual Representation Once a trend shift is confirmed:

  • The script draws a visual line (a dotted section) on the chart.
  • This line connects these major structural peaks or valleys.
  • It essentially "connects the dots" of the market's most significant highs and lows, providing a cleaner view of the price action by ignoring small fluctuations that do not meet the defined range criteria.

4. Intelligent Filtering To ensure the lines are clean and meaningful, the script includes logic to handle "inner" bars—price candles that stay within the boundaries of the previous extremes. It effectively ignores these until a new significant high or low is established. Additionally, it uses the relationship between a candle's open and close price to help decide which specific extreme (high or low) should be anchored when a trend transition occurs.

Summary for the User

In simple terms, this tool acts like an automated "trend mapper." It watches the market history, identifies the most important peaks and valleys based on your preferred sensitivity, and draws a path between them. This allows you to see the primary skeleton of the price movement without being distracted by minor, insignificant price movements.

Miscellaneous
Implements a curve of type %1Uses files from the file systemIt issuies visual alerts to the screenIt writes information to file
5 Views
0 Downloads
0 Favorites
GannZigZag_002
//+------------------------------------------------------------------+
//|                                                   GannZIGZAG.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Black
//---- input parameters
extern int GSv_range=2;
//---- buffers
double GSv_sl[];
double GSv_m[];
//----
bool draw_up=0,draw_dn=0,initfl=0;
int  fPoint_i,sPoint_i,s_up,s_dn,drawf,lb,idFile;
double h,l;
bool cur_h=0,cur_l=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_SECTION,STYLE_DOT);
   //SetIndexStyle(1,DRAW_SECTION);
   SetIndexBuffer(0,GSv_sl);
   //SetIndexBuffer(1,GSv_m);
   SetIndexEmptyValue(0,0.0);
   //SetIndexEmptyValue(1,0.0);
   FileDelete("Gann.txt");
   idFile=FileOpen("Gann.txt",FILE_READ|FILE_WRITE,"  ");
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   int cb,limit,i;
//---- 
   if( GSv_range<1 )
   {
      Alert("Èíäèêàòîð ðàññ÷èòûâàåò çíà÷åíèÿ /n ïðè ïàðàìåòðå GSv_range íå ìåíüøå 1!!!");
      return(-1);
   }
   if( counted_bars<0 )
   {
      return(-1);
   }
   else
   {
      if( Bars-1-counted_bars<=0 )
      {
         limit=0;
      }
      else
      {
         limit=Bars-1-counted_bars;
      }
   }
   //ïåðâîíà÷àëüíàÿ èíèöèàëèçàöèÿ
   if( initfl!=1 )
   {
      myInit();
   }
   //FileWrite(idFile,"  0. Áàðîâ íà ãðàôèêå "+Bars);
   //öèêë ïî áàðàì
   for( cb=limit;cb>=0;cb--)
   {
      if( cb==0 ) FileWrite(idFile,"---- èíäåêñ òåêóùåãî áàðà "+cb+" "+(Bars-1-cb)+" âðåìÿ "+TimeToStr(Time[cb])+" áàðîâ íà ãðàôèêå "+Bars);
      //åñëè íà ïðåäûäóùåì áàðå áûë îòðèñîâàí ýêñòðåìóì
      if( GSv_sl[cb+1]>0 && lb!=Bars-1-cb )
      {
         if( draw_up!=0 )
         {
            s_dn=0;
            if( cb==0 ) FileWrite(idFile,"  1. Áûë îòðèñîâàí ìàêñèìóì, ñ÷åò÷èê ìèíèìóìîâ îáíóëåí");
         }
         else
         {
            if( draw_dn!=0 )
            {
               s_up=0;
               if( cb==0 ) FileWrite(idFile,"  2. Áûë îòðèñîâàí ìèíèìóì, ñ÷åò÷èê ìàêñèìóìîâ îáíóëåí");
            }
         }
      }
      if( lb!=Bars-1-cb )
      {
         cur_h=0;
         cur_l=0;
         if( cb==0 ) FileWrite(idFile,"  2.1 íîâûé áàð ôëàãè íàëè÷èÿ ìàêñèìóìà è ìèíèìóìà ñáðîøåíû");
      }
      if( cb>Bars-2-drawf || (High[cb]<=High[cb+1] && Low[cb]>=Low[cb+1]) )
      {
         if( cb==0 ) FileWrite(idFile,"  3. Áàð ëèáî 'âíóòðåííèé', ëèáî äî ïåðâîãî ýêñòðåìóìà");
         continue;
      }
      if( draw_up!=0 )
      {
         if( cb==0 ) FileWrite(idFile,"  4. Èíäèêàòîð íàïðàâëåí ââåðõ");
         //åñëè ëèíèÿ íàïðàâëåíà ââåðõ
         if( High[cb]>h )
         {
            //åñëè äîñòèãíóò íîâûé ìàêñèìóì
            h=High[cb];
            cur_h=1;
            if( cb==0 ) FileWrite(idFile,"  5. Äîñòèãíóò íîâûé ìàêñèìóì = "+h+" cur_h "+cur_h);
         }
         if( Low[cb]<l )
         {
            //åñëè äîñòèãíóò íîâûé ìèíèìóì
            l=Low[cb];
            if( cb==0 ) FileWrite(idFile,"  6. Äîñòèãíóò íîâûé ìèíèìóì = "+l);
            //åñëè ýòî íå òîò æå ñàìûé áàð
            if( lb!=Bars-1-cb || cur_l!=1 ) s_dn++;
            cur_l=1;
            if( cb==0 ) FileWrite(idFile,"  7. Áàð íîâûé, ñ÷åò÷èê ìèíèìóìîâ óâåëè÷åí "+s_dn+" cur_l "+cur_l);
         }
         //åñëè ñ÷åò÷èêè ðàâíû
         if( s_up==s_dn )
         {
            if( cb==0 ) FileWrite(idFile,"  8. Ñ÷åò÷èêè ðàâíû");
            //åñëè ïîñëåäíèé áàð îäíîâðåìåííî íîâûé ìàêñèìóì è ìèíèìóì
            if( cur_h==cur_l && cur_l==1 )
            {
               if( cb==0 ) FileWrite(idFile,"  9. Åñòü äâà ýêñòðåìóìà");
               //åñëè ñâå÷à ìåäâåæüÿ
               if( Close[cb]<=Open[cb] )
               {
                  draw_up=0;
                  draw_dn=1;
                  fPoint_i=sPoint_i;
                  sPoint_i=Bars-1-cb;
                  GSv_sl[cb]=l;
                  for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                  {
                     GSv_sl[i]=0;
                  }
                  if( cb==0 ) FileWrite(idFile,"  10. Ñâå÷à ìåäâåæüÿ, ëèíèÿ ââåðõ = "+draw_up+", ëèíèÿ âíèç,"+draw_dn+" fPoint_i = "+fPoint_i+" sPoint_i "+sPoint_i+" èíäèêàòîð = "+GSv_sl[cb]);
               }
               else
               {
                  //åñëè ñâå÷à áû÷üÿ
                  sPoint_i=Bars-1-cb;
                  GSv_sl[cb]=h;
                  for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                  {
                     GSv_sl[i]=0;
                  }
                  if( cb==0 ) FileWrite(idFile,"  11. Ñâå÷à áû÷üÿ, ëèíèÿ ââåðõ = "+draw_up+", ëèíèÿ âíèç,"+draw_dn+" fPoint_i = "+fPoint_i+" sPoint_i "+sPoint_i+" èíäèêàòîð = "+GSv_sl[cb]);
               }
            }
            else
            {
               //åñëè ïîñëåäíèé áàð òîëüêî íîâûé ìàêñèìóì
               if( cur_h==1 )
               {
                  sPoint_i=Bars-1-cb;
                  GSv_sl[cb]=h;
                  l=Low[cb];
                  for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                  {
                     GSv_sl[i]=0;
                  }
                  if( cb==0 ) FileWrite(idFile,"  12. Òîëüêî ìàêñèìóì, èíäèêàòîð = "+GSv_sl[cb]+" sPoint_i "+sPoint_i+" l "+l);
               }
               else
               {
                  if( cur_l==1 )
                  {
                     //åñëè ïîñëåäíèé áàð òîëüêî íîâûé ìèíèìóì
                     draw_up=0;
                     draw_dn=1;
                     fPoint_i=sPoint_i;
                     sPoint_i=Bars-1-cb;
                     GSv_sl[cb]=l;
                     h=High[cb];
                     for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                     {
                        GSv_sl[i]=0;
                     }
                     if( cb==0 ) FileWrite(idFile,"  13. Òîëüêî ìèíèìóì, èíäèêàòîð = "+GSv_sl[cb]+" fPoint_i "+fPoint_i+" sPoint_i "+sPoint_i+" draw_up "+draw_up+" draw_dn "+draw_dn+" h "+h);
                  }
               }
            }
         }
         else
         {
            //èíà÷å åñëè ñìåíû íàïðàâëåíèÿ íåò ÿâíî (ñ÷åò÷èê Dn ñâå÷åé íå ðàâåí GSv_range)
            if( cb==0 ) FileWrite(idFile,"  14. Ñ÷åò÷èêè íå ðàâíû");
            //åñëè äîñòèãíóò íîâûé ìàêñèìóì
            if( cur_h==1 )
            {
               sPoint_i=Bars-1-cb;
               GSv_sl[cb]=h;
               for( i=cb+1;i<Bars-1-fPoint_i;i++ )
               {
                  GSv_sl[i]=0;
               }
               l=Low[cb];
               if( cb==0 ) FileWrite(idFile,"  15. Åñòü íîâûé ìàêñèìóì, èíäèêàòîð "+GSv_sl[cb]+" sPoint_i "+sPoint_i+" l "+l);
            }
         }
      }
      else
      {
         //åñëè ëèíèÿ íàïðàâëåíà âíèç
         if( cb==0 ) FileWrite(idFile,"  16. Èíäèêàòîð íàïðàâëåí âíèç");
         if( High[cb]>h )
         {
            //åñëè äîñòèãíóò íîâûé ìàêñèìóì
            h=High[cb];
            if( cb==0 ) FileWrite(idFile,"  17. Äîñòèãíóò íîâûé ìàêñèìóì "+h);
            if( lb!=Bars-1-cb || cur_h!=1 ) s_up++;
            cur_h=1;
            //åñëè ýòî íå òîò æå ñàìûé áàð
            if( cb==0 ) FileWrite(idFile,"  18. Íîâûé áàð, ñ÷åò÷èê ìàêñèìóìîâ óâåëè÷åí "+s_up+" cur_h "+cur_h+" h "+h);
         }
         if( Low[cb]<l )
         {
            //åñëè äîñòèãíóò íîâûé ìèíèìóì
            l=Low[cb];
            cur_l=1;
            if( cb==0 ) FileWrite(idFile,"  19. Äîñòèãíóò íîâûé ìèíèìóì "+l+" cur_l "+cur_l);
         }
         //åñëè ñ÷åò÷èêè ðàâíû 
         if( s_up==s_dn )
         {
            if( cb==0 ) FileWrite(idFile,"  20. Ñ÷åò÷èêè ðàâíû");
            //åñëè ïîñëåäíèé áàð îäíîâðåìåííî íîâûé ìàêñèìóì è ìèíèìóì
            if( cur_h==cur_l && cur_l==1 )
            {
               if( cb==0 ) FileWrite(idFile,"  21. Åñòü äâà ýêñòðåìóìà");
               //åñëè ñâå÷à ìåäâåæüÿ
               if( Close[cb]<=Open[cb] )
               {
                  sPoint_i=Bars-1-cb;
                  GSv_sl[cb]=l;
                  for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                  {
                     GSv_sl[i]=0;
                  }
                  if( cb==0 ) FileWrite(idFile,"  22. Ñâå÷à ìåäâåæüÿ, èíäèêàòîð "+GSv_sl[cb]+" sPoint_i "+sPoint_i);
               }
               else
               {
                  //åñëè ñâå÷à áû÷üÿ
                  draw_up=1;
                  draw_dn=0;
                  fPoint_i=sPoint_i;
                  sPoint_i=Bars-1-cb;
                  GSv_sl[cb]=h;
                  for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                  {
                     GSv_sl[i]=0;
                  }
                  if( cb==0 ) FileWrite(idFile,"  23. Ñâå÷à áû÷üÿ, èíäèêàòîð "+GSv_sl[cb]+" draw_up "+draw_up+" draw_dn "+draw_dn+" sPoint_i "+sPoint_i+" fPoint_i "+fPoint_i);
               }
            }
            else
            {
               //åñëè ïîñëåäíèé áàð òîëüêî íîâûé ìàêñèìóì
               if( cur_h==1 )
               {
                  draw_up=1;
                  draw_dn=0;
                  fPoint_i=sPoint_i;
                  sPoint_i=Bars-1-cb;
                  GSv_sl[cb]=h;
                  l=Low[cb];
                  for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                  {
                     GSv_sl[i]=0;
                  }
                  if( cb==0 ) FileWrite(idFile,"  24. Òîëüêî ìàêñèìóì, èíäèêàòîð "+GSv_sl[cb]+" draw_up "+draw_up+" draw_dn "+draw_dn+" sPoint_i "+sPoint_i+" fPoint_i "+fPoint_i+" l "+l);
               }
               else
               {
                  if( cur_l==1 )
                  {
                     //åñëè ïîñëåäíèé áàð òîëüêî íîâûé ìèíèìóì
                     sPoint_i=Bars-1-cb;
                     GSv_sl[cb]=l;
                     h=High[cb];
                     for( i=cb+1;i<Bars-1-fPoint_i;i++ )
                     {
                        GSv_sl[i]=0;
                     }
                     if( cb==0 ) FileWrite(idFile,"  25. Òîëüêî ìèíèìóì, èíäèêàòîð "+GSv_sl[cb]+" sPoint_i "+sPoint_i+" h "+h);
                  }
               }
            }
         }
         else
         {
            //èíà÷å åñëè ñìåíû íàïðàâëåíèÿ íåò ÿâíî (ñ÷åò÷èê Up ñâå÷åé íå ðàâåí GSv_range)
            if( cb==0 ) FileWrite(idFile,"  26. Ñ÷åò÷èêè íå ðàâíû");
            //åñëè äîñòèãíóò íîâûé ìèíèìóì
            if( cur_l==1 )
            {
               sPoint_i=Bars-1-cb;
               GSv_sl[cb]=l;
               for( i=cb+1;i<Bars-1-fPoint_i;i++ )
               {
                  GSv_sl[i]=0;
               }
               h=High[cb];
               if( cb==0 ) FileWrite(idFile,"  27. Äîñòèãíóò íîâûé ìèíèìóì, èíäèêàòîð "+GSv_sl[cb]+" sPoint_i "+sPoint_i+" h "+h);
            }
         }
      }
      if( lb!=Bars-1-cb ) lb=Bars-1-cb;
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   FileClose(idFile);
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// Ôóíêöèÿ íà÷àëüíîé èíèöèàëèçàöèè èíäèêàòîðà                        |
//+------------------------------------------------------------------+
void myInit()
  {
//---- 
   int cb;
   fPoint_i=0;
   h=High[Bars-1];
   l=Low[Bars-1];
   for( cb=Bars-2;cb>=0;cb--)
   {
      if( High[cb]>High[cb+1] || Low[cb]<Low[cb+1] )
      {
         if( High[cb]>h && High[cb]>High[cb+1] )
         {
            s_up++;
         }
         if( Low[cb]<l && Low[cb]<Low[cb+1] )
         {
            s_dn++;
         }
      }
      else
      {
         continue;
      }
      if( s_up==s_dn && s_up==GSv_range )
      {
         h=High[cb];
         l=Low[cb];
         sPoint_i=Bars-1-cb;
         if( Close[cb]>=Open[cb] )
         {
            s_dn=0;
            GSv_sl[Bars-1]=Low[Bars-1];
            GSv_sl[cb]=High[cb];
            draw_up=1;
            break;
         }
         else
         {
            s_up=0;
            GSv_sl[Bars-1]=High[Bars-1];
            GSv_sl[cb]=Low[cb];
            draw_dn=1;
            break;
         }
      }
      else
      {
         h=High[cb];
         l=Low[cb];
         sPoint_i=Bars-1-cb;
         if( s_up==GSv_range )
         {
            s_dn=0;
            GSv_sl[Bars-1]=Low[Bars-1];
            GSv_sl[cb]=High[cb];
            draw_up=1;
            break;
         }
         else
         {
            if( s_dn==GSv_range )
            {
               s_up=0;
               GSv_sl[Bars-1]=High[Bars-1];
               GSv_sl[cb]=Low[cb];
               draw_dn=1;
               break;
            }
         }
      }
   }
   initfl=1;
   drawf=sPoint_i;
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments