//+------------------------------------------------------------------+
//|                                                     2MAAlert.mq4 |
//|                        Индикаторы и советники платно и бесплатно |
//|                                                 xronso@yandex.ru |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
extern bool UseSound = True;
extern string NameFileSound = "alert.wav";
extern bool TypeChart = True;
extern bool ShowMA = true;
extern int History_bars = 300;

extern string info1 = "";
extern string info2 = "MA_method:0-SMA,1-EMA,2-SMMA,3-LWMA";
extern string info3 = "MA_applied price:0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL,6-WEIGHTED";
extern string info4 = "";

extern int MA1_period = 5;
extern int MA1_shift = 0;
extern int MA1_method = 0;
extern int MA1_applied_price = 4;
 
extern int MA2_period = 14;
extern int MA2_shift = 0;
extern int MA2_method = 0;
extern int MA2_applied_price = 4;

double ExtMapBuffer1[];
double ExtMapBuffer2[];

bool first_load = true;

int init()
{
  if (ShowMA) {
      SetIndexStyle(0,DRAW_LINE);
      SetIndexBuffer(0,ExtMapBuffer1);
      SetIndexStyle(1,DRAW_LINE);
      SetIndexBuffer(1,ExtMapBuffer2);
   }
   return(0);
}
  
int deinit()
{
   if (TypeChart==1) Comment ("");
   return(0);
}

bool Crossed (double line1 , double line2 )
{
   static string last_direction = "";
   string current_dirction = "";

   if(line1>line2)current_dirction = "up";
   if(line1<=line2)current_dirction = "down";

   if(current_dirction != last_direction) 
   {
     if (first_load == false) {
      if (UseSound==1) PlaySound(NameFileSound);
      if (TypeChart==1) Comment ("Cross signal "+current_dirction+" at Ask=",Ask,", Bid=",Bid,", Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime())," Symbol=",Symbol()," Period=",Period());
     }
     else {
         first_load = false;
     }
     last_direction = current_dirction;
     return (true);
   }
   else
   {
     return (false);
   }
} 

int start()
  {
   int counted_bars=IndicatorCounted(),pos;
   if (counted_bars>0 && (History_bars >= 50 || History_bars == 0)) counted_bars--;
   else return(-1);
   if (History_bars>=50) pos=History_bars;else pos=Bars-counted_bars;
   while(pos>=0)
     {
         ExtMapBuffer1[pos]= iMA(NULL,0,MA1_period,MA1_shift,MA1_method,MA1_applied_price,pos);
         ExtMapBuffer2[pos]= iMA(NULL,0,MA2_period,MA2_shift,MA2_method,MA2_applied_price,pos);
         pos--;
     }
   Print(Crossed (ExtMapBuffer1[0],ExtMapBuffer2[0]));
   return(0);
}