int ticket, itmp=0, maxcand;        bool t_O240=false;      //, Per_240    , t_Sent=false

int OnInit() { string Symb=StringFind(Symbol(), ".")? StringSubstr(Symbol(), 0, StringFind(Symbol(), ".")): Symbol();
   Comment("** Entry_Points_Pro Ind - en cours **");
   maxcand = Symb == "XAUUSD" || Symb == "USDJPY"? 500: Symb == "US30"? 3000: Symb == "FRA40"? 1000: Symb == "USDJPY"? 150: 200;
   return(INIT_SUCCEEDED); }

int start() { bool tst=false;       string temp;
      if (GlobalVariableCheck(Symbol() + "_Success_240")) itmp = GlobalVariableGet(Symbol() + "_Success_240");
      if (! GlobalVariableCheck(Symbol() + "_M5_Buy") && ! GlobalVariableCheck(Symbol() + "_M5_Sell") && itmp >= 72) M5_Signals();

/*      t_Sent = OrdersTotal();       if (t_Sent && ! ticket) { ticket = GlobalVariableGet(Symbol() + "_ticket_240");
         if (ticket > 0) Per_240 = GlobalVariableGet(Symbol() + "_Period"); }          t_Sent = ticket; */

      if (! t_O240 && GlobalVariableCheck(Symbol() + "_Order_240")) t_O240 = GlobalVariableGet(Symbol() + "_Order_240");
//         if (t_O240) Per_240 = GlobalVariableGet(Symbol() + "_Period"); }              if (! Per_240) Per_240 = 1;

      if (((Period() < 30 && (Minute() < 57 || Hour() < 23 || DayOfWeek() < 5) && (Hour() > 3 || DayOfWeek() > 1)) &&
      ((iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", t_O240, 2, 0) > 0.0 && t_O240) ||
      (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", t_O240, 2, 0) > 0.0 && ! t_O240)) && itmp >= 72) &&
      ! GlobalVariableGet(Symbol() + "_M5_Buy")) { GlobalVariableSet(Symbol() + (temp="_M5_Buy"), 1);    GlobalVars(temp, 1, 1);
         GlobalVariableSet(Symbol() + (temp="_M5_tim1"), TimeCurrent());      GlobalVars(temp, TimeCurrent(), 1);
         temp = "*** BUY M5! - " + Symbol() + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";
/*if (! (Seconds() % 20)) Alert(temp);*/            copy_file(temp); }

      if (((Period() < 30 && (Minute() < 57 || Hour() < 23 || DayOfWeek() < 5) && (Hour() > 3 || DayOfWeek() > 1)) &&
      ((iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", t_O240, 3, 0) > 0.0 && t_O240) ||
      (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", t_O240, 3, 0) > 0.0 && ! t_O240)) && itmp >= 72) &&
      ! GlobalVariableGet(Symbol() + "_M5_Sell")) { GlobalVariableSet(Symbol()+(temp="_M5_Sell"), 1);    GlobalVars(temp, 1, 2);
         GlobalVariableSet(Symbol() + (temp="_M5_tim2"), TimeCurrent());      GlobalVars(temp, TimeCurrent(), 2);
         temp = "*** SELL M5! - " + Symbol() + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";
/*if (! (Seconds() % 20)) Alert(temp);*/            copy_file(temp); }

      if (((Period() >= 30 && (Minute() < 57 || Hour() < 23 || DayOfWeek() < 5) && (Hour() > 3 || DayOfWeek() > 1)) &&
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", true, 2, 0) > 0.0 && itmp >= 72) &&
      ! GlobalVariableGet(Symbol() + "_H4_Buy")) { t_O240 = true;
         GlobalVariableSet(Symbol() + (temp="_Order_240"), 1);          GlobalVars(temp, 1, 3);
         GlobalVariableSet(Symbol() + (temp="_H4_Buy"), 1);             GlobalVars(temp, 1, 3);
         GlobalVariableSet(Symbol() + (temp="_H4_tim1"), TimeCurrent());      GlobalVars(temp, TimeCurrent(), 3);
         temp = "*** BUY H4! - " + Symbol() + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";
if (! (Seconds() % 20)) Alert(temp);            copy_file(temp); }

      if (((Period() >= 30 && (Minute() < 57 || Hour() < 23 || DayOfWeek() < 5) && (Hour() > 3 || DayOfWeek() > 1)) &&
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", true, 3, 0) > 0.0 && itmp >= 72) &&
      ! GlobalVariableGet(Symbol() + "_H4_Sell")) { t_O240 = true;
         GlobalVariableSet(Symbol() + (temp="_Order_240"), 1);          GlobalVars(temp, 1, 4);
         GlobalVariableSet(Symbol() + (temp="_H4_Sell"), 1);            GlobalVars(temp, 1, 4);
         GlobalVariableSet(Symbol() + (temp="_H4_tim2"), TimeCurrent());      GlobalVars(temp, TimeCurrent(), 4);
         temp = "*** SELL H4! - " + Symbol() + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";
if (! (Seconds() % 20)) Alert(temp);            copy_file(temp); }

      datetime tim1=Period() < 30? fmax(GlobalVariableGet(Symbol() + "_M5_tim1"), GlobalVariableGet(Symbol() + "_M5_tim2")):
         fmax(GlobalVariableGet(Symbol() + "_H4_tim1"), GlobalVariableGet(Symbol() + "_H4_tim2"));
// dnTrend GBPJPY.i240 2022.12.08 08:00 O
      for (int pos=ObjectsTotal()-1; pos >= 0 && ! tst; pos --) {
         datetime OTim1/*=0, OTim2=0*/;       string name = ObjectName(pos);         OTim1 = ObjectGet(name, OBJPROP_TIME1);
/*         if (! OTim2) OTim2 = OTim1;      if (OTim1 > OTim2) OTim2 = OTim1;*/          int type=ObjectType(name);
         tst = (StringSubstr(name, 0, 7) == "dnTrend" || StringSubstr(name, 0, 7) == "upTrend") &&// StringSubstr(name, 8, StringLen(Symbol())) == Symbol() &&
            type == OBJ_ARROW && floor(OTim1 / (60 * Period())) > floor(tim1 / (60 * Period())) && tim1 > 0;
         if (StringSubstr(name, 2, 5) == "Trend" && fabs(floor(OTim1 / (60 * Period())) - floor(tim1 / (60 * Period()))) < 120 && type == OBJ_ARROW &&
         TimeCurrent() % 300 > 298) { temp = "*** --Trend! x - " + name + " - " + TimeToStr(OTim1, TIME_MINUTES) + " - " + TimeToStr(tim1, TIME_MINUTES) + " ***";
            copy_file(temp); } }       // && floor(TimeCurrent() / (60 * Period())) - floor(OTim1 / (60 * Period())) < 720 / Period();
      if (tst && ! OrdersTotal()) tst = false;
      if (tst) { temp = "*** --Trend! (2) - " + name + " - " + TimeToStr(OTim1, TIME_MINUTES) + " - " + /*TimeToStr(OTim2, TIME_MINUTES) + " - " + */TimeToStr(tim1, TIME_MINUTES) + " ***";
         if (AccountInfoInteger(ACCOUNT_TRADE_ALLOWED)) Alert(temp);        copy_file(temp); }

      if (((! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, f_wBars(tim1, TimeCurrent())) ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0) > 0.0 ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 1) > 0.0 ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 3, 0) > 0.0 ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0) > 0.0 ||
      ! tim1 || tst) && (GlobalVariableGet(Symbol() + "_M5_Buy") || GlobalVariableGet(Symbol() + "_H4_Buy")))) {
         if (Period() < 30) { if (GlobalVariableGet(Symbol() + "_M5_Buy")) {
            GlobalVariableSet(Symbol() + (temp="_M5_Buy"), 0);             GlobalVars(temp, 0, 5);
            GlobalVariableSet(Symbol() + (temp="_M5_Previous"), 1);        GlobalVars(temp, 1, 5);
            GlobalVariableSet(Symbol() + "_M5_tim1_", GlobalVariableGet(Symbol() + "_M5_tim1"));
            GlobalVariableSet(Symbol() + (temp="_M5_tim1"), 0);            GlobalVars(temp, 0, 5);
            temp = "*** M5_Buy = false;! - " + Symbol() + " - " + IntegerToString(Period()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "",
               "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, f_wBars(tim1, TimeCurrent())), Digits()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4",
               "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0), Digits()) + " - " + DoubleToStr(iCustom(NULL, 0,
               "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 1), Digits()) + " - " +
               DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 3, 0),
               Digits()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0), Digits()) + " - " + IntegerToString(f_wBars(tim1,
               TimeCurrent())) + " - " + TimeToStr(tim1, TIME_MINUTES) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";         copy_file(temp); } }
         else if (GlobalVariableGet(Symbol() + "_H4_Buy")) { GlobalVariableSet(Symbol() + (temp="_H4_Buy"), 0);            GlobalVars(temp, 0, 6);
            GlobalVariableSet(Symbol() + (temp="_H4_tim1"), 0);               GlobalVars(temp, 0, 6);
            t_O240 = false;      GlobalVariableSet(Symbol() + (temp="_Order_240"), 0);       GlobalVars(temp, 0, 6); } }
      if (((! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 3, f_wBars(tim1, TimeCurrent())) ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0) > 0.0 ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 1) > 0.0 ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, 0) > 0.0 ||
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0) > 0.0 ||
      ! tim1 || tst) && (GlobalVariableGet(Symbol() + "_M5_Sell") || GlobalVariableGet(Symbol() + "_H4_Sell")))) {
         if (Period() < 30) { if (GlobalVariableGet(Symbol() + "_M5_Sell")) {
            GlobalVariableSet(Symbol() + (temp="_M5_Sell"), 0);            GlobalVars(temp, 0, 7);
            GlobalVariableSet(Symbol() + (temp="_M5_Previous"), 2);        GlobalVars(temp, 2, 7);
            GlobalVariableSet(Symbol() + "_M5_tim2_", GlobalVariableGet(Symbol() + "_M5_tim2"));
            GlobalVariableSet(Symbol() + (temp="_M5_tim2"), 0);            GlobalVars(temp, 0, 7);
            temp = "*** M5_Sell = false;! - " + Symbol() + " - " + IntegerToString(Period()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "",
               "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 3, f_wBars(tim1, TimeCurrent())), Digits()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4",
               "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0), Digits()) + " - " + DoubleToStr(iCustom(NULL, 0,
               "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 1), Digits()) + " - " +
               DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, 0),
               Digits()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0), Digits()) + " - " + IntegerToString(f_wBars(tim1,
               TimeCurrent())) + " - " + TimeToStr(tim1, TIME_MINUTES) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";         copy_file(temp); } }
         else if (GlobalVariableGet(Symbol() + "_H4_Sell")) { GlobalVariableSet(Symbol() + (temp="_H4_Sell"), 0);          GlobalVars(temp, 0, 8);
            GlobalVariableSet(Symbol() + (temp="_H4_tim2"), 0);               GlobalVars(temp, 0, 8);
            t_O240 = false;      GlobalVariableSet(Symbol() + (temp="_Order_240"), 0);       GlobalVars(temp, 0, 8); } }

   return(0); }

void OnDeinit(const int reason) { Comment(""); }

int f_wBars(datetime Tim1,datetime Tim2) { int wbars=0;
   if (DayOfWeek() < TimeDayOfWeek(Tim1)) wbars = 60 * 48 / Period();
   if (floor(Tim2/(60*Period())) - floor(Tim1/(60*Period())) > 60 * 168 / Period())
      wbars = 2 * 60 * 48 / Period();
   return(floor(Tim2/(60*Period())) - floor(Tim1/(60*Period())) - wbars); }

void M5_Signals() { bool tst=false;       string temp;
   for (int pos=0; pos < 200 && ! tst; pos ++) {
      if (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, pos) > 0.0) { tst = true;
         GlobalVariableSet(Symbol() + (temp="_M5_Buy"), 1);             GlobalVars(temp, 1, 9);
         GlobalVariableSet(Symbol() + (temp="_M5_tim1"), Time[pos]);    GlobalVars(temp, Time[pos], 9); }

      if (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 3, pos) > 0.0) { tst = true;
         GlobalVariableSet(Symbol() + (temp="_M5_Sell"), 1);            GlobalVars(temp, 1, 10);
         GlobalVariableSet(Symbol() + (temp="_M5_tim2"), Time[pos]);    GlobalVars(temp, Time[pos], 10); } }
 }

int copy_file(string tmp) { int handle, it=0;
   MqlDateTime t;    TimeLocal(t);     bool tst=false;
   string Symb=StringFind(Symbol(), ".")? StringSubstr(Symbol(), 0, StringFind(Symbol(), ".")): Symbol(),
      filename="Entry_Points_Pro_file_" + Symb + ".log";
   while (! tst && it < 7) { if (! FileIsExist(filename)) handle = FileOpen (filename, FILE_TXT|FILE_WRITE);
      else  handle = FileOpen (filename, FILE_TXT|FILE_READ|FILE_WRITE);
      if (handle != INVALID_HANDLE) { if (FileSize(handle)) FileSeek(handle, FileSize(handle), 0);
         tst = FileWrite(handle, TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " | " + tmp);
         if (! tst) Print ("Problème : FileWrite(handle, ("+it+" - "+IntegerToString(GetLastError())+") / "+Symbol()+" ***"); }
      else Print ("Problème : handle == INVALID_HANDLE. ("+it+" - "+IntegerToString(GetLastError())+") / "+Symbol()+" ***");
   FileClose(handle);      it ++; }
   return(handle); }

void GlobalVars(string temp,ulong utmp,int item) {
   temp = "*** GlobalVariableSet("+IntegerToString(Period())+"-"+IntegerToString(item)+")! - " + Symbol() + temp + " - " + utmp + " - " + IntegerToString(Period()) + " - " +
      TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";         // Print(())
   copy_file(temp); }
