//+------------------------------------------------------------------+
//|                                                   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);
  }
//+------------------------------------------------------------------+