//+------------------------------------------------------------------+
//|                                       Reversal Bar and Alert.mq4 |
//|                                  Copyright © 2022 aandragon.com. |
//|                                         http://www.aandragon.com |
//+------------------------------------------------------------------+
#property copyright "aandragon"
#property link "http://aandragon.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Violet

extern string ahi="******* ALERT SETTINGS:";
extern bool   Use_BullReversal      = true;
extern bool   Use_BearReversal      = true;
extern int    AlertCandle            = 0;//0:current, 1:last bar, etc.
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFileLong          = "alert.wav";
extern string SoundFileShort         = "alert2.wav";
int lastAlert=3;

//---- buffers
double Up[],Dn[];
double cl2,op2,cl1,op1;

//+------------------------------------------------------------------+

int init()
  {
   SetIndexBuffer(0,Up);
   SetIndexBuffer(1,Dn);
   
   if(Use_BullReversal){SetIndexStyle(0,DRAW_ARROW);}else{SetIndexStyle(0,DRAW_NONE);}
   if(Use_BearReversal){SetIndexStyle(1,DRAW_ARROW);}else{SetIndexStyle(1,DRAW_NONE);}
    
   SetIndexArrow(0,233);
   SetIndexArrow(1,234);
   
   
   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+

int start() {
   int i, counter;
   double Range, AvgRange;
   int CountBars=Bars;

   for(i=0; i<CountBars; i++)
   
   
   {
      counter=i;
      Range=0;
      AvgRange=0;
      for (counter=i ;counter<=i+2;counter++)
      {
       AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10; //placement of arrow
      
      cl1=Close[i+1];
      cl2=Close[i+2];
      op1=Open[i+1]; 
      op2=Open[i+2];
       
//  bullish reversal

	     if(Use_BullReversal){
	     if (cl1<cl2)
           {
          	Up[i]=Low[i+1]-Range*1.5;
          	
     	      if (i==AlertCandle) if (lastAlert!=2) { doAlerts("BULLISH",SoundFileLong); lastAlert=2; }          	
           }}
		                                                           
// bearish reversal

        if(Use_BearReversal){
        if (cl1>cl2)
           {                               
		      Dn[i]=High[i+1]+Range*1.5;
		      if (i==AlertCandle) if (lastAlert!=1) { doAlerts("BEARISH",SoundFileShort); lastAlert=1; }
		     }} 		          
           
   }
   return(0);
}

//----------------
void doAlerts(string msg,string SoundFile) {
        msg="Bullish Bar Alert on "+Symbol()+", period "+TFtoStr(Period())+": "+msg+" Reversal possible.";
 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);

}
//----------------
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");
}
