#property copyright "fxdaytrader"
#property link ""

#property indicator_chart_window
extern string ahi0="true:will create moveable trendline at LinePrice";
extern string ahi1="false:create the line name as LineName by yourself";
extern bool   CreateLine = TRUE;
extern double LinePrice  = 1.38000;
extern string LineName   = "AlertLine";
extern color  LineColor  = AliceBlue; 
extern int    LineStyle  = STYLE_SOLID;
extern bool   DeleteTrendLineOnExit = TRUE;

extern string ashi="******* Alert settings:";
extern double AlertPipRange = 5.0;
extern bool   DeleteLineAfterAlert   = TRUE;
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFileName          = "alert.wav";
       bool   ShowScreenComment      = TRUE;
int Multiplier; double pips2dbl,lastAlertPrice;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  BrokerDigitAdjust(Symbol());
  
  if (CreateLine) {  
   ObjectCreate(LineName, OBJ_HLINE, 0, 0, LinePrice);//WindowPriceMax()-3*pips2dbl);
   ObjectSet(LineName, OBJPROP_STYLE, LineStyle);
   ObjectSet(LineName, OBJPROP_COLOR, LineColor);
  }

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  if (ShowScreenComment) Comment("");
  if (DeleteTrendLineOnExit) ObjectDelete(LineName);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //int    counted_bars=IndicatorCounted();

      /*
      ObjectCreate(LineName, OBJ_HLINE, 0, 0, Bid);
      ObjectSet(LineName, OBJPROP_STYLE, LineStyle);
      ObjectSet(LineName, OBJPROP_COLOR, LineColor);

      */
      string sLineStatus = "status: NO TRENDLINE FOUND (name: "+LineName+")";
      if (ObjectFind(LineName)>-1) {
       double val =  ObjectGet(LineName, OBJPROP_PRICE1);
       sLineStatus = "status: TRENDLINE FOUND (name: "+LineName+") at "+DoubleToStr(val,Digits);
       if (Bid!=lastAlertPrice) if (Bid-AlertPipRange*pips2dbl <= val && Bid+AlertPipRange*pips2dbl >= val) {
        lastAlertPrice=Bid; doAlerts("Trendline ("+LineName+") cross @ "+DoubleToStr(Bid,Digits)+", servertime: "+TimeToStr(TimeCurrent()),SoundFileName);
        if (DeleteLineAfterAlert) ObjectDelete(LineName);
       }
      }
      if (ShowScreenComment) Comment("HLine Alert "+sLineStatus);
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+

void BrokerDigitAdjust(string symbol) {
 Multiplier = 1;
 if (MarketInfo(symbol,MODE_DIGITS) == 3 || MarketInfo(symbol,MODE_DIGITS) == 5) Multiplier = 10;
 if (MarketInfo(symbol,MODE_DIGITS) == 6) Multiplier = 100;   
 if (MarketInfo(symbol,MODE_DIGITS) == 7) Multiplier = 1000;
 pips2dbl = Multiplier*MarketInfo(symbol,MODE_POINT);
 //Slippage*=Multiplier;
}

void doAlerts(string msg,string SoundFile) {
        msg="HLine Alert mod. Alert 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) {
 if (period==0) period=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) {