//+------------------------------------------------------------------+
//|                                                     MA_ALERT.mq4 |
//+------------------------------------------------------------------+
#property copyright "MA_ALERT mod by ReVeR27"
#property version   "1.1"
#property indicator_chart_window
#property indicator_color1 Gold
#property indicator_buffers 1
#property strict

extern string   s0___  = "===== MA Settings ======";
extern int MA_Period = 14;
extern int MA_Shift = 0;
extern ENUM_MA_METHOD MA_Method = 0;
extern ENUM_APPLIED_PRICE AppiledPrice = 0;

extern string   s1___  = "===== Alert ======";
extern bool SendMailON = true;
extern bool AlertON = true;
extern double PriceToMA = 0;

int PrevAlertTime = 0, bar = 0;
double maBuffer[];
string TFPeriod = "";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexBuffer(0,maBuffer);
   
   switch(Period())
   {  
      case 1:
         TFPeriod = "M1"; break;
      case 5:
         TFPeriod = "M5"; break;
      case 15:
         TFPeriod = "M15"; break;
      case 30:
         TFPeriod = "M30"; break;
      case 60:
         TFPeriod = "H1"; break;
      case 240:
         TFPeriod = "H4"; break; 
      case 1440:
         TFPeriod = "D1"; break; 
      case 10080: 
         TFPeriod = "W1"; break;
      case 43200:  
         TFPeriod = "MN"; break;
      default: TFPeriod = "Unknown TF";
   }
   
   if (Digits == 3 || Digits == 5) {
      PriceToMA *= 10;
   }
   
//----
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---- 
   
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[])
  {
  
  
//---- 
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) counted_bars=0;
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   string AlertString = "";
//---- 
   
   for(int i = 0 ;i < limit ;i++)
   {
      maBuffer[i]=iMA(Symbol(),0,MA_Period,MA_Shift,MA_Method,AppiledPrice,i);
   }
   
   double MA_0 = NormalizeDouble(iMA(Symbol(),0,MA_Period,MA_Shift,MA_Method,AppiledPrice,0),Digits);
   double MA_2 = NormalizeDouble(iMA(Symbol(),0,MA_Period,MA_Shift,MA_Method,AppiledPrice,2),Digits);
      
   if (Bars != bar && AlertON) {
      if (PriceToMA > 0) {
         double _bid = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits);
         if ((_bid - MA_0 <= PriceToMA*Point && MA_0 < High[0]) || (MA_0 - _bid <= PriceToMA*Point && MA_0 > Low[0])) {
            AlertString = ": Price near to MA ";
            bar = Bars;
         }
      }
      else {
         
         if (MA_0 > Low[0] && MA_2 < Low[0]) {
            AlertString = ": MA increasing ";
            bar = Bars;
         }
         else if(MA_0 < High[0] && MA_2 > High[0]) {
            AlertString = ": MA decreasing ";
            bar = Bars;
         }
      }   

      if (AlertString != "") {
         if (AlertON) Alert(Symbol() + " " + TFPeriod + AlertString + TimeToString(Time[0]));
         if (SendMailON) SendMail("Notification", "Account " + AccountCompany() + ":" + IntegerToString(AccountNumber()) + "| " + Symbol() + " " + TFPeriod + AlertString + TimeToString(Time[0]));
      }
   }
//----
   return(rates_total);
  }
//+------------------------------------------------------------------+