//+------------------------------------------------------------------+
//|                                                  SR Alert V3.mq4 |
//+------------------------------------------------------------------+
#property copyright "APinbox@gmail.com"

// Version 3 has a counter that is reset after an alert was given.
// Previous version gave alerts on EACH BAR that met alert criteria.
// If S/R level was breached and price stayed above/below that level, 
// alert would still be given on each new bar.
// With V3, alerts will only be renewed if the price crossed back
// the breached S/R level, and then re-crossed it again.

#property indicator_chart_window

extern string ResistanceLineName="R";
extern string SupportLineName="S";
extern color LineColor=RoyalBlue; 
extern ENUM_LINE_STYLE LineStyle=STYLE_DASHDOTDOT;
extern int AlertPipRange=1;
extern string AlertWav="alert.wav";
extern bool EmailON=false;
bool AlertR = false;
bool AlertS = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();

      ObjectCreate(ResistanceLineName, OBJ_HLINE, 0, 0, Ask);
      ObjectSet(ResistanceLineName, OBJPROP_STYLE, LineStyle);
      ObjectSet(ResistanceLineName, OBJPROP_COLOR, LineColor);
      ObjectSetText(ResistanceLineName, "RESISTANCE Alert",10,"Arial", White);
       
      double val1 =  ObjectGet(ResistanceLineName, OBJPROP_PRICE1);
      if (Bid+AlertPipRange*Point >= val1 && AlertR == false) {
         AlertOnce(Symbol() + " - RESISTANCE hit!", 1);
         AlertR = true;
       if(EmailON){
         EmailOnce (TimeToStr(CurTime(),TIME_MINUTES) + " - " + Symbol() + " - RESISTANCE hit!", 1);
         }
       }
       else if (Bid+AlertPipRange*Point <= val1) {
       AlertR = false;
       }
      
      ObjectCreate(SupportLineName, OBJ_HLINE, 0, 0, Bid);
      ObjectSet(SupportLineName, OBJPROP_STYLE, LineStyle);
      ObjectSet(SupportLineName, OBJPROP_COLOR, LineColor);
      ObjectSetText(SupportLineName, "SUPPORT Alert",10,"Arial", White);
        
      double val2 =  ObjectGet(SupportLineName, OBJPROP_PRICE1);
      if (Bid-AlertPipRange*Point <= val2 && AlertS == false) {
      AlertOnce(Symbol() + " - SUPPORT hit!", 2);
      AlertS = true;
       if(EmailON){
         EmailOnce (TimeToStr(CurTime(),TIME_MINUTES) + " - " + Symbol() + " - SUPPORT hit!", 2);
         }
         AlertS = true;
       }
       else if (Bid+AlertPipRange*Point >= val1) {
       AlertS = false;
       }

//----
//----
   return(0);
  }
//+------------------------------------------------------------------+


bool AlertOnce(string alert_msg, int ref)
{
   static int LastAlert_1 = 0;
   static int LastAlert_2 = 0;
   static int LastAlert_3 = 0;
   static int LastAlert_4 = 0;
   
   switch(ref)
   {
      case 1:
         if( LastAlert_1 == 0 || LastAlert_1 < Bars )
         {
            Alert(alert_msg);
            LastAlert_1 = Bars;
            return (true);
         }
      break;
      case 2:
         if( LastAlert_2 == 0 || LastAlert_2 < Bars )
         {
            Alert(alert_msg);
            LastAlert_2 = Bars;
            return (true);
         }
      break;
      case 3:
         if( LastAlert_3 == 0 || LastAlert_3 < Bars )
         {
            Alert(alert_msg);
            LastAlert_3 = Bars;
            return (true);
         }
      break;
      case 4:
         if( LastAlert_4 == 0 || LastAlert_4 < Bars )
         {
            Alert(alert_msg);
            LastAlert_4 = Bars;
            return (true);
         }
      break;
   }
            return (false);
}

bool EmailOnce(string email_msg, int ref)
{
   static int LastEmail_1 = 0;
   static int LastEmail_2 = 0;
   static int LastEmail_3 = 0;
   static int LastEmail_4 = 0;

   switch(ref)
   {
      case 1:
         if( LastEmail_1 == 0 || LastEmail_1 < Bars )
         {
            SendMail(email_msg,email_msg);
            LastEmail_1 = Bars;
            return (true);
         }
      break;
      case 2:
         if( LastEmail_2 == 0 || LastEmail_2 < Bars )
         {
            SendMail(email_msg,email_msg);
            LastEmail_2 = Bars;
            return (true);
         }
      break;
      case 3:
         if( LastEmail_3 == 0 || LastEmail_3 < Bars )
         {
            SendMail(email_msg,email_msg);
            LastEmail_3 = Bars;
            return (true);
         }
      break;
      case 4:
         if( LastEmail_4 == 0 || LastEmail_4 < Bars )
         {
            SendMail(email_msg,email_msg);
            LastEmail_4 = Bars;
            return (true);
         }
      break;
   }
            return (false);
}