//+------------------------------------------------------------------+
//|                                                     AlertSMA.mq4 |
//|                                                 Marcin Konieczny |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Marcin Konieczny"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_width1 3
#property indicator_color2 Lime
#property indicator_width2 2
#property indicator_color3 Red
#property indicator_width3 2

extern int Arrow_otstup = 10;
extern int       MA_Period=10;
extern int       MA_Shift=0;
extern int       MA_Type=0;
extern string    types="0-SMA,1-EMA,2-SMMA,3-LWMA";
extern bool      Mail=true;
extern bool      Alert_Window=false;

double ma[];
double up[], down[];
datetime lastAlert;

string name;

int init(){
   lastAlert = 0;
   if(MA_Shift < 0) MA_Shift = 0;
   if(MA_Period <= 0) MA_Period = 10;
   if(MA_Type < 0 || MA_Type > 3) MA_Type = 0;
   
   switch(MA_Type){
      case 0: name = "SMA("; break;
      case 1: name = "EMA("; break;
      case 2: name = "SMMA("; break;
      case 3: name = "LWMA("; break;
   }
   
   name = name + MA_Period + "," + MA_Shift + ")";
   
   SetIndexBuffer(0,ma);
   SetIndexDrawBegin(0,MA_Period+MA_Shift);
   SetIndexLabel(0, name);
   
   SetIndexBuffer(1,up);
   SetIndexDrawBegin(1,MA_Period+MA_Shift);
   SetIndexLabel(1, "up");
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,233);
   
   SetIndexBuffer(2,down);
   SetIndexDrawBegin(2,MA_Period+MA_Shift);
   SetIndexLabel(2, "down");
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,234);
   
   return(0);
}

int start(){
   int counted = IndicatorCounted();
   int start;
 
   
   if(counted < 0) return(0);
   if(counted == 0) start = Bars - 1;
   if(counted > 0) start = Bars - counted;
   
   for(int i=start; i>=0; i--){
      ma[i] = iMA(Symbol(),0,MA_Period,MA_Shift,MA_Type,PRICE_CLOSE,i);
      
      if(i > 0 && Close[i] > ma[i] && Close[i+1] <= ma[i+1])
         up[i] = Low[i] - Arrow_otstup * Point;
         
      if(i > 0 && Close[i] < ma[i] && Close[i+1] >= ma[i+1])
         down[i] = High[i] + Arrow_otstup * Point;     
   }
   
   if(lastAlert == 0) lastAlert = Time[0];
   
   if(up[1] != EMPTY_VALUE && Time[0] > lastAlert){
      SendAlert(true);
   }
   
   if(down[1] != EMPTY_VALUE && Time[0] > lastAlert){
      SendAlert(false);
   }
   
   lastAlert = Time[0];
   return(0);
}

void SendAlert(bool up){
   string txt = Symbol() + " " + TFtoString(Period()) + ": " + name + " cross ";
   
   if(up)
      txt = txt + "UP";
   else
      txt = txt + "DOWN";

   if(Mail)
      SendMail(txt, txt);
   if(Alert_Window)
      Alert(txt);
}

string TFtoString(int period){
    string tf = "";

    switch(period){
      case 1: tf = "M1"; break;
      case 5: tf = "M5"; break;
      case 15: tf = "M15"; break;
      case 30: tf = "M30"; break;
      case 60: tf = "H1"; break;
      case 240: tf = "H4"; break;
      case 1440: tf = "D1"; break;
      case 10080: tf = "W1"; break;
      case 43200: tf = "MN1"; break;
      default: tf = "";
    }

    return(tf);
}