GannZigZag_v1

Download script
Author:
GannZigZag_v1

This script is designed to identify and map significant price structural shifts on a trading chart using logic inspired by the "ZigZag" method. It focuses on identifying key reversal points by tracking the extremes of market movement (highs and lows) over a specific range.

Core Logic Overview

The primary goal of this script is to filter out "market noise" and only plot significant structural peaks and valleys. It achieves this through several distinct logical layers:

1. Range-Based Filtering The script uses a user-defined parameter (GSv_range) to act as a sensitivity threshold. Instead of reacting to every small price fluctuation, the logic requires a specific amount of "momentum" or a series of consecutive movements in one direction before it confirms a new trend point.

2. Extreme Point Tracking As the script moves through historical data, it constantly monitors the highest highs and lowest lows. It maintains internal counters for upward and downward movements. If the price reaches a new high or a new low, the script updates its memory of the current "extreme."

3. Trend Reversal Identification The script determines when to draw a new line based on the following conditions:

  • Consecutive Movement: It counts how many times a new high or low is reached relative to the previous bars.
  • Symmetry Logic: When the count of upward movements reaches the defined range, it prepares for a potential reversal.
  • Candle Confirmation: To decide which way the trend has flipped (Up or Down), it looks at the price action of the current candle. For example, if a reversal is triggered, it checks whether the candle closed as "bullish" (closing higher than it opened) or "bearish" to determine the final direction of the plotted line.

4. Dynamic Line Drawing Once a valid structural point is confirmed:

  • The script "anchors" a new segment on the chart.
  • It clears any pending data points between the old anchor and the new one to ensure the visual output remains clean and connected.
  • It updates its internal direction (Up or Down) so that the next search for a reversal starts from the correct perspective.

Visual Behavior

From a user's perspective, this results in a stepped line on the chart that ignores minor price wobbles. The lines only appear to "bend" when the market has moved significantly enough to satisfy the GSv_range requirement and confirmed a change in direction via the candle's closing price.

Technical Execution summary

  • Initialization: When first loaded, the script scans backward from the current time to establish the first set of reliable "anchor points."
  • Real-time Processing: It continuously evaluates new incoming bars, updating its internal counters and potentially drawing a new line segment the moment the price breaks out of the established range in the opposite direction.
Miscellaneous
Implements a curve of type %1Uses files from the file systemIt issuies visual alerts to the screenIt writes information to file
6 Views
0 Downloads
0 Favorites
GannZigZag_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                   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