#property indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  Black
#property  indicator_color2  YellowGreen
#property  indicator_color3  Red
#property  indicator_color4  Silver

extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
extern double SigPlus=0.0;
extern double SigMinus=0.0;
extern string str_Price="0-Close;1-Open;2-High;3-Low;4-Median;5-Typical;6-Weighted";
extern int Price=0;
extern string str_Metod="0-SMA;1-EMA;2-SMMA;3-LWMA;";
extern int Metod=1;

double     ExtBuffer0[];
double     ExtBuffer1[];
double     ExtBuffer2[];
double     SugnBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_LINE);
   
   IndicatorDigits(Digits+1);
   SetIndexDrawBegin(0,34);
   SetIndexDrawBegin(1,34);
   SetIndexDrawBegin(2,34);
   SetIndexDrawBegin(3,SignalSMA);
//---- 3 indicator buffers mapping
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,SugnBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD");
   SetIndexLabel(0,NULL);
   SetIndexLabel(3,NULL);
//---- initialization done
//----
   if(Price<0 || Price>6)Price=0;
   if(Metod<0 || Metod>3)Metod=0;
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd
   for(int i=0; i<limit; i++)
      ExtBuffer0[i]=iMA(NULL,0,FastEMA,0,Metod,Price,i)-iMA(NULL,0,SlowEMA,0,Metod,Price,i);

//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ExtBuffer0[i];
      prev=ExtBuffer0[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         ExtBuffer2[i]=current;
         ExtBuffer1[i]=0.0;
        }
      else
        {
         ExtBuffer1[i]=current;
         ExtBuffer2[i]=0.0;
        }
     }
   for(i=0; i<limit; i++)
      SugnBuffer[i]=iMAOnArray(ExtBuffer0,Bars,SignalSMA,0,MODE_SMA,i);
   
   if(SigPlus>0.0)
      {
         if(SigPlus<=ExtBuffer0[0] && SigPlus>=ExtBuffer0[1])
         {
            Alert("Пройден положительный уровень "+SigPlus);
            PlaySound("alarm1.wav");
            SigPlus=0.0;
         }
      }
    if(SigMinus<0.0)
      {
         if(SigMinus>=ExtBuffer0[0] && SigMinus<=ExtBuffer0[1])
         {
            Alert("Пройден отрицательный уровень "+SigMinus);
            PlaySound("alarm1.wav");
            SigMinus=0.0;
         }
      }
   return(0);
  }
//+------------------------------------------------------------------+