
#property indicator_chart_window

extern bool AddAlerts = TRUE;
extern int AlertDist = 10;
extern bool AlertsHLine = TRUE;
extern bool AlertsTLine = FALSE;
extern bool AlertsRect = FALSE;
extern bool AlertsFib = FALSE;
extern bool AlertsFibExp = FALSE;
extern bool DisplaySettings = TRUE;
extern bool CommentAlert = FALSE;
extern bool EmailAlert = FALSE;
extern bool PopupAlert = TRUE;
bool gi_120 = FALSE;
int g_time_124 = 0;

int init() {
   createIndicatorLabel("Alerter Label");
   Comment("");
   return (0);
}

int deinit() {
   ObjectDelete("Alerter Label");
   Comment("");
   return (0);
}

int start() {
   bool li_0;
   string l_name_8;
   double ld_16;
   int li_24;
   int li_28;
   string ls_32;
   if (NewBar() && AddAlerts) li_0 = TRUE;
   else li_0 = FALSE;
   for (int li_4 = ObjectsTotal() - 1; li_4 >= 0; li_4--) {
      l_name_8 = ObjectName(li_4);
      ld_16 = getObjectPrice(l_name_8);
      if (ld_16 != -1.0) {
         li_24 = getObjectAlert(l_name_8);
         if (li_24 == -1) {
            if (li_0) addAlertDescription(l_name_8, AlertDist);
         } else {
            li_28 = MathAbs(Bid - ld_16) / Point;
            if (li_28 <= li_24) {
               ls_32 = "Alert triggered: " + Symbol() + "@" + DoubleToStr(Bid, Digits) + "." + "  " + li_28 + " pips from " + l_name_8 + ".";
               if (CommentAlert) Comment(Month(), "/", Day(), " ", Hour(), ":", Minute(), " ", ls_32);
               if (PopupAlert) Alert(ls_32);
               else Print(ls_32);
               if (EmailAlert) SendMail("Alert on " + Symbol(), ls_32);
               clearObjectAlert(l_name_8);
            }
         }
      }
   }
   return (0);
}

double getObjectPrice(string a_name_0) {
   double ld_12;
   double ld_20;
   int li_28;
   int li_32;
   double ld_ret_36;
   double ld_44;
   double ld_52;
   double ld_60;
   double ld_ret_68;
   double ld_76;
   double ld_84;
   string l_str_concat_92;
   double ld_104;
   int li_8 = ObjectType(a_name_0);
   if (AlertsHLine && li_8 == 1) return (ObjectGet(a_name_0, OBJPROP_PRICE1));
   if (AlertsTLine && li_8 == 2) return (ObjectGetValueByShift(a_name_0, 0));
   if (AlertsRect && li_8 == 16) {
      ld_12 = ObjectGet(a_name_0, OBJPROP_PRICE1);
      ld_20 = ObjectGet(a_name_0, OBJPROP_PRICE2);
      li_28 = ObjectGet(a_name_0, OBJPROP_TIME1);
      li_32 = ObjectGet(a_name_0, OBJPROP_TIME2);
      ld_ret_36 = 0;
      if (Time[0] >= li_28 && Time[0] <= li_32) {
         if (MathAbs(Close[0] - ld_12) > MathAbs(Close[0] - ld_20)) ld_ret_36 = ld_20;
         else ld_ret_36 = ld_12;
      }
      if (ld_ret_36 > 0.0) return (ld_ret_36);
   }
   if (AlertsFib && li_8 == 10) {
      ld_44 = ObjectGet(a_name_0, OBJPROP_PRICE1);
      ld_52 = ObjectGet(a_name_0, OBJPROP_PRICE2);
      ld_60 = ObjectGet(a_name_0, OBJPROP_FIBOLEVELS);
      ld_ret_68 = ld_52;
      ld_76 = 0;
      ld_84 = 0;
      l_str_concat_92 = StringConcatenate("100: ", ld_44, " 0: ", ld_52);
      for (int l_count_100 = 0; l_count_100 < ld_60; l_count_100++) {
         ld_84 = ObjectGet(a_name_0, l_count_100 + 210);
         if (ld_84 > 0.0) {
            ld_76 = ld_52 + ld_84 * (ld_44 - ld_52);
            if (MathAbs(Close[0] - ld_ret_68) > MathAbs(Close[0] - ld_76)) ld_ret_68 = ld_76;
            l_str_concat_92 = StringConcatenate(l_str_concat_92, " ", ld_84, ":", ld_76);
         }
      }
      l_str_concat_92 = StringConcatenate(l_str_concat_92, " nearest: ", ld_ret_68);
      if (gi_120) Comment(l_str_concat_92);
      if (ld_ret_68 > 0.0) return (ld_ret_68);
   }
   if (AlertsFibExp && li_8 == 14) {
      ld_44 = ObjectGet(a_name_0, OBJPROP_PRICE1);
      ld_52 = ObjectGet(a_name_0, OBJPROP_PRICE2);
      ld_ret_68 = ObjectGet(a_name_0, OBJPROP_PRICE3);
      ld_60 = ObjectGet(a_name_0, OBJPROP_FIBOLEVELS);
      ld_76 = 0;
      ld_84 = 0;
      ld_104 = 0;
      l_str_concat_92 = StringConcatenate("100: ", ld_44, " 0: ", ld_52, " A-B: ", ld_44 - ld_52, " PB: ", ld_ret_68);
      for (l_count_100 = 0; l_count_100 < ld_60; l_count_100++) {
         ld_84 = ObjectGet(a_name_0, l_count_100 + 210);
         if (ld_84 > 0.0) {
            ld_76 = ld_ret_68 - ld_84 * (ld_44 - ld_52);
            if (MathAbs(Close[0] - ld_104) > MathAbs(Close[0] - ld_76)) ld_104 = ld_76;
            l_str_concat_92 = StringConcatenate(l_str_concat_92, " ", ld_84, ":", ld_76);
         }
      }
      l_str_concat_92 = StringConcatenate(l_str_concat_92, " nearest: ", ld_104);
      if (gi_120) Comment(l_str_concat_92);
      if (ld_ret_68 > 0.0) return (ld_ret_68);
   }
   return (-1);
}

int addAlertDescription(string a_name_0, int ai_8) {
   ObjectSetText(a_name_0, StringConcatenate("Alert_", ai_8));
   return (1);
}

int getObjectAlert(string a_name_0) {
   string ls_8 = ObjectDescription(a_name_0);
   int li_16 = StringFind(ls_8, "Alert_");
   int l_str_len_20 = StringLen("Alert_");
   if (li_16 == -1) return (-1);
   return (StrToInteger(StringSubstr(ls_8, li_16 + l_str_len_20)));
}

void clearObjectAlert(string a_name_0) {
   string ls_8 = ObjectDescription(a_name_0);
   int li_16 = StringFind(ls_8, "Alert_");
   if (li_16 == 0) {
      ObjectSetText(a_name_0, "");
      return;
   }
   if (li_16 > 0) ObjectSetText(a_name_0, StringSubstr(ls_8, 0, li_16));
}

void createIndicatorLabel(string a_name_0) {
   string l_text_8;
   if (DisplaySettings) {
      l_text_8 = "A:";
      if (CommentAlert) l_text_8 = l_text_8 + "C";
      if (EmailAlert) l_text_8 = l_text_8 + "E";
      if (PopupAlert) l_text_8 = l_text_8 + "P";
      if (AddAlerts) l_text_8 = l_text_8 + "+" + AlertDist;
      if (AlertsHLine) l_text_8 = l_text_8 + "H";
      if (AlertsTLine) l_text_8 = l_text_8 + "T";
      if (AlertsRect) l_text_8 = l_text_8 + "R";
      if (AlertsFib) l_text_8 = l_text_8 + "F";
      if (AlertsFibExp) l_text_8 = l_text_8 + "E";
      ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0);
      ObjectSetText(a_name_0, l_text_8, 8, "Arial", Yellow);
      ObjectSet(a_name_0, OBJPROP_XDISTANCE, 5);
      ObjectSet(a_name_0, OBJPROP_YDISTANCE, 5);
      ObjectSet(a_name_0, OBJPROP_CORNER, 1);
   }
}

int NewBar() {
   if (Time[0] != g_time_124) {
      g_time_124 = Time[0];
      return (1);
   }
   return (0);
}
