//+------------------------------------------------------------------+
//|                                                ZigZag_AleksD.mq4 |
//|                                         Copyright © 2008, AleksD |
//|                                                 http://ideal.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, AleksD"
#property link      "http://ideal.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrMagenta
#property indicator_color2 clrRed
#property indicator_color3 clrBlue

extern int       History=120;
extern int       Speed=6;
extern bool UseSound=true;
extern bool AlertSound=true;
extern string SoundFileBuy ="Пробой_вверх.wav";
extern string SoundFileSell="Пробой_вниз.wav";
extern bool SendMailPossible = false;
extern int SIGNAL_BAR = 1;
bool SoundBuy  = False;
bool SoundSell = False;

double ExtMapBuffer1[],ExtMapBuffer2[],ExtMapBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,2);
   SetIndexArrow(1,108);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(2,0.0);
   SetIndexStyle(2,DRAW_ARROW,EMPTY,2);
   SetIndexArrow(2,108);
   SetIndexBuffer(2,ExtMapBuffer3);
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int Swing,Swing_n,i,zu,zd;
   double LL,HH,BH,BL; 

   for (i=History;i>=0;i--)
   { 
      LL=Low[iLowest(NULL,0,MODE_LOW,Speed,i+1)];
      HH=High[iHighest(NULL,0,MODE_HIGH,Speed,i+1)];
      if (Low[i]<LL && High[i]>HH) { Swing=2; if (Swing_n==1) zu=i+1; if (Swing_n==-1) zd=i+1; }
      else {  if (Low[i]<LL) Swing=-1; if (High[i]>HH) Swing=1; }

      if (Swing!=Swing_n && Swing_n!=0)
      {
         if (Swing==2) { Swing=-Swing_n; BH = High[i]; BL = Low[i];}
         if (Swing==1) {ExtMapBuffer1[zd]=BL;ExtMapBuffer2[i]=Low[i];}
         if (Swing==-1){ExtMapBuffer1[zu]=BH;ExtMapBuffer3[i]=High[i];}
         BH = High[i];
         BL = Low[i]; 
      }
      if (Swing==1)
      {
         if (High[i]>=BH) { BH=High[i]; zu=i; }
      } 
      if (Swing==-1)
      {
         if (Low[i]<=BL) { BL=Low[i]; zd=i; }
      } 
      Swing_n=Swing;
   }
   //+------------------------------------------------------------------+ 
        string  message  =  StringConcatenate("ZigZag_AleksD (", Symbol(), ", ", Period(), ")  -  BUY!!!" ,"-" ,TimeToStr(TimeLocal(),TIME_SECONDS)); 
        string  message2 =  StringConcatenate("ZigZag_AleksD (", Symbol(), ", ", Period(), ")  -  SELL!!!","-"  ,TimeToStr(TimeLocal(),TIME_SECONDS)); 
               
        if (ExtMapBuffer2[SIGNAL_BAR] != EMPTY_VALUE && ExtMapBuffer2[SIGNAL_BAR] != 0 && SoundBuy)
         {
         SoundBuy = False;
            if (UseSound) PlaySound("Пробой_вверх.wav");
               if(AlertSound){         
               Alert(message);                             
               if (SendMailPossible)SendMail(StringConcatenate(Symbol()," super-signals-channel "),message);
            }              
         } 
      if (!SoundBuy && (ExtMapBuffer2[SIGNAL_BAR] == EMPTY_VALUE || ExtMapBuffer2[SIGNAL_BAR] == 0)) SoundBuy = True;  
            
  
     if (ExtMapBuffer3[SIGNAL_BAR] != EMPTY_VALUE && ExtMapBuffer3[SIGNAL_BAR] != 0 && SoundSell)
         {
         SoundSell = False;
            if (UseSound)PlaySound("Пробой_вниз.wav");
             if(AlertSound){                    
             Alert(message2);             
             if (SendMailPossible) SendMail(StringConcatenate(Symbol()," super-signals-channel "),message2);
             }                     
         } 
      if (!SoundSell && (ExtMapBuffer3[SIGNAL_BAR] == EMPTY_VALUE || ExtMapBuffer3[SIGNAL_BAR] == 0)) SoundSell = True; 
      
       //+------------------------------------------------------------------+   
   return(0);
}
//+------------------------------------------------------------------+