#property indicator_separate_window
#property indicator_buffers    3
#property indicator_color1     clrLimeGreen
#property indicator_color2     clrOrange
#property indicator_color3     clrGray
#property indicator_width1     2
#property indicator_width2     2
#property indicator_width3     2
#property indicator_levelcolor clrMediumOrchid
#property strict

//
//
//
//
//

extern int                 MaPeriod        = 14;               // Ma period
extern ENUM_MA_METHOD      MaMethod        = MODE_SMA;         // Ma method
extern ENUM_APPLIED_PRICE  MaPrice         = PRICE_CLOSE;      // Ma price to use
extern ENUM_APPLIED_PRICE  Price           = PRICE_CLOSE;      // Price to use
extern double              levelUp         = 5.00;             // Upper level
extern double              levelDn         = -5.00;            // Lower level
extern bool                alertsOn        = true;             // Turn alerts on?
extern bool                alertsOnCurrent = false;            // Alerts on current (still opened) bar?
extern bool                alertsMessage   = true;             // Alerts showing a pop-up message?
extern bool                alertsSound     = false;            // Alerts playing sound?
extern bool                alertsEmail     = false;            // Alerts sending email?
extern bool                alertsNotify    = false;            // Alerts send notification?
extern string              soundFile       = "alert2.wav";     // Alerts Sound File to use


double up[],dn[],diff[],trend[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,up); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,dn); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,diff);
   SetIndexBuffer(3,trend);
   SetLevelValue(0,levelUp);
   SetLevelValue(1,levelDn);
return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         double pipMultiplier = MathPow(10,MathMod(Digits,2));

   //
   //
   //
   //
   //
   //

   for(int i=limit; i >= 0; i--)
   {
      diff[i] = (iMA(NULL,0,1,0,MODE_SMA,Price,i)-iMA(NULL,0,MaPeriod,0,MaMethod,MaPrice,i))/(pipMultiplier*_Point);
      if (diff[i]>0) 
            { up[i] = diff[i]; dn[i] = EMPTY_VALUE; }
      else  { dn[i] = diff[i]; up[i] = EMPTY_VALUE; }
      trend[i] = (diff[i]>levelUp) ? 1 : (diff[i]<levelDn) ? -1 :  0;
   }
   
   //
   //
   //
   //
   //
   
   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0; 
      if (trend[whichBar]!=trend[whichBar+1])
      {
         if (trend[whichBar] ==  1) doAlert(whichBar," crossing upper level");
         if (trend[whichBar] == -1) doAlert(whichBar," crossing lower level");  
      }
   }               
return(0);
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Ma distance ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " Ma distance "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}


