//+------------------------------------------------------------------+
//|                                       Ma_Distance_From_Price.mq4 |
//|                              transport_david , David W Honeywell |
//|                                        transport.david@gmail.com |
//|                                            this version : mladen |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrRed
#property indicator_color2 clrLime
#property indicator_width1 1
#property indicator_width2 1

//
//
//
//
//

extern int                MaPeriod           =   9;  // default = 9,0,3,0,15 on H1 charts
extern int                MaShift            =   0;
extern ENUM_MA_METHOD     MaMethod           =   3;
extern ENUM_APPLIED_PRICE AppliedPrice       =   0;
extern double             PipBuffer          = 1.5; // If Ma > Price+PipBuffer then arrow , If Ma < Price-PipBuffer then arrow
extern bool               alertsOn           = true;
extern bool               alertsOnCurrent    = true;
extern bool               alertsMessage      = true;
extern bool               alertsSound        = false;
extern bool               alertsEmail        = false;
extern bool               alertsNotification = false;

double ExtMapBuffer1[];
double ExtMapBuffer2[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   SetIndexStyle(0,DRAW_ARROW,0,1); SetIndexArrow(0,226); SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_ARROW,0,1); SetIndexArrow(1,225); SetIndexBuffer(1,ExtMapBuffer2);
   return(0);
}
int deinit()
{
   return(0);
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         double pipMul = MathPow(10,_Digits%2);
         int limit = MathMin(Bars-counted_bars,Bars-1);

   for (int i=limit; i>=0; i--)
   {
      double MaCurrent = iMA(NULL,0,MaPeriod,MaShift,MaMethod,AppliedPrice,i);
         ExtMapBuffer1[i] = EMPTY_VALUE;
         ExtMapBuffer2[i] = EMPTY_VALUE;
         if ( MaCurrent > ( High[i+1] + PipBuffer*Point*pipMul/10)) ExtMapBuffer1[i+1] = High[i+1]+44*Point;
         if ( MaCurrent < ( Low[i+1]  - PipBuffer*Point*pipMul/10)) ExtMapBuffer2[i+1] = Low[i+1] -44*Point;
   }
   manageAlerts();
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (ExtMapBuffer1[whichBar] != EMPTY_VALUE) doAlert(whichBar,"DOWN");
      if (ExtMapBuffer2[whichBar] != EMPTY_VALUE) doAlert(whichBar,"UP");
   }
}   

//
//
//
//
//

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 =  Symbol()+" M"+Period()+"-"+TimeToStr(TimeLocal(),TIME_SECONDS)+ "->> SIGNAL "+doWhat;
          if (alertsMessage)      Alert(message);
          if (alertsEmail)        SendMail(Symbol()+"ma distance from price ",message);
          if (alertsNotification) SendNotification(message);
          if (alertsSound)        PlaySound("alert2.wav");
   }
}