//+------------------------------------------------------------------+
//|                                                  HLine Alert.mq4 |
//+------------------------------------------------------------------+
#property copyright "raff1410@o2.pl"
#property indicator_chart_window
#property indicator_buffers 1

//
//
//
extern string             LineName             = "MyLineL";
extern string             LineName1            = "MyLineH";
extern color              LineColor            = clrPink; 
extern color              LineColor1           = clrDodgerBlue; 
extern ENUM_LINE_STYLE    LineStyle            = STYLE_SOLID;
extern ENUM_LINE_STYLE    LineStyle1           = STYLE_SOLID;
extern int                pipDistance          = 50;
input bool                alertsOn             = true;                 // Alerts on true/false?
input bool                alertsOnCurrent      = false;                // Alerts on still opened bar true/false?
input bool                alertsMessage        = true;                 // Alerts pop-up message true/false?
input bool                alertsSound          = false;                // Alerts sound true/false?
input bool                alertsPushNotif      = false;                // Alerts push notification true/false?
input bool                alertsEmail          = false;                // Alerts email true/false?
input string              soundFile            = "alert2.wav"; 

double trend[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  SetIndexBuffer(0,trend);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit(){  ChartRedraw(); return(0);  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = fmin(Bars - counted_bars,Bars-1);
   
   //double pipModifier = 1; if (Digits==3 || Digits==5) pipModifier=10;
   for(int i = limit; i >= 0 ; i--)
   {

      ObjectCreate(LineName, OBJ_HLINE, 0, 0, Bid - pipDistance*_Point);
      ObjectSet(LineName, OBJPROP_STYLE, LineStyle);
      ObjectSet(LineName, OBJPROP_COLOR, LineColor);
      
      ObjectCreate(LineName1, OBJ_HLINE, 0, 0, Bid + pipDistance*_Point);
      ObjectSet(LineName1, OBJPROP_STYLE, LineStyle1);
      ObjectSet(LineName1, OBJPROP_COLOR, LineColor1);
        
      double val  =  ObjectGet( LineName, OBJPROP_PRICE1);
      double val1 =  ObjectGet( LineName1, OBJPROP_PRICE1);
      trend[i] = 0;
      if (Close[i] >= val)  trend[i] = 1;
      if (Close[i] <= val1) trend[i] =-1;
      
      //
      //
      //
   }     
   if (alertsOn)
   {
     int whichBar = (alertsOnCurrent) ? 0 : 1;
     if (trend[whichBar] != trend[whichBar+1])
     {
        if (trend[whichBar] == 1) doAlert(" crossed up");
        if (trend[whichBar] ==-1) doAlert(" crossed down");       
     }         
   }      
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 = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" Laguerre RSI "+doWhat;
             if (alertsMessage)    Alert(message);
             if (alertsPushNotif)  SendNotification(message);
             if (alertsEmail)      SendMail(_Symbol+" Laguerre RSI ",message);
             if (alertsSound)      PlaySound(soundFile);
      }
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}


