//+------------------------------------------------------------------+
//|                                                  HO LO alert.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window




// === Section 1: paste this code directly BELOW the final 'extern' statement ================================================================= //
                                                                                                                                                //
extern int     AlertCandle         = 0;
extern bool    ShowChartAlerts     = true;
extern bool    AvoidCurrentHourHighLow = true;                                                                                                     //
extern string  AlertTextCrossUp    = "HO Setup"; 
extern string  AlertTextCrossDown  = "LO Setup";
extern string  AlertEmailSubject   = "";
extern string  AlertNotificationText   = "";                                                                                                        
                                                                                                                                                //
datetime       LastAlertTime       = -999999;                                                                                                   //
                                                                                                                                                //
                                                                                                                                                //
// ============================================================================================================================================ //

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
// === Section 2: paste this code just BEFORE the final 'return(0)' statement in the start() module =========================================== //
                                                                                                                                                //
  ProcessAlerts();                                                                                                                              //
                                                                                                                                                //
// ============================================================================================================================================ //  
  return(0);
}
//+------------------------------------------------------------------+
// === Section 3: paste this code at the end of the indicator ================================================================================= //

                                                                                                                                         //

//+------------------------------------------------------------------+                                                                          //
int ProcessAlerts()   {                                                                                                                         //
//+------------------------------------------------------------------+                                                                          //
  int x;
  int CurrentHOpenIndex, CurrentLOpenIndex;
  float CurrentHOpen, CurrentLOpen, HOD, LOD, LastLOpen, LastHOpen, HODYesterday, LODYesterday, HCurrentHour, LCurrentHour;
  bool isCurrentHourHigh, isCurrentHourLow;
    
  if (AlertCandle >= 0  &&  Time[0] > LastAlertTime && Hour()>0)  {     
  
  CurrentHOpenIndex = iHighest(NULL,60,MODE_OPEN,Hour()+1,0);
  CurrentHOpen = iOpen(NULL,60,CurrentHOpenIndex);                                                                            //
  CurrentLOpenIndex = iLowest(NULL,60,MODE_OPEN,Hour()+1,0);
  CurrentLOpen = iOpen(NULL,60,CurrentLOpenIndex);   
  HOD = iHigh(NULL,1440,0);
  LOD = iLow(NULL,1440,0);
  HODYesterday = iHigh(NULL,1440,1);
  LODYesterday = iLow(NULL,1440,1);
  HCurrentHour = iHigh(NULL,60,0);
  LCurrentHour = iLow(NULL,60,0);
  
  isCurrentHourHigh = false;
  isCurrentHourLow = false;

   if (AvoidCurrentHourHighLow)
      {
      isCurrentHourHigh = (HCurrentHour ==  HOD);
      isCurrentHourLow = (LCurrentHour == LOD);
      }
       
      
// Alert High 
    if (
         (Open[0] > CurrentHOpen) && (Open[0] < HOD) && !isCurrentHourHigh
                  
      )
    
    {
      string AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossUp + " at " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);                                                          //
      if (ShowChartAlerts)          Alert(AlertText);                                                                                           //
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText);    
      if (AlertNotificationText > "")   SendNotification(AlertNotificationText+" "+AlertText);                                                                     //
      LastAlertTime = Time[0];       
      LastHOpen = CurrentHOpen;                                                                                                           //
    }                                                                                                                                           //
                                                                                                                                                //
    // === Alert  Low   //
    if(
    
          (Open[0] < CurrentLOpen) && (Open[0] > LOD) && !isCurrentHourLow
    )
    {
      AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossDown + " at " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES) ;                                                                //
      if (ShowChartAlerts)          Alert(AlertText);                                                                                           //
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText); 
      if (AlertNotificationText > "")   SendNotification(AlertNotificationText+" "+AlertText);                                                                        //
      LastAlertTime = Time[0];   
      LastLOpen = CurrentLOpen;                                                                                                               //
    }                                                                                                                                           //
                                                                                                                                                //
  }                                                                                                                                             //
  return(0);                                                                                                                                    //
}                                                                                                                                               //
                                                                                                                                                //
//+------------------------------------------------------------------+                                                                          //
string TFToStr(int tf)   {                                                                                                                      //
//+------------------------------------------------------------------+                                                                          //
  if (tf == 0)        tf = Period();                                                                                                            //
  if (tf >= 43200)    return("MN");                                                                                                             //
  if (tf >= 10080)    return("W1");                                                                                                             //
  if (tf >=  1440)    return("D1");                                                                                                             //
  if (tf >=   240)    return("H4");                                                                                                             //
  if (tf >=    60)    return("H1");                                                                                                             //
  if (tf >=    30)    return("M30");                                                                                                            //
  if (tf >=    15)    return("M15");                                                                                                            //
  if (tf >=     5)    return("M5");                                                                                                             //
  if (tf >=     1)    return("M1");                                                                                                             //
  return("");                                                                                                                                   //
}                                                                                                                                               //
// ============================================================================================================================================ //