int ticket, ticket2, itmp_, itmp=0, maxcand, orderMagicNum;       datetime tim1, tim3, tmp_saved;
bool Bull, t_Sent=false, t_O240=false, t_Wait=false, t_Time=false, t_Pass;

int OnInit() { string Symb=StringFind(Symbol(), ".")? StringSubstr(Symbol(), 0, StringFind(Symbol(), ".")): Symbol();
   maxcand = Period() < 30? Symb == "XAUUSD" || Symb == "USDJPY"? 500: Symb == "US30"? 3000: Symb == "FRA40"? 1000: 200:
      Symb == "US30"? 15000: Symb == "USOUSD" || Symb == "FRA40"? 5000: 1000;                      t_Pass = false;
   if (! OrdersTotal()) { ticket = 0;     tim1 = 0; }       ArrayResize(line_read, 12, 0);         return(INIT_SUCCEEDED); }

void OnTick() { string temp, name, comm;     datetime OTim1;      bool tst=false, t_H4_M5=false;      itmp_ = itmp;     f_Order_240();
   if (ObjectsTotal() && (! itmp_ || ((myHour() == 1 || (myHour() == 13 && Period() < 240) || Period() < 15) && ! Minute() && ! Seconds())))
      for (int pos=0; pos < ObjectsTotal() && ! tst; pos ++) { name = ObjectName(pos);
         tst = name == "eFess_ Success rate";         temp = ObjectGetString(0, name, OBJPROP_TEXT);
         itmp = tst && StringSubstr(temp, StringLen(temp) - 1) == "%"? StrToInteger(StringSubstr(temp, StringLen(temp) - 4, 3)): 0; }
   if ((! itmp_ || itmp_ != itmp) && tst && itmp) { Print("*** eFess_ Success rate! - " + name + " - " + temp + " - " + IntegerToString(itmp) + " ***");
      GlobalVariableSet(Symbol() + (temp="_Success_240"), itmp);        GlobalVars(temp, itmp, 1); }

   int signs, wins, loss;     if (ObjectsTotal()) for (pos = 0; pos < ObjectsTotal(); pos ++) { name = ObjectName(pos);
         if (name == "eFess_ --------" || name == "eFess_ Cancelled" || name == "eFess_ Min profit" ||
         name == "eFess_ Signals" || name == "eFess_ Success rate" || name == "eFess_ Title" ||
         name == "eFess_ Win loss" || name == "eFess_RectLabel") ObjectSet(name, OBJPROP_XDISTANCE, -220.0);
         if (name == "eFess_ Signals") { temp = ObjectGetString(0, name, OBJPROP_TEXT);
            signs = StrToInteger(StringSubstr(temp, StringLen(temp) - 3, 3));    comm = "* Signals : " + IntegerToString(signs); }
         if (name == "eFess_ Win loss") { temp = ObjectGetString(0, name, OBJPROP_TEXT);
            temp = StringSubstr(temp, StringLen(temp) - 9, 1) == ":"? StringSubstr(temp, StringLen(temp) - 7, 7):
               StringSubstr(temp, StringLen(temp) - 8, 1) == ":"? StringSubstr(temp, StringLen(temp) - 6, 6):
               StringSubstr(temp, StringLen(temp) - 7, 1) == ":"? StringSubstr(temp, StringLen(temp) - 5, 5):
               StringSubstr(temp, StringLen(temp) - 6, 1) == ":"? StringSubstr(temp, StringLen(temp) - 4, 4):
               StringSubstr(temp, StringLen(temp) - 3, 3);     
            wins = StrToInteger(StringSubstr(temp, 3, 1) == "/"? StringSubstr(temp, 0, 3):
               StringSubstr(temp, 2, 1) == "/"? StringSubstr(temp, 0, 2): StringSubstr(temp, 0, 1));
            loss = StrToInteger(StringSubstr(temp, 3, 1) == "/"? StringSubstr(temp, 4, 3):
               StringSubstr(temp, 2, 1) == "/"? StringSubstr(temp, 3, 3): StringSubstr(temp, 2, 3));
            comm += " / Win/Loss : " + wins + "/" + loss + " / Success : " + itmp + "% *"; }
         if (itmp_ != itmp) Comment(comm); }

   t_Sent = OrdersTotal();       if (t_Sent && ! ticket) { ticket = GlobalVariableGet(Symbol() + "_ticket" + (Period() >= 30? "_240": ""));
      if (ticket > 0) tim1 = GlobalVariableGet(Symbol() + "_tim1" + (Period() >= 30? "_240": "")); }
   t_Sent = ticket;        Bull = t_Sent && OrderSelect(ticket, SELECT_BY_TICKET) && ! OrderCloseTime() && ! OrderType();

   if (GlobalVariableCheck(Symbol() + "_Order_240") && TimeCurrent() % 300 > 290) t_O240 = GlobalVariableGet(Symbol() + "_Order_240");    // ! t_O240 && 
   if (Period() >= 30) if (t_Time && (myHour() > 21 || myHour() < 8)) t_Time = false;           else  t_Time = true;

   if ((t_Time && Period() >= 30 && (Minute() < 57 || myHour() < 23 || DayOfWeek() < 5) && (myHour() > 3 || DayOfWeek() > 1)) ||
   (((myHour() == 11 && Minute() > 45) || myHour() == 12 || (myHour() == 13 && Minute() <= 15) || (myHour() == 15 &&
   Minute() > 30) || myHour() == 16 || myHour() == 17 || (myHour() == 18 && Minute() <= 15)) && ((Period() <= 15 &&
   wins >= 10 && itmp >= 72) || (Period() > 15 && wins > 7 && itmp > 70)))) {
/* if (! (Seconds() % 20) && GlobalVariableCheck(Symbol() + "_M5_Buy") && GlobalVariableGet(Symbol() + "_M5_Buy"))
Alert("*** BUY TEST - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + t_O240 + " - " + t_Sent + " - " +
   iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 0) + " - " +
   iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 1) + " - " +
   GlobalVariableGet(Symbol() + "_M5_Buy") + " - " + GlobalVariableGet(Symbol() + "_M5_Sell") + " - " +
   GlobalVariableGet(Symbol() + "_M5_tim1") + " - " + GlobalVariableGet(Symbol() + "_M5_tim2") + " " + TimeToStr(tim1, TIME_MINUTES) + " ***"); */
      if ((t_Wait=(t_O240 && GlobalVariableCheck(Symbol() + "_M5_Buy") && GlobalVariableGet(Symbol() + "_M5_Buy") && ! t_Sent &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 0) &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 1) && TimeCurrent() % 300 > 290 &&
      (! GlobalVariableCheck(Symbol() + "_M5_Sell") || ! GlobalVariableGet(Symbol() + "_M5_Sell")/* || (GlobalVariableGet(Symbol() + "_M5_Sell") < 0 &&
      GlobalVariableGet(Symbol() + "_M5_Buy") > 0 && GlobalVariableGet(Symbol() + "_M5_tim2") < GlobalVariableGet(Symbol() + "_M5_tim1"))*/))) &&
      (TimeCurrent() % 300 > 297 || t_Wait)) { t_Wait = false;
         ticket = OrderSend(Symbol(), 0, 0.01, Ask, 15, 0.99 * Ask, 0.0, NULL, Period() == 30? 5555541: Period() == 60? 5555542: 5555543, 0, clrNONE);
         if (ticket > 0) { tim1 = TimeCurrent();         t_Sent = true;       Bull = true;
/*            if (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 1) > 0.0)
               tim1 -= (TimeCurrent() % (60 * Period()) >= Period() * 30? 60: 30) * Period();
            if (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 2) > 0.0)
               tim1 -= (TimeCurrent() % (60 * Period()) >= Period() * 30? 120: 90) * Period(); */
Alert((temp="*** BUY! - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + TimeToStr(tim1, TIME_MINUTES) + " ***"));       copy_file(temp, 1); } }

/* if (! (Seconds() % 20) && GlobalVariableCheck(Symbol() + "_M5_Sell") && GlobalVariableGet(Symbol() + "_M5_Sell"))
Alert("*** SELL TEST - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + t_O240 + " - " + t_Sent + " - " +
   iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 0) + " - " +
   iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 1) + " - " +
   GlobalVariableGet(Symbol() + "_M5_Buy") + " - " + GlobalVariableGet(Symbol() + "_M5_Sell") + " - " +
   GlobalVariableGet(Symbol() + "_M5_tim1") + " - " + GlobalVariableGet(Symbol() + "_M5_tim2") + " " + TimeToStr(tim1, TIME_MINUTES) + " ***"); */
      if ((t_Wait=(t_O240 && GlobalVariableCheck(Symbol() + "_M5_Sell") && GlobalVariableGet(Symbol() + "_M5_Sell") && ! t_Sent &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 0) &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 1) && TimeCurrent() % 300 > 290 &&
      (! GlobalVariableCheck(Symbol() + "_M5_Buy") || ! GlobalVariableGet(Symbol() + "_M5_Buy")/* || (GlobalVariableGet(Symbol() + "_M5_Buy") < 0 &&
      GlobalVariableGet(Symbol() + "_M5_Sell") > 0 && GlobalVariableGet(Symbol() + "_M5_tim1") < GlobalVariableGet(Symbol() + "_M5_tim2"))*/))) &&
      (TimeCurrent() % 300 > 297 || t_Wait)) { t_Wait = false;
         ticket = OrderSend(Symbol(), 1, 0.01, Bid, 15, 1.01 * Bid, 0.0, NULL, Period() == 30? 5555541: Period() == 60? 5555542: 5555543, 0, clrNONE);
         if (ticket > 0) { tim1 = TimeCurrent();         t_Sent = true;       Bull = false;
/*            if (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 1) > 0.0)
               tim1 -= (TimeCurrent() % (60 * Period()) >= Period() * 30? 60: 30) * Period();
            if (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 2) > 0.0)
               tim1 -= (TimeCurrent() % (60 * Period()) >= Period() * 30? 120: 90) * Period(); */
Alert((temp="*** SELL! - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + TimeToStr(tim1, TIME_MINUTES) + " ***"));      copy_file(temp, 1); } } }

   if (t_Sent && TimeCurrent() - tim1 < 2 && TimeCurrent() - GlobalVariableGet(Symbol() + "_tim1" + (Period() >= 30? "_240": "")) > 20) {
      GlobalVariableSet(Symbol() + (temp="_ticket" + (Period() >= 30? "_240": "")), ticket);          GlobalVars(temp, ticket, 2);
      GlobalVariableSet(Symbol() + (temp="_tim1" + (Period() >= 30? "_240": "")), tim1 - 20);         GlobalVars(temp, (tim1=(tim1-20)), 2); }

// dnTrend GBPJPY.i240 2022.12.08 08:00 O
   tst = false;      for (pos = ObjectsTotal()-1; pos >= 0 && ! tst && t_Sent; pos --) {
/*      datetime OTim2=0;*/       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") && type == OBJ_ARROW &&// StringSubstr(name, 8, StringLen(Symbol())) == Symbol() &&
         floor(OTim1 / (60 * Period())) > floor(tim1 / (60 * Period())) && tim1 > 0; }   // && floor(TimeCurrent() / (60 * Period())) - floor(OTim1 / (60 * Period())) < 720 / Period();
   if (tst && ! OrdersTotal()) tst = false;
   if (tst) { temp="*** --Trend! - " + name + " - " + TimeToStr(OTim1, TIME_MINUTES) + " - " + /*TimeToStr(OTim2, TIME_MINUTES) + " - " + */TimeToStr(tim1, TIME_MINUTES) + " ***";
      if (AccountInfoInteger(ACCOUNT_TRADE_ALLOWED)) Alert(temp);       copy_file(temp, 1); }               bool tst_=tst;

   t_H4_M5 = Period() >= 30 && (t_Sent || ticket2) && ((Bull && (GlobalVariableGet(Symbol() + "_M5_Sell") || ! GlobalVariableGet(Symbol() + "_M5_Buy"))) ||
      (! Bull && (GlobalVariableGet(Symbol() + "_M5_Buy") || ! GlobalVariableGet(Symbol() + "_M5_Sell"))));

   if (t_Pass && t_Sent && Bull && (! t_O240 || iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 3, 0) > 0.0 ||
   tst || t_H4_M5) && OrderSelect(ticket, SELECT_BY_TICKET) && ! OrderCloseTime()) {      // 4/5 flèche grise
      t_Sent = ! OrderClose(ticket, 0.01, OrderType()? Ask: Bid, 15, clrNONE);            if (! t_Sent) {
Alert((temp="*** BUY Closed! - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + tst + " - " + t_H4_M5 + " - " + TimeToStr(tim1, TIME_MINUTES) + " - " +
   GlobalVariableGet(Symbol() + "_M5_Sell") + " - " + (f_wBars(tim1, TimeCurrent())) + " ***"));   copy_file(temp, 1);     // floor(TimeCurrent()/(60*Period())) - floor(tim1/(60*Period()))
         ticket = 0;       tim1 = 0;         /* if (t_H4_M5) GlobalVariableSet(Symbol() + "_M5_Sell", -1);           GlobalVariableSet(Symbol() + "_M5_tim2", 0); }*/
         if (tst && t_O240 && GlobalVariableGet(Symbol() + "_H4_Sell")) {
            ticket = OrderSend(Symbol(), 1, 0.01, Bid, 15, 1.01 * Bid, 0.0, NULL, Period() == 30? 5555541: Period() == 60? 5555542: 5555543, 0, clrNONE);
            if (ticket > 0) { tim1 = TimeCurrent();         t_Sent = true;       Bull = false;        tst = false;
Alert((temp="*** SELL! (2) - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + TimeToStr(tim1, TIME_MINUTES) + " ***"));      copy_file(temp, 1); } } } }

   if (t_Pass && t_Sent && ! Bull && (! t_O240 || iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, 0) > 0.0 ||
   tst || t_H4_M5) && OrderSelect(ticket, SELECT_BY_TICKET) && ! OrderCloseTime()) {      // 4/5 flèche grise
      t_Sent = ! OrderClose(ticket, 0.01, OrderType()? Ask: Bid, 15, clrNONE);            if (! t_Sent) {
Alert((temp="*** SELL Closed! - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + tst + " - " + t_H4_M5 + " - " + TimeToStr(tim1, TIME_MINUTES) + " - " +
   GlobalVariableGet(Symbol() + "_M5_Buy") + " - " + (f_wBars(tim1, TimeCurrent())) + " ***"));    copy_file(temp, 1);     // floor(TimeCurrent()/(60*Period())) - floor(tim1/(60*Period()))
         ticket = 0;       tim1 = 0;         /* if (t_H4_M5) GlobalVariableSet(Symbol() + "_M5_Buy", -1);            GlobalVariableSet(Symbol() + "_M5_tim1", 0); }*/
         if (tst && t_O240 && GlobalVariableGet(Symbol() + "_H4_Buy")) {
            ticket = OrderSend(Symbol(), 0, 0.01, Ask, 15, 0.99 * Ask, 0.0, NULL, Period() == 30? 5555541: Period() == 60? 5555542: 5555543, 0, clrNONE);
            if (ticket > 0) { tim1 = TimeCurrent();         t_Sent = true;       Bull = true;        tst = false;
Alert((temp="*** BUY! (2) - " + IntegerToString(ticket) + " - " + Symbol() + " - " + Bull + " - " + TimeToStr(tim1, TIME_MINUTES) + " ***"));      copy_file(temp, 1); } } } }

   if (t_Sent && TimeCurrent() - tim1 < 2 && TimeCurrent() - GlobalVariableGet(Symbol() + "_tim1" + (Period() >= 30? "_240": "")) > 1) {
      GlobalVariableSet(Symbol() + (temp="_ticket" + (Period() >= 30? "_240": "")), ticket);          GlobalVars(temp, ticket, 3);
      GlobalVariableSet(Symbol() + (temp="_tim1" + (Period() >= 30? "_240": "")), tim1);              GlobalVars(temp, tim1, 3); }

   if ((! ticket || (ticket && (! OrderSelect(ticket, SELECT_BY_TICKET) || OrderCloseTime()))) &&
   GlobalVariableGet(Symbol() + "_tim1" + (Period() >= 30? "_240": ""))) { ticket = 0;          tim1 = 0;
      GlobalVariableSet(Symbol() + (temp="_ticket" + (Period() >= 30? "_240": "")), 0);         GlobalVars(temp, 0, 4);
      GlobalVariableSet(Symbol() + (temp="_tim1" + (Period() >= 30? "_240": "")), 0);           GlobalVars(temp, 0, 4); }

   bool tst2=false, tst3=false;        if (! ticket2) for (pos = 0; pos < OrdersTotal(); pos ++)      // && ! tst2
      if (OrderSelect(pos, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() > 5555550 && OrderType() < 2) { ticket2 = OrderTicket();
         temp = Symbol() + " | " + IntegerToString(ticket2) + " | " + IntegerToString(OrderType()) + " | " + DoubleToStr(OrderLots(), 2) + " | " +
            DoubleToStr(OrderOpenPrice(), Digits()) + " | " + DoubleToStr(OrderStopLoss(), Digits()) + " | " + DoubleToStr(OrderTakeProfit(), Digits()) + " | " +
            OrderComment() + " | " + IntegerToString(OrderMagicNumber());
         tst2 = copy_file(temp, 0) != INVALID_HANDLE;       int orderType=OrderType();       double orderLots=OrderLots(), orderOpenPrice=OrderOpenPrice(),
            orderStopLoss=OrderStopLoss(), orderTakeProfit=OrderTakeProfit();                string orderComment=OrderComment(); }

   if (! tst2 && ! orderMagicNum && (tst3=FileIsExist("EA-write-file_Order_" + Symbol() + ".csv", 0))) { CSV_FILE_READ();        int row=0;
      while(row < ArraySize(line_read) / 7) { ticket2 = StrToInteger(line_read[row][0]);            orderType = StrToInteger(line_read[row][1]);         int ticket3=ticket2;
         orderLots = StrToDouble(line_read[row][2]);             orderOpenPrice = StrToDouble(line_read[row][3]);           orderStopLoss = StrToDouble(line_read[row][4]);
         orderTakeProfit = StrToDouble(line_read[row][5]);             orderComment = line_read[row][6];                    orderMagicNum = StrToInteger(line_read[row][7]);

      if (t_Pass && ticket2 && ! orderType && (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 3, 0) > 0.0 || ((tst_ || t_H4_M5) &&
      (GlobalVariableGet(Symbol() + "_H4_Buy") || GlobalVariableGet(Symbol() + "_M5_Previous") == 1))) && OrderSelect(ticket2, SELECT_BY_TICKET) && ! OrderCloseTime()) {     // 4/5 flèche grise
         ticket2 = ! OrderClose(ticket2, orderLots, orderType? Ask: Bid, 15, clrNONE);       if (! ticket2) {        // < 0.03? orderLots: orderLots - 0.02
Alert((temp="*** BUY Closed! (2) - " + IntegerToString(ticket3) + " - " + Symbol() + " - " + (! orderType) + " - " + tst + " - " + t_H4_M5 + " - " + TimeToStr(OrderOpenTime(), TIME_MINUTES) +
   " - " + GlobalVariableGet(Symbol() + "_M5_Sell") + " ***"));         copy_file(temp, 1);        tim3 = OrderOpenTime(); } }

      if (t_Pass && ticket2 && orderType && (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "01:00", "23:00", "", 1, 90, 2, 0) > 0.0 || ((tst_ || t_H4_M5) &&
      (GlobalVariableGet(Symbol() + "_H4_Sell") || GlobalVariableGet(Symbol() + "_M5_Previous") == 2))) && OrderSelect(ticket2, SELECT_BY_TICKET) && ! OrderCloseTime()) {    // 4/5 flèche grise
         ticket2 = ! OrderClose(ticket2, orderLots, orderType? Ask: Bid, 15, clrNONE);       if (! ticket2) {        // < 0.03? orderLots: orderLots - 0.02
Alert((temp="*** SELL Closed! (2) - " + IntegerToString(ticket3) + " - " + Symbol() + " - " + (! orderType) + " - " + tst + " - " + t_H4_M5 + " - " + TimeToStr(OrderOpenTime(), TIME_MINUTES) +
   " - " + GlobalVariableGet(Symbol() + "_M5_Buy") + " ***"));          copy_file(temp, 1);        tim3 = OrderOpenTime(); } }

      if (ticket3 && tst3 && ! orderType && (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 0) > 0.0 || (tst_ &&
      GlobalVariableGet(Symbol() + "_H4_Buy")) || (GlobalVariableGet(Symbol() + "_M5_Previous") == 2 && GlobalVariableGet(Symbol() + "_M5_tim2_") > GlobalVariableGet(Symbol() + "_M5_tim1_") &&
      GlobalVariableGet(Symbol() + "_M5_tim2_") > tim3 && tim3 > 0 && ! GlobalVariableGet(Symbol() + "_M5_Buy") && ! GlobalVariableGet(Symbol() + "_M5_Sell")) ||
      GlobalVariableGet(Symbol() + "_M5_Buy")) && OrderSelect(ticket3, SELECT_BY_TICKET) && OrderCloseTime()) {         // 4/5 flèche grise     | && OrderProfit() > -1.0
         ticket2 = OrderSend(Symbol(), 0, orderLots, orderOpenPrice, 15, orderStopLoss, orderTakeProfit, orderComment, orderMagicNum, 0, clrNONE);    // < 0.03? orderLots: orderLots - 0.02
if (ticket2) { Alert((temp="*** BUY! (3) - "+IntegerToString(ticket2)+" - "+Symbol()+" - "+(! orderType)+" - "+GlobalVariableGet(Symbol()+"_M5_Buy")+" - "+TimeToStr(TimeCurrent(),TIME_MINUTES)+" ***"));
   copy_file(temp, 1);        ticket3 = ticket2; } }

      if (ticket3 && tst3 && orderType && (iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 0) > 0.0 || (tst_ &&
      GlobalVariableGet(Symbol() + "_H4_Sell")) || (GlobalVariableGet(Symbol() + "_M5_Previous") == 1 && GlobalVariableGet(Symbol() + "_M5_tim1_") > GlobalVariableGet(Symbol() + "_M5_tim2_") &&
      GlobalVariableGet(Symbol() + "_M5_tim1_") > tim3 && tim3 > 0 && ! GlobalVariableGet(Symbol() + "_M5_Buy") && ! GlobalVariableGet(Symbol() + "_M5_Sell")) ||
      GlobalVariableGet(Symbol() + "_M5_Sell")) && OrderSelect(ticket3, SELECT_BY_TICKET) && OrderCloseTime()) {        // 4/5 flèche grise     | && OrderProfit() > -1.0
         ticket2 = OrderSend(Symbol(), 1, orderLots, orderOpenPrice, 15, orderStopLoss, orderTakeProfit, orderComment, orderMagicNum, 0, clrNONE);    // < 0.03? orderLots: orderLots - 0.02
if (ticket2) { Alert((temp="*** SELL! (3) - "+IntegerToString(ticket2)+" - "+Symbol()+" - "+(! orderType)+" - "+GlobalVariableGet(Symbol()+"_M5_Sell")+" - "+TimeToStr(TimeCurrent(),TIME_MINUTES)+" ***"));
   copy_file(temp, 1);        ticket3 = ticket2; } }     row ++; } }

   row = 0;    if (! tst2 && orderMagicNum) { while(! tst2 && row < ArraySize(line_read) / 7) { tst2 = ! (OrderSelect(StrToInteger(line_read[row][0]), SELECT_BY_TICKET) &&
         OrderCloseTime() && (OrderProfit() < -1.0 || ((StrToInteger(line_read[row][1]) && (Ask < StrToDouble(line_read[row][5]) || Bid > StrToDouble(line_read[row][4]))) ||
         (! StrToInteger(line_read[row][1]) && (Bid > StrToDouble(line_read[row][5]) || Ask < StrToDouble(line_read[row][4]))))));        row ++; }
      if (! tst2) { FileDelete("EA-write-file_Order_" + Symbol() + ".csv");               orderMagicNum = 0;
Alert((temp="*** Fin du signal " + IntegerToString(orderMagicNum) + (OrderProfit() < -1.0? " (1)": " (2)") + " - " +
   IntegerToString(ticket2) + " - " + Symbol() + " - " + (! orderType) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***"));       copy_file(temp, 1); } }

/*   if ((GlobalVariableCheck(Symbol() + "_M5_Buy") && GlobalVariableGet(Symbol() + "_M5_Buy") && floor(TimeCurrent() / (60 * Period())) > floor(GlobalVariableGet(Symbol() + "_M5_tim1") / (60 * Period()))) ||
   ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 3, 0)) { GlobalVariableSet(Symbol() + "_M5_Buy", 0);        GlobalVariableSet(Symbol() + "_M5_tim1", 0); }
   if ((GlobalVariableCheck(Symbol() + "_M5_Sell") && GlobalVariableGet(Symbol() + "_M5_Sell") && floor(TimeCurrent() / (60 * Period())) > floor(GlobalVariableGet(Symbol() + "_M5_tim2") / (60 * Period()))) ||
   ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, 0)) { GlobalVariableSet(Symbol() + "_M5_Sell", 0);       GlobalVariableSet(Symbol() + "_M5_tim2", 0); }*/

   if (DayOfWeek() == 5 && myHour() == 22 && Minute() > 42 && OrdersTotal()) Close_All_TradesW();           t_Pass = true;
 }

void OnDeinit(const int reason) { }

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 f_Order_240() { int tmfr=Period();   string stmfr=tmfr==PERIOD_M1? "M1": tmfr==PERIOD_M5? "M5": tmfr==PERIOD_M15? "M15":
      tmfr==PERIOD_M30? "M30": tmfr==PERIOD_H1? "H1": tmfr==PERIOD_H4? "H4": tmfr==PERIOD_D1? "D1": tmfr==PERIOD_W1? "W1": "MN1", temp;

      if (((Minute() < 57 || myHour() < 23 || DayOfWeek() < 5) && (myHour() > 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 || (Bid - iOpen(NULL, 0, 0) > maxcand &&
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0) > 0.0) || (iClose(NULL, 0, 1) - iOpen(NULL, 0, 1) > maxcand &&
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
         2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 1) > 0.0)) && itmp >= 72 &&
      (! t_O240 || ! GlobalVariableGet(Symbol() + "_H4_Buy"))) { t_O240 = true;
         GlobalVariableSet(Symbol() + (temp="_Order_240"), 1);          GlobalVars(temp, 1, 5);
         GlobalVariableSet(Symbol() + (temp="_H4_Buy"), 1);             GlobalVars(temp, 1, 5);
         GlobalVariableSet(Symbol() + (temp="_H4_tim1"), TimeCurrent());      GlobalVars(temp, TimeCurrent(), 5);
if (! (Seconds() % 20)) Alert("*** BUY " + stmfr + "! - " + Symbol() + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***");
         temp = "*** BUY " + stmfr + "! - " + Symbol() + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
            2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, 0), Digits()) + " - " + DoubleToStr(Bid - iOpen(NULL, 0, 0), Digits()) + " - " + DoubleToStr(maxcand, Digits()) + " - " +
            DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0),
            Digits()) + " - " + DoubleToStr(iClose(NULL, 0, 1) - iOpen(NULL, 0, 1), Digits()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "",
            "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 1), Digits()) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";       copy_file(temp, 1); }

      if (((Minute() < 57 || myHour() < 23 || DayOfWeek() < 5) && (myHour() > 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 || (iOpen(NULL, 0, 0) - Ask > maxcand &&
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0) > 0.0) || (iOpen(NULL, 0, 1) - iClose(NULL, 0, 1) > maxcand &&
      iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
         2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 1) > 0.0)) && itmp >= 72 &&
      (! t_O240 || ! GlobalVariableGet(Symbol() + "_H4_Sell"))) { t_O240 = true;
         GlobalVariableSet(Symbol() + (temp="_Order_240"), 1);          GlobalVars(temp, 1, 6);
         GlobalVariableSet(Symbol() + (temp="_H4_Sell"), 1);            GlobalVars(temp, 1, 6);
         GlobalVariableSet(Symbol() + (temp="_H4_tim2"), TimeCurrent());      GlobalVars(temp, TimeCurrent(), 6);
if (! (Seconds() % 20)) Alert("*** SELL " + stmfr + "! - " + Symbol() + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***");
         temp = "*** SELL " + stmfr + "! - " + Symbol() + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
            2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 3, 0), Digits()) + " - " + DoubleToStr(iOpen(NULL, 0, 0) - Ask, Digits()) + " - " + DoubleToStr(maxcand, Digits()) + " - " +
            DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0),
            Digits()) + " - " + DoubleToStr(iOpen(NULL, 0, 1) - iClose(NULL, 0, 1), Digits()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "",
            "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 1), Digits()) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";       copy_file(temp, 1); }

      if (t_O240 && ! GlobalVariableGet(Symbol() + "_Period")) { GlobalVariableSet(Symbol() + (temp="_Period"), Period());       GlobalVars(temp, Period(), 7); }
      datetime tim2=Period() < 30? fmax(GlobalVariableGet(Symbol() + "_M5_tim1"), GlobalVariableGet(Symbol() + "_M5_tim2")):
         fmax(GlobalVariableGet(Symbol() + "_H4_tim1"), GlobalVariableGet(Symbol() + "_H4_tim2"));
      if (((! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue,
         Red, LimeGreen, LightSkyBlue, "", false, 2, f_wBars(tim2, TimeCurrent())) &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
         2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 0) &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 4, 1)) || ! tim2) && GlobalVariableGet(Symbol() + "_H4_Buy")) {
         GlobalVariableSet(Symbol() + (temp="_H4_Buy"), 0);             GlobalVars(temp, 0, 8);
         GlobalVariableSet(Symbol() + (temp="_H4_tim1"), 0);            GlobalVars(temp, 0, 8);
         t_O240 = false;         GlobalVariableSet(Symbol() + (temp="_Order_240"), 0);       GlobalVars(temp, 0, 8);
      temp = "*** t_O240 = false;! (1) - " + Symbol() + " - " + IntegerToString(Period()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "",
         0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, f_wBars(tim2, TimeCurrent())), Digits()) + " - " +
         IntegerToString(f_wBars(tim2, TimeCurrent())) + " - " +TimeToStr(tim2, TIME_MINUTES) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";       copy_file(temp, 1); }
      if (((! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue,
         Red, LimeGreen, LightSkyBlue, "", false, 3, f_wBars(tim2, TimeCurrent())) &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
         2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 0) &&
      ! iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "", 0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90,
      2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 5, 1)) || ! tim2) && GlobalVariableGet(Symbol() + "_H4_Sell")) {
         GlobalVariableSet(Symbol() + (temp="_H4_Sell"), 0);            GlobalVars(temp, 0, 9);
         GlobalVariableSet(Symbol() + (temp="_H4_tim2"), 0);            GlobalVars(temp, 0, 9);
         t_O240 = false;         GlobalVariableSet(Symbol() + (temp="_Order_240"), 0);       GlobalVars(temp, 0, 9);
      temp = "*** t_O240 = false;! (2) - " + Symbol() + " - " + IntegerToString(Period()) + " - " + DoubleToStr(iCustom(NULL, 0, "Entry_Points_Pro.ex4", "", maxcand, "",
         0, 0, 500, "", 0, "", "08:00", "21:00", "", 1, 90, 2, DeepSkyBlue, Red, LimeGreen, LightSkyBlue, "", false, 2, f_wBars(tim2, TimeCurrent())), Digits()) + " - " +
         IntegerToString(f_wBars(tim2, TimeCurrent())) + " - " +TimeToStr(tim2, TIME_MINUTES) + " - " + TimeToStr(TimeCurrent(), TIME_MINUTES) + " ***";       copy_file(temp, 1); } }

int copy_file(string tmp,int item) { int handle, it=0, t=0;     string str, temp="";
/*   MqlDateTime t;    TimeLocal(t);*/     bool tst=false;      ulong cmpl_size;
   string Symb=StringFind(Symbol(), ".")? StringSubstr(Symbol(), 0, StringFind(Symbol(), ".")): Symbol(),
      filename=item? "Entry_Points_Pro_file_" + Symb + ".log": "Entry_Points_Pro_file_order_" + Symb + ".csv";
   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 ((cmpl_size=FileSize(handle)) > 0) FileSeek(handle, FileSize(handle), 0);
         if (item && (myHour() == 11 || myHour() == 23) && cmpl_size > 10000000 && tmp_saved > 0 && TimeCurrent() - tmp_saved > 150) {
            FileSeek(handle, -20480, SEEK_CUR);    while(! tst && t < 5000) { FileSeek(handle, -10240, SEEK_CUR);    FileReadString(handle);
               str = FileReadString(handle);          str = StringSubstr(str, 11, 2);        t ++;          tst = StrToInteger(str) < 23; }
            if (! tst) { if (cmpl_size > 0) FileSeek(handle, FileSize(handle), 0);           break; }
            while(! FileIsEnding(handle)) temp += (temp != "" ? "\r\n" : "") + FileReadString(handle);
            temp += "\r\n - - - - - - -";       FileClose(handle);         FileCopy(filename, 0, filename + "2", 0);
            Sleep(100);       FileDelete(filename, 0);      Sleep(100);    handle = FileOpen (filename, FILE_TXT|FILE_WRITE);
            FileWrite(handle, temp); }
         tst = FileWrite(handle, TimeToStr(TimeCurrent(), item? TIME_DATE|TIME_SECONDS: 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 ++; }       tmp_saved = TimeCurrent();
   return(handle); }

string line_read[][8];
void CSV_FILE_READ() { string filename="EA-write-file_Order_" + Symbol() + ".csv";
   int row=0, col=0, handle=FileOpen(filename,FILE_CSV|FILE_READ, "|");
   if (handle > 0) { while(True) { string temp=FileReadString(handle);
      if (FileIsEnding(handle) || (col == 1 && StringTrimLeft(StringTrimRight(temp)) != Symbol())) break;
      if (col > 1) line_read[row][col-2] = temp;
      if (FileIsLineEnding(handle)) { col = 0;     row ++; }
      else  col ++; }      FileClose(handle); }
   else Alert("*** (2) File "+filename+" not found, the last error is ", GetLastError() + " ***"); }

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

void Close_All_TradesW() { string temp;
  for (int i = OrdersTotal()-1; i >= 0; i --) {
    if (OrderSelect(i, SELECT_BY_POS)) { bool result = false;
       switch(OrderType()) {
          //Close opened long positions
          case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Pink);   // Slippage
                         break;
          //Close opened short positions
          case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Pink);   // Slippage
                         break;
          //Close pending orders
            case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() );     break;
            case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );     break;
            case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );     break;
            case OP_SELLSTOP  : result = OrderDelete( OrderTicket() ); }
       if (result == false) Sleep(2000); } }

  temp = "***** Account WE Reached. All Trades Have Been Closed. ("+DoubleToStr(AccountBalance(),2)+" - "+myHour()+") *****";      Alert(temp); }

int myHour() { return(Hour()); }       // - 1
