#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Red

extern int    FastMa          = 30;
extern int    FastMaShift     = 0;
extern int    FastMAMethod    = MODE_EMA;
extern int    FastMAPrice     = PRICE_CLOSE;
extern int    SlowMa          = 200;
extern int    SlowMaShift     = 0;
extern int    SlowMAMethod    = MODE_SMA;
extern int    SlowMAPrice     = PRICE_CLOSE;


extern string ahi="******* ALERT SETTINGS:";
extern int    AlertCandle            = 1;
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = true;
extern bool   SoundAlerts            = false;
extern string SoundFileUp            = "alert.wav";
extern string SoundFileDn            = "alert2.wav";

bool alertsOn=true;
/*
extern string note             = "turn on Alert = true; turn off = false";
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsEmail      = false;
extern string soundfile        = "alert2.wav";
*/

extern string note7            = "Arrow Type";
extern string note8            = "0=default,1=Thick,2=Thin,3=Hollow";
extern string note9            = "4=Round,5=Fractal,6=Diagonal Thin";
extern string note10           = "7=Diagonal Thick,8=Diagonal Hollow";
extern string note11           = "9=Thumb,10=Finger";
extern int    ArrowType        = 2;
extern int    arrowthickness   = 2;


//
//
//
//
//

double CrossUp[];
double CrossDn[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(3);   
   
   if (ArrowType == 0) {
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0,DRAW_ARROW,0,arrowthickness); SetIndexArrow(0,119);
   SetIndexBuffer(1, CrossDn );  SetIndexStyle(1,DRAW_ARROW,0,arrowthickness); SetIndexArrow(1,119);
   }
   if (ArrowType == 1) {
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 234);
   }
   else if (ArrowType == 2) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 225);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 226);
   }
   else if (ArrowType == 3) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 241);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 242);
   }
   else if (ArrowType == 4) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 221);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 222);
   }
   else if (ArrowType == 5) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 217);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 218);
   }
   else if (ArrowType == 6) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 228);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 230);
   }
   else if (ArrowType == 7) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 236);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 238);
   }
   else if (ArrowType == 8) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 246);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 248);
   }
   else if (ArrowType == 9) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 67);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 68);
   }
   else if (ArrowType == 10) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 71);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 72);
   }

   SetIndexBuffer(2, trend);
   return(0);
}
int deinit() {  return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int start() {
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         
   //
   //
   //
   //
   //
            
   for(i=limit; i>=0; i--)
   {
      double fastMA = iMA(NULL,0,FastMa,FastMaShift,FastMAMethod,FastMAPrice,i);
      double slowMA = iMA(NULL,0,SlowMa,SlowMaShift,SlowMAMethod,SlowMAPrice,i);
      
      trend[i] = trend[i+1];
         if (fastMA>slowMA) trend[i] = 1;
         if (fastMA<slowMA) trend[i] =-1;

         //
         //
         //
         //
         //
      
         CrossUp[i] = EMPTY_VALUE;
         CrossDn[i] = EMPTY_VALUE;
         if (trend[i] !=trend[i+1])
         if (trend[i] == 1)
               CrossUp[i] = Low[i] - iATR(NULL,0,20,i)/2.0;
         else  CrossDn[i] = High[i]+ iATR(NULL,0,20,i)/2.0;
         }
         
         if (alertsOn) {

          int whichBar=AlertCandle;
          /*if (alertsOnCurrent) int whichBar = 0;
            else  whichBar = 1; */

         if (trend[whichBar] != trend[whichBar+1])
         if (trend[whichBar] == 1) doAlert("uptrend");
         else  doAlert("downtrend");       
   }
   
   return(0);
}
//+------------------------------------------------------------------+


void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," ma cross ",doWhat);
          message = "ma cross "+doWhat+" (at "+TimeToStr(TimeLocal(),TIME_SECONDS)+")";
          if (doWhat=="uptrend") string SoundFile=SoundFileUp;
          if (doWhat=="downtrend") SoundFile=SoundFileDn;
          doAlerts(message,SoundFile);
             /*
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," ma cross "),message);
             if (alertsSound)   PlaySound(soundfile);
             */
      }
}

//////////////////////////////////////////////////////////////////////
void doAlerts(string msg,string SoundFile) {
  msg="ForexprofitSupremeDline Signal on "+Symbol()+", period "+TFtoStr(Period())+": "+msg;
  string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
   if (PopupAlerts) Alert(msg);
   if (EmailAlerts) SendMail(emailsubject,msg);
   if (PushNotificationAlerts) SendNotification(msg);
   if (SoundAlerts) PlaySound(SoundFile);
}//void doAlerts(string msg,string SoundFile) {

string TFtoStr(int period) {
 switch(period) {
  case 1     : return("M1");  break;
  case 5     : return("M5");  break;
  case 15    : return("M15"); break;
  case 30    : return("M30"); break;
  case 60    : return("H1");  break;
  case 240   : return("H4");  break;
  case 1440  : return("D1");  break;
  case 10080 : return("W1");  break;
  case 43200 : return("MN1"); break;
  default    : return(DoubleToStr(period,0));
 }
 return("UNKNOWN");
}//string TFtoStr(int period) {
//////////////////////////////////////////////////////////////////////