//+------------------------------------------------------------------+
//| Signaux donnés sur Gold Cup / Telegram                           |
//+------------------------------------------------------------------+
#define o_FHn     n
#define PAUSE	   100
#import "shell32.dll"
 int ShellExecuteW(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd);
#import
#ifdef o_FHn
#include <Telegram_Gold-Cup_VPS.mqh>
#ifdef o_FH2
#include <Telegram_Gold-Cup-2.mqh>
#endif
#ifdef o_FH4
#include <Telegram_Gold-Cup-4.mqh>
#endif
#ifdef o_FH5
#include <Telegram_Gold-Cup-5.mqh>
#endif
#ifdef o_FH6
#include <Telegram_Gold-Cup-6.mqh>
#endif
#ifndef o_FH6
/*extern */bool EA_20pips = true;
#endif
#else
extern bool EA_20pips = true;
#endif
int slip#ifdef o_FHn, tickt_[2], tickt_b[2], EA_n_TP=EA_nb_TP, t_web=0#endif;          double lot=_lot, pips2dbl#ifdef o_FHn, LM_#endif;
string Symb_, Op_20pips, Op_Symbs, Lm_Symbs, Op_Symb2s, terminal=arr_Terms[Terminal], admin=arr_Admins[Terminal];       bool Del_Reps=false, port;

int OnInit() { EventSetTimer(5);
   if (FileIsExist(".\\Op_Symbs_List.txt")) { int handle = FileOpen(".\\Op_Symbs_List.txt", FILE_TXT|FILE_READ);
         if (handle != INVALID_HANDLE) { Op_Symbs = FileReadString(handle);      Sleep(PAUSE);     FileClose(handle);
            Alert("******* File Op_Symbs_List.txt contains: " + Op_Symbs + " *******"); }
         else Alert("*** (1) File Op_Symbs_List.txt not found, the last error is ", GetLastError() + " ***"); }
   if (FileIsExist(".\\Op_Symb2s_List.txt")) { handle = FileOpen(".\\Op_Symb2s_List.txt", FILE_TXT|FILE_READ);
         if (handle != INVALID_HANDLE) { Op_Symb2s = FileReadString(handle);     Sleep(PAUSE);     FileClose(handle);
            Alert("******* File Op_Symb2s_List.txt contains: " + Op_Symb2s + " *******"); }
         else Alert("*** (1) File Op_Symb2s_List.txt not found, the last error is ", GetLastError() + " ***"); }
   if (FileIsExist(".\\Lm_Symbs_List.txt")) { handle = FileOpen(".\\Lm_Symbs_List.txt", FILE_TXT|FILE_READ);
         if (handle != INVALID_HANDLE) { Lm_Symbs = FileReadString(handle);      Sleep(PAUSE);     FileClose(handle);
            Alert("******* File Lm_Symbs_List.txt contains: " + Lm_Symbs + " *******"); }
         else Alert("*** (1) File Lm_Symbs_List.txt not found, the last error is ", GetLastError() + " ***"); }
   return(INIT_SUCCEEDED); }

void OnTick() {
 if (FileIsExist(".\\Telegram\\Copy_Trade_On.txt", 0) || FileIsExist(".\\Telegram\\Copy_Trade_Off.txt", 0)) {
   if (FileIsExist(".\\Telegram\\Copy_Trade_On.txt", 0)) FileMove(".\\Telegram\\Copy_Trade_On.txt", 0, ".\\Telegram\\Copy_Trade_Off.txt", 0);
   int nf, _nrep=1, n_rep=1, nLines=0, row=0, cpt=0;        string filename;       // B3511AE7AE29D20268E67E28E5D8DF10 (FH4)
   int ts=ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
      "date_time.bat", ">>logs\\Copy_Trade_All.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
#ifdef o_FHn   while(cpt < 23 && (ts=#else   ts =#endif ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "Copy_Trade_All.bat", ">>logs\\Copy_Trade_All.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1)#ifdef o_FHn) != 42) {
Alert("******* (1) ts = " + IntegerToString(ts) + " *******");         if (cpt) Sleep(PAUSE);        cpt ++; }      cpt = 0;#else;#endif
   Sleep(5 * PAUSE);         #ifdef o_FHn while(cpt < 23 && FileIsExist(".\\Telegram\\Copy_Trade_Bat_On.txt", 0)) { Sleep(PAUSE);
Alert("******* cpt = " + IntegerToString(cpt) + " *******");            cpt ++; }
   Sleep(PAUSE);              if (FileIsExist(".\\Telegram\\Copy_Trade_Bat_On.txt", 0)) {// FileDelete(".\\Telegram\\Copy_Trade_Off.txt", 0);
Alert("******* cpt = " + IntegerToString(cpt) + " *******");         return; }#endif         nf = 1;
   while((filename=File_Next(0, nf, 0)) != "last_file" && filename != "") if (StringSubstr(filename, StringLen(filename)-4, 4) != ".bat" &&
     #ifdef o_FHn filename != "Copy_Trade_Bat_Off.txt" &&#endif filename != "Copy_Trade_Off.txt") FileDelete("Telegram\\"+filename, 0);            else  nf ++;
   ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
      "date_time.bat", ">>logs\\Del_Trade_Dskt.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
   ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
      "Del_Trade_Dskt.bat", ">>logs\\Del_Trade_Dskt.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
   Sleep(5 * PAUSE);          FileDelete(".\\Telegram\\Copy_Trade_Off.txt", 0);#ifdef o_FHn           cpt = 0;#endif

   while(FileIsExist(".\\Telegram_copied ("+IntegerToString(n_rep)+")\\Copy_Trade_Off.txt",0) || FileIsExist(".\\Telegram_copied ("+IntegerToString(n_rep)+")\\Copy_Trade_Bat_On.txt",0)) n_rep ++;
   n_rep --;   while(_nrep <= n_rep) { nf = 1;   while((filename=File_Next(1, nf, _nrep)) != "last_file" && filename != "") {
      if (StringSubstr(filename, StringLen(filename)-4, 4) != ".bat" &&#ifdef o_FHn filename != "Copy_Trade_Bat_Off.txt" &&
      filename != "Copy_Trade_Bat_On.txt" &&#endif filename != "Copy_Trade_Off.txt") {
         int handle=FileOpen("Telegram_copied ("+IntegerToString(_nrep)+")\\"+filename, FILE_TXT|FILE_READ);
         string line;    if (handle != INVALID_HANDLE) { while(true) { line = "";    line = FileReadString(handle);
            if (FileIsLineEnding(handle)) nLines ++;           if (FileIsEnding(handle)) break; }        nLines ++;
            FileClose(handle);      Print("******* File "+filename+" contains "+nLines+" line"+(nLines > 1? "s": "")+" *******"); }
         else Alert("*** (1) File "+filename+" not found, the last error is ", GetLastError() + " ***"); }
         nLines ++;     nf ++; }   _nrep ++; }#ifdef o_FHn           Sleep(PAUSE);#endif
   if (line_read[0][0] != "") while(row < nLines + 1 && row < ArraySize(line_read) / 8 && line_read[row][0] != "- - -") {
      line_read[row][0] = "";       line_read[row][1] = "";       line_read[row][2] = "";       line_read[row][3] = "";
      line_read[row][4] = "";       line_read[row][5] = "";       line_read[row][6] = "";       line_read[row][7] = "";    row ++; }
   ArrayResize(line_read, nLines + 1, 1);       _nrep = 1;        row = 0;          int row_;
   while(_nrep <= n_rep) { row_ = row;          while(row_ == row && cpt < 3) {
         nf = 1;        while((filename=File_Next(1, nf, _nrep)) != "last_file" && filename != "") {
            if (StringSubstr(filename, StringLen(filename)-4, 4) != ".bat" &&#ifdef o_FHn filename != "Copy_Trade_Bat_Off.txt" &&
            filename != "Copy_Trade_Bat_On.txt" &&#endif filename != "Copy_Trade_Off.txt") {
               line_read[row][0] = "--- " + filename + " ---------------------------------";       row ++;
               row = CSV_FILE_READ(row, _nrep, filename);            Sleep(2 * PAUSE); }           nf ++; }
         if (row_ == row) Sleep(2 * PAUSE);        cpt ++; }#ifdef o_FHn            cpt = 0;#endif
      handle = FileOpen("Telegram\\logs\\Copy_Trade_All_b.txt", FILE_TXT|FILE_READ);
/* #ifdef o_FH5      if (handle == INVALID_HANDLE) {
         ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
            "date_time.bat", ">>logs\\Copy_Trade_All_b3.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
         ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\logs\\"+
            "Copy_Trade_All_b1.bat", ">>Copy_Trade_All_b3.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\logs", 1);
         filename = "Copy_Trade_All_b2.txt"; }           else { filename = "Copy_Trade_All_b.txt";          FileClose(handle); }          Sleep(2 * PAUSE);#endif */
      ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "date_time.bat", ">>logs\\" + filename, "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
#ifdef o_FHn   while(cpt < 23 && (ts=#else   ts =#endif ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "Copy_Trade_All_b.bat", "\"..\\Telegram_copied ("+IntegerToString(_nrep)+")\" >>logs\\" + filename, "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal"+
         "\\" + terminal + "\\MQL4\\Files\\Telegram", 1)#ifdef o_FHn) != 42) {
Alert("******* (2) ts = " + IntegerToString(ts) + " *******");         if (cpt) Sleep(PAUSE);        cpt ++; }      cpt = 0;#else;#endif           Sleep(t_vps * PAUSE);
/* #ifdef o_FH5      if (filename == "Copy_Trade_All_b2.txt")
         ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\logs\\"+
            "Copy_Trade_All_b2.bat", ">>Copy_Trade_All_b3.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\logs", 1);#endif */
#ifdef o_FHn      while(cpt < 23 && FileIsExist(".\\Telegram\\Copy_Trade_Bat_On.txt", 0)) { Sleep(PAUSE);         cpt ++; }/*      if (cpt) Sleep(PAUSE);*/#endif
      ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "date_time.bat", ">>logs\\Del_Trade_All.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
      ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "Del_Trade_All.bat", "\"..\\Telegram_copied ("+IntegerToString(_nrep)+")\" >>logs\\Del_Trade_All.txt", 
         "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
      Sleep(2 * PAUSE);          _nrep ++; }       line_read[row][0] = "- - -";

   int col=0;        row = 0;          string temp=TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS);
   while(row < ArraySize(line_read) / 8 && line_read[row][0] != "- - -") {
      temp += (row? "\r\n": " ") + line_read[row][col] + " " + line_read[row][col+1] + " " + line_read[row][col+2] + " " + line_read[row][col+3] + " " +
         line_read[row][col+4] + " " + line_read[row][col+5] + " " + line_read[row][col+6] + " " + line_read[row][col+7];        row ++; }
   temp += "\r\n--------------------------------------------------------------------------------";             copy_file(0, temp);

   int type, ticket;       bool t_close;        row = 0;     col = 0;      cpt = 0;#ifdef o_FH6    t_limit = false;#endif
   while(row < ArraySize(line_read) / 8 && line_read[row][0] != "- - -") {
Alert("*****1* "+IntegerToString(row)+" - "+line_read[row][0]+" *******");
      type = 0;   ticket = 0;    slip = 2;      port = true;         //      if (line_read[row][0] == "Share") type = 0;
#ifdef o_FH2      int itm1=2, itm2=0;        bool moon=false, virg=false;
      while(itm1 < 15 && ! moon) { moon = (StringSubstr(line_read[row-itm1][0], 0, 4) == "ＭＯＯＮ");        itm1 ++; }        if (moon) { itm1 --;
         while(itm2 < 8 && ! virg) { virg = (StringSubstr(line_read[row-itm1][itm2], StringLen(line_read[row-itm1][itm2]) - 1) == ",");      itm2 ++; } }
      if ((StringSubstr(line_read[row-itm1][0], 0, 4) == "ＭＯＯＮ" && StringSubstr(line_read[row-itm1][itm2], 0, 1) == "[" && StringSubstr(line_read[row-itm1][itm2], 3, 1) == "/" &&
      (Day() != StrToInteger(StringSubstr(line_read[row-itm1][itm2], 1, 2)) || Hour() - StrToInteger(StringSubstr(line_read[row-itm1][itm2+1], 0, 2)) > (Minute()? 1: 2) ||
      Minute() - StrToInteger(StringSubstr(line_read[row-itm1][itm2+1], 3, 2)) > 0)) && moon) {
Alert("*****1b* "+IntegerToString(row)+" - "+line_read[row-itm1][0]+" - "+line_read[row-itm1][1]+" - "+line_read[row-itm1][2]+" - "+line_read[row-itm1][3]+" - "+line_read[row-itm1][4]+" - "+
   StringSubstr(line_read[row-itm1][4], 0, 2) + " - " + StringSubstr(line_read[row-itm1][4], 3, 2) + " *******");
#endif
#ifdef o_FH4
      if (((StringSubstr(line_read[row-3][1],0,1) == "[" && StrToInteger(StringSubstr(line_read[row-3][2],0,2)) && StrToInteger(StringSubstr(line_read[row-3][2],3,2)) && (Day() !=
      StrToInteger(StringSubstr(line_read[row-3][1],1,2)) || Hour()-StrToInteger(StringSubstr(line_read[row-3][2],0,2)) > 1 || Minute()-StrToInteger(StringSubstr(line_read[row-3][2],3,2)) > 17)) ||
      (StringSubstr(line_read[row-1][1],0,1) == "[" && StrToInteger(StringSubstr(line_read[row-1][2],0,2)) && StrToInteger(StringSubstr(line_read[row-1][2],3,2)) && (Day() !=
      StrToInteger(StringSubstr(line_read[row-1][1],1,2)) || Hour()-StrToInteger(StringSubstr(line_read[row-1][2],0,2)) > 1 || Minute()-StrToInteger(StringSubstr(line_read[row-1][2],3,2)) > 17))) &&
      StringSubstr(line_read[row-3][0], StringLen(line_read[row-3][0]) - 1) == ",") {
Alert("*****1b* "+IntegerToString(row)+" - "+line_read[row-3][0]+" - "+line_read[row-3][1]+" - "+line_read[row-3][2]+" - "+line_read[row-3][3]+" - "+line_read[row-3][4]+" - "+
   StringSubstr(line_read[row-3][4], 0, 2) + " - " + StringSubstr(line_read[row-3][4], 3, 2)+" | "+line_read[row-1][0]+" - "+line_read[row-1][1]+" - "+line_read[row-1][2]+" - "+
   line_read[row-1][3]+" - "+line_read[row-1][4]+" - "+StringSubstr(line_read[row-1][4], 0, 2)+" - " + StringSubstr(line_read[row-1][4], 3, 2)+" *******");
#endif
#ifdef o_FH5
      if ((line_read[row-3][0] == "Professor" && StringSubstr(line_read[row-3][1],0,3) == "VIP" && line_read[row-3][2] == "(yearly)," && (Day() !=
      StrToInteger(StringSubstr(line_read[row-3][3],1,2)) || Hour()-StrToInteger(StringSubstr(line_read[row-3][4],0,2))>(Minute()?1:2) || Minute()-StrToInteger(StringSubstr(line_read[row-3][4],3,2)) > 17)) ||
      (line_read[row-1][0] == "Professor" && StringSubstr(line_read[row-1][1],0,3) == "VIP" && line_read[row-1][2] == "(yearly)," && (Day() !=
      StrToInteger(StringSubstr(line_read[row-1][3],1,2)) || Hour()-StrToInteger(StringSubstr(line_read[row-1][4],0,2))>(Minute()?1:2) || Minute()-StrToInteger(StringSubstr(line_read[row-1][4],3,2)) > 17)) ||
      (line_read[row-15][0] == "Professor" && StringSubstr(line_read[row-15][1],0,3) == "VIP" && line_read[row-15][2] == "(yearly)," && (Day() !=
      StrToInteger(StringSubstr(line_read[row-15][3],1,2)) || Hour()-StrToInteger(StringSubstr(line_read[row-15][4],0,2))>(Minute()?1:2) || Minute()-StrToInteger(StringSubstr(line_read[row-15][4],3,2)) > 17)) ||
      (line_read[row-14][0] == "Professor" && StringSubstr(line_read[row-14][1],0,3) == "VIP" && line_read[row-14][2] == "(yearly)," && (Day() !=
      StrToInteger(StringSubstr(line_read[row-14][3],1,2)) || Hour()-StrToInteger(StringSubstr(line_read[row-14][4],0,2))>(Minute()?1:2) || Minute()-StrToInteger(StringSubstr(line_read[row-14][4],3,2)) > 17))) {
Alert("*****1b* "+IntegerToString(row)+" - "+line_read[row-3][0]+" - "+line_read[row-3][1]+" - "+line_read[row-3][2]+" - "+line_read[row-3][3]+" - "+line_read[row-3][4]+" - "+
   StringSubstr(line_read[row-3][4], 0, 2) + " - " + StringSubstr(line_read[row-3][4], 3, 2)+" | "+line_read[row-1][0]+" - "+line_read[row-1][1]+" - "+line_read[row-1][2]+" - "+
   line_read[row-1][3]+" - "+line_read[row-1][4]+" - "+StringSubstr(line_read[row-1][4], 0, 2)+" - " + StringSubstr(line_read[row-1][4], 3, 2)+" | "+line_read[row-15][0]+
   " - "+line_read[row-15][1]+" - "+line_read[row-15][2]+" - "+line_read[row-15][3]+" - "+line_read[row-15][4]+" - "+StringSubstr(line_read[row-15][3], 1, 2)+" - "+StringSubstr(line_read[row-15][4],
   0, 2)+" - "+StringSubstr(line_read[row-15][4], 3, 2)+" | "+line_read[row-14][0]+" - "+line_read[row-14][1]+" - "+line_read[row-14][2]+" - "+line_read[row-14][3]+" - "+line_read[row-14][4]+" - "+
   StringSubstr(line_read[row-14][3], 1, 2)+" - "+StringSubstr(line_read[row-14][4], 0, 2)+" - "+StringSubstr(line_read[row-14][4], 3, 2)+" *******");
#endif
#ifdef o_FH6      int itm1=2, itm2=0;        bool moon=false, virg=false;
      while(itm1 < 15 && ! moon) { moon = (StringSubstr(line_read[row-itm1][0], 0, 4) == "ＭＯＯＮ");        itm1 ++; }        if (moon) { itm1 --;
         while(itm2 < 8 && ! virg) { virg = (StringSubstr(line_read[row-itm1][itm2], StringLen(line_read[row-itm1][itm2]) - 1) == ",");      itm2 ++; } }
      if ((StringSubstr(line_read[row-itm1][0], 0, 4) == "ＭＯＯＮ" && StringSubstr(line_read[row-itm1][itm2], 0, 1) == "[" && StringSubstr(line_read[row-itm1][itm2], 3, 1) == "/" &&
      (Day() != StrToInteger(StringSubstr(line_read[row-itm1][itm2], 1, 2)) || Hour() - StrToInteger(StringSubstr(line_read[row-itm1][itm2+1], 0, 2)) > (Minute()? 1: 2) ||
      Minute() - StrToInteger(StringSubstr(line_read[row-itm1][itm2+1], 3, 2)) > 0)) && moon) {
Alert("*****1b* "+IntegerToString(row)+" - "+line_read[row-itm1][0]+" - "+line_read[row-itm1][1]+" - "+line_read[row-itm1][2]+" - "+line_read[row-itm1][3]+" - "+line_read[row-itm1][4]+" - "+
   StringSubstr(line_read[row-itm1][4], 0, 2) + " - " + StringSubstr(line_read[row-itm1][4], 3, 2) + " *******");
#endif
         row ++;        continue; }
#ifdef o_FH2
      if (moon) port = false;
#endif
#ifdef o_FH4 
      if (StringSubstr(line_read[row-3][0], StringLen(line_read[row-3][0]) - 1) == ",") port = false;
#endif
#ifdef o_FH5 
      if (line_read[row-3][0] == "Professor" || line_read[row-1][0] == "Professor" || line_read[row-15][0] == "Professor" || line_read[row-14][0] == "Professor") port = false;
#endif
#ifdef o_FH6
      if (StringSubstr(line_read[row-3][0], StringLen(line_read[row-3][0]) - 1) == ",") port = false;
#endif
#ifdef o_FHn 
      type = line_read_type(row);         string Symb;         double LM;
      while((! ticket || ticket == -1) && cpt < 9) { int icase=line_read_switch(type, row);
         ticket = tickt_[0];        Symb = Symb_;        LM = LM_;
         if (type == 2 && ticket) { tickt_b[0] = tickt_[0];       tickt_b[1] = tickt_[1]; }        // else tickt_b = 0;
#else
      if ((line_read[row][0] == "Sell" || line_read[row][0] == "Buy") && line_read[row][1] == "again") type = 1;
      else if ((line_read[row][0] == "Sell" || line_read[row][0] == "Buy") && line_read[row][1] == "limit") type = 2;
      else if ((line_read[row][0] == "TP" || line_read[row][0] == "TO") && line_read[row-1][0] == "SL" && (row+2 > ArraySize(line_read)/8-1 ||
         (StringSubstr(line_read[row+2][0], 3, 4) != "pips" && line_read[row+2][1] != "profit," && line_read[row+2][1] != "again"))) type = 3;
//      if (line_read[row][0] == "GOLD" && line_read[row][1] == "CUP") type = 4;
      else if ((line_read[row][0] == "join" || line_read[row][0] == "@iml_goldcup") && (line_read[row-2][0] == "TP" || line_read[row-2][0] == "TO")) type = 4;
      else if (StringSubstr(line_read[row][0], 3, 4) == "pips" && line_read[row][1] == "profit,") type = 5;
      else if (StringSubstr(line_read[row][0], 2, 4) == "pips" && line_read[row][1] == "profit,") type = 6;
      else if (line_read[row][0] == "CLOSE" || line_read[row][0] == "close") type = 7;
      else if (line_read[row][0] == "DAILY" && line_read[row][1] == "STATS") type = 8;
      else if (line_read[row][0] == "Sell" || line_read[row][0] == "sell" || line_read[row][0] == "Buy" || line_read[row][0] == "buy") type = 9;

      while((! ticket || ticket == -1) && cpt < 9) { switch(type) {
         case 1  :
Alert("*****2-1* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row-2][1]+" - "+line_read[row-3][1]+" - "+line_read[row-4][2]+" - "+line_read[row-4][1]+" - "+line_read[row-4][0]+" *******");
            double TP=StrToDouble(line_read[row-2][1]), SL=StrToDouble(line_read[row-3][1]), LM=StrToDouble(line_read[row-4][2]);
            string Symb=line_read[row-4][0];          int icase=0;
            ResetLastError();    if (StringFind(Symb, "/") > 0) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));
            if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
// . . .
         break;
         case 2  :
Alert("*****2-2* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row-2][1]+" - "+line_read[row-3][1]+" - "+line_read[row-4][2]+" - "+line_read[row-4][1]+" - "+line_read[row-4][0]+" *******");
            LM = StrToDouble(line_read[row][4]);         Symb = line_read[row][2];         ticket = 0;       icase = 0;
            SL = line_read[row+2][0] == "TP"? StrToDouble(line_read[row+3][1]): StrToDouble(line_read[row+2][1]);
            TP = line_read[row+2][0] == "TP"? fabs(LM - StrToDouble(line_read[row+2][1])) > fabs(LM - StrToDouble(line_read[row+1][1]))? 
               StrToDouble(line_read[row+2][1]): StrToDouble(line_read[row+1][1]): StrToDouble(line_read[row+1][1]);
            Bull = line_read[row][0] == "BUY" || line_read[row][0] == "Buy" || line_read[row][0] == "buy";
            ResetLastError();    if (StringFind(Symb, "/") > 0) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));
            if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
// . . .
         break;
         case 3  :
Alert("*****2-3* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row][1]+" - "+line_read[row-1][1]+" - "+line_read[row-2][2]+" - "+line_read[row-2][1]+" - "+line_read[row-2][0]+" *******");
            TP = StrToDouble(line_read[row][1]);      SL = StrToDouble(line_read[row-1][1]);    LM = StrToDouble(line_read[row-2][2]);
            Bull = line_read[row-2][1] == "BUY";      Symb = line_read[row-2][0];         ticket = 0;       icase = 0;
            ResetLastError();    if (StringFind(Symb, "/") > 0) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));
            if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
// . . .
         break;
         case 4  :
Alert("*****2-4* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row-2][1]+" - "+line_read[row-3][1]+" - "+line_read[row-4][2]+" - "+line_read[row-4][1]+" - "+line_read[row-4][0]+" *******");
            TP = StrToDouble(line_read[row-2][1]);    SL = StrToDouble(line_read[row-3][1]);    LM = StrToDouble(line_read[row-4][2]);
            Bull = line_read[row-4][1] == "BUY";      Symb = line_read[row-4][0];         ticket = 0;       icase = 0;
            ResetLastError();    if (StringFind(Symb, "/") > 0) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));
            if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
// . . .
         break;
         case 7  :
Alert("*****2-7* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row][1]+" *******");
            Symb = line_read[row][1];                 ResetLastError();
            if (StringFind(Symb, "/") > 0) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));
            if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
// . . .
         break;
         case 9  :
Alert("*****2-9* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row][1]+" *******");
            TP = StrToDouble(line_read[row+2][1]);       SL = StrToDouble(line_read[row+1][1]);
            Symb = line_read[row][1];        ResetLastError();       ticket = 0;       icase = 0;
            Bull = line_read[row][0] == "Buy" || line_read[row][0] == "buy";
            if (Symb == "GOLD" || Symb == "gold") Symb = "XAUUSD";
            if (StringFind(Symb, "/") > 0) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));
            if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
// . . .
         break;
         default :
Alert("*****2-0* "+IntegerToString(row)+" - "+line_read[row][0]+" - "+line_read[row][1]+" *******"); }
#endif
if (icase) Alert("******* "+IntegerToString(icase)+" - "+DoubleToStr(MarketInfo(Symb,MODE_BID), MarketInfo(Symb, MODE_DIGITS))+" - "+DoubleToStr(MarketInfo(Symb,MODE_ASK), MarketInfo(Symb, MODE_DIGITS))+" - "+
   DoubleToStr(iCustom(Symb, PERIOD_M30, "New folder\\ADX.ex4", 1, 14), 2)+" - "+DoubleToStr(iCustom(Symb, PERIOD_M30, "New folder\\ADX.ex4", 1, 7), 2)+" - "+IntegerToString(ticket)+" - "+GetLastError()+" *******");
#ifdef o_FHn
if (line_read[row-3][2] == "(yearly),") Alert("*****1c* "+IntegerToString(row)+" - "+line_read[row-3][0]+" - "+line_read[row-3][1]+" - "+line_read[row-3][2]+" - "+line_read[row-3][3]+" - "+line_read[row-3][4]+" - "+
#else
if (StrToInteger(StringSubstr(line_read[row-3][1], 1, 2)) == Day() || icase)
   Alert("*****1c* "+IntegerToString(row) + " - " + line_read[row-3][0] + " - " + line_read[row-3][1] + " - " + line_read[row-3][2] + " - " + line_read[row-3][3] + " - " + line_read[row-3][4] + " - " +
#endif
   StringSubstr(line_read[row-3][2], 0, 2) + " - " + StringSubstr(line_read[row-3][2], 3, 2) + " - " + StringSubstr(line_read[row-3][0], 0, 4) + " - " + StringSubstr(line_read[row-3][0], 4, 3) + " - " +
   StringLen(line_read[row-3][0])+ " *******");
      if ((! ticket || ticket == -1) && iCustom(Symb, PERIOD_M30, "New folder\\ADX.ex4", 1, 7) < 47) { if (slip == 2) slip = 5;     else cpt = 9; }      else cpt = 9; }
      cpt = 0;          row ++; } }

   int total=OrdersTotal(), itmp#ifdef o_FHn, n_tcks#endif;      bool tst;      double ordprof, dtmp;
   for (int pos=0; pos < total; pos ++) { slip = 2;
      if (!OrderSelect(pos, SELECT_BY_POS) || OrderMagicNumber()!=mgc_num) continue;      ticket = OrderTicket();       //BrokerDigitAdjust(OrderSymbol());
if (FileIsExist(".\\Telegram\\Copy_Trade_On.txt", 0)) 
Alert("*****1* OrderSelect: "+IntegerToString(pos)+" - "+IntegerToString(total)+" - "+OrderSelect(pos, SELECT_BY_POS)+" - "+IntegerToString(OrderTicket())+" *******");
#ifdef o_FHn      n_tcks = OrderSymbol() == "XAUUSD"+_p? 237: 187;      int ticket_;#endif
      if (StringFind(OrderComment(), "|") > 0 || StringFind(OrderComment(), "-") > 0) {
         if (StringFind(OrderComment(), "|") > 0) LM = StrToDouble(StringSubstr(OrderComment(), 0, StringFind(OrderComment(), "|") - 1));
         if (StringFind(OrderComment(), "-") > 0) LM = StrToDouble(StringSubstr(OrderComment(), StrToInteger(StringSubstr(OrderComment(), 0, 1))? 0: 1, 
            StringFind(OrderComment(), "-") - (StrToInteger(StringSubstr(OrderComment(), 0, 1))? 1: 2)));      //int(pips2dbl / MarketInfo(OrderSymbol(),MODE_POINT))
         ordprof = LM > 0.0? OrderProfit() + ((OrderType()? 1: -1) * (LM - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_TICKSIZE) - 10) *
            MarketInfo(OrderSymbol(), MODE_TICKVALUE) * lot: OrderProfit(); }       else { ordprof = OrderProfit();        LM = 0.0; }    // ^ rapport à 197 ticks
#ifdef o_FHn
      if (! EA_20pips && (! GlobalVariableCheck(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP1") ||
      GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP1") == 0.0 ||
      ! GlobalVariableCheck(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP2") ||
      GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP2") == 0.0)) continue;
#else
      if (! EA_20pips && StringFind(Op_20pips, OrderSymbol()) == -1) continue;
#endif
#ifdef o_FHn                                                                           // * le cas du mOrdersTotal(OrderSymbol(), OrderStopLoss()) == 3 n'est pas traité correctement *
/*      if (EA_TP_secure && (OrderSymbol() == "XAUUSD" || OrderSymbol() == "DJ30") && AccountBalance() < 200.0 * lot / 0.01 && (mOrdersTotal(OrderSymbol(), OrderStopLoss()) % 2) > 0) EA_n_TP = 1;
      else EA_n_TP = EA_nb_TP; */        switch(EA_n_TP) {
         case 0  :
#endif
      if (round(ordprof*100.0)/100.0 > (dtmp=(n_tcks * lot * MarketInfo(OrderSymbol(), MODE_TICKVALUE))) && OrderType() < 2 && !((itmp=mOrdersTotal(OrderSymbol(), OrderStopLoss())) % 2)) {
         t_close = false;        cpt = 0;          tst = OrderSelect(ticket, SELECT_BY_TICKET);
         while (! t_close && (cpt < 4 || slip == 2)) { t_close = OrderClose(ticket, lot, OrderType()? MarketInfo(OrderSymbol(),MODE_ASK): MarketInfo(OrderSymbol(),MODE_BID), slip, clrNONE);
            if (cpt) { Sleep(5 * PAUSE);        RefreshRates(); }          cpt ++;
            if (slip == 2 && cpt == 4) { slip = 5;       cpt = 0; } }
         if (! t_close) Alert("***** Ticket NOT CLOSED! (2) " + ticket + " - " + GetLastError() + " *****");
         else { Sleep(2 * PAUSE);         pos --;
            if (OrderSelect(mOrderSelect(1, OrderSymbol(), !OrderType()), SELECT_BY_POS)) { ticket = OrderTicket();
               tst = OrderModify(ticket, OrderOpenPrice(), (OrderType() && LM < OrderOpenPrice() && LM > 0.0) || 
                  (! OrderType() && LM > OrderOpenPrice()) ? LM : OrderOpenPrice(), OrderTakeProfit(), 0, clrNONE);
Alert("*****2* (0) OrderSelect: "+IntegerToString(tst)+" - "+IntegerToString(ticket)+" - "+OrderCloseTime()+" - "+IntegerToString(OrderTicket())+" - "+DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS))+" *******");
             }// }
else Alert("*****3* (0) OrderSelect: " + IntegerToString(ticket) + " - " + OrderCloseTime() + " - " + mOrderSelect(1, OrderSymbol(), !OrderType()) + " - " + IntegerToString(OrderTicket()) + " - " +
   DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS)) + " *******"); }
if (FileIsExist(".\\Telegram\\Copy_Trade_On.txt", 0)) 
Alert("******* (0) +20pips: "+OrderTicket()+" - "+OrderSymbol()+" - "+OrderProfit()+" - "+DoubleToStr(MarketInfo(OrderSymbol(), MODE_TICKVALUE), MarketInfo(OrderSymbol(), MODE_DIGITS))+" - "+
   DoubleToStr(200 * MarketInfo(OrderSymbol(), MODE_TICKVALUE), MarketInfo(OrderSymbol(), MODE_DIGITS))+" - "+DoubleToStr(200.0 * MarketInfo(OrderSymbol(), MODE_TICKVALUE), MarketInfo(
   OrderSymbol(), MODE_DIGITS))+" - "+DoubleToStr(MarketInfo(OrderSymbol(), MODE_TICKSIZE), MarketInfo(OrderSymbol(), MODE_DIGITS))+" - "+DoubleToStr(200 * MarketInfo(OrderSymbol(), 
   MODE_TICKSIZE), MarketInfo(OrderSymbol(), MODE_DIGITS))+" - "+DoubleToStr(200.0 * MarketInfo(OrderSymbol(), MODE_TICKSIZE), MarketInfo(OrderSymbol(), MODE_DIGITS))+" - "+
   mOrdersTotal(OrderSymbol(), OrderStopLoss())+" | "+IntegerToString(pos)+" - "+IntegerToString(total)+" - "+OrderSelect(pos, SELECT_BY_POS)+" *******"); }
#ifdef o_FHn
         break;
         case 1  :      double TP1=GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(ticket), StringLen(IntegerToString(ticket)) - 4) + "_TP1");
      if (fabs(OrderStopLoss() - OrderOpenPrice()) > n_tcks * 2.0 / 3.0 * MarketInfo(OrderSymbol(), MODE_TICKSIZE) && OrderType() < 2#ifdef o_FH6 && EA_20pips #endif &&
      ((OrderType() && OrderStopLoss() > OrderOpenPrice()) || (! OrderType() && OrderStopLoss() < OrderOpenPrice())) &&
      (round(ordprof*100.0)/100.0 > (dtmp=(n_tcks * lot * MarketInfo(OrderSymbol(), MODE_TICKVALUE))) ||       // pour l'instant que dans l'option EA_nb_TP = 1
      (OrderType() && MarketInfo(OrderSymbol(), MODE_BID) < TP1) || (! OrderType() && MarketInfo(OrderSymbol(), MODE_ASK) > TP1 && TP1 > 0.0))) {
//         if (OrderSelect(mOrderSelect(1, OrderSymbol(), !OrderType()), SELECT_BY_POS)) { ticket = OrderTicket();
            tst = OrderModify(ticket, OrderOpenPrice(), (OrderType() && LM < OrderOpenPrice() && LM > 0.0) || 
               (! OrderType() && LM > OrderOpenPrice()) ? LM : OrderOpenPrice(), OrderTakeProfit(), 0, clrNONE);
Alert("*****2* (1) OrderSelect: "+IntegerToString(tst)+" - "+IntegerToString(ticket)+" - "+OrderCloseTime()+" - "+IntegerToString(OrderTicket())+" - "+DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS))+" *******");
             }// }
else if (fabs(OrderStopLoss() - OrderOpenPrice()) > n_tcks * 2.0 / 3.0 * MarketInfo(OrderSymbol(), MODE_TICKSIZE) &&
   ((OrderType() && OrderStopLoss() > OrderOpenPrice()) || (! OrderType() && OrderStopLoss() < OrderOpenPrice())) &&
   ((OrderType() && OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK) > n_tcks * 4.0 / 5.0 * MarketInfo(OrderSymbol(), MODE_TICKSIZE)) ||
   (! OrderType() && MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice() > n_tcks * 4.0 / 5.0 * MarketInfo(OrderSymbol(), MODE_TICKSIZE))))
Alert("*****3* (1) OrderSelect: " + IntegerToString(ticket) + " - " + OrderCloseTime() + " - " + mOrderSelect(1, OrderSymbol(), !OrderType()) + " - " + IntegerToString(OrderTicket()) + " - " +
   DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS)) + " - " + DoubleToStr(TP1, MarketInfo(OrderSymbol(), MODE_DIGITS)) + " *******");
         break;
         case 2  :      double TP2=GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP2"),
            TP3=GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP3");
      if (round(ordprof*100.0)/100.0 > (dtmp=(n_tcks * lot * MarketInfo(OrderSymbol(), MODE_TICKVALUE))) && OrderType() < 2 && !((itmp=mOrdersTotal(OrderSymbol(), OrderStopLoss())) % 2)) {
         t_close = false;        cpt = 0;          tst = OrderSelect(ticket, SELECT_BY_TICKET);
         while (! t_close && (cpt < 4 || slip == 2)) { t_close = OrderClose(ticket, lot, OrderType()? MarketInfo(OrderSymbol(),MODE_ASK): MarketInfo(OrderSymbol(),MODE_BID), slip, clrNONE);
            if (cpt) { Sleep(5 * PAUSE);        RefreshRates(); }          cpt ++;
            if (slip == 2 && cpt == 4) { slip = 5;       cpt = 0; } }
         if (! t_close) Alert("***** Ticket NOT CLOSED! (2) " + ticket + " - " + GetLastError() + " *****");
         else { Sleep(2 * PAUSE);         pos --;        ticket = 0;
            if (OrderSelect(mOrderSelect(1, OrderSymbol(), !OrderType()), SELECT_BY_POS)) { ticket = OrderTicket();
               tst = OrderModify(ticket, OrderOpenPrice(), (OrderType() && LM < OrderOpenPrice() && LM > 0.0) || 
                  (! OrderType() && LM > OrderOpenPrice()) ? LM : OrderOpenPrice(), TP3, 0, clrNONE);
Alert("*****2* (2) OrderSelect: "+IntegerToString(tst)+" - "+IntegerToString(ticket)+" - "+OrderCloseTime()+" - "+IntegerToString(OrderTicket())+" - "+DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS))+" *******");
             }// }
else Alert("*****3* (2) OrderSelect: " + IntegerToString(ticket) + " - " + OrderCloseTime() + " - " + mOrderSelect(1, OrderSymbol(), !OrderType()) + " - " + IntegerToString(OrderTicket()) + " - " +
   DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS))+" *******"); } }
      TP1 = ticket? GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(ticket), StringLen(IntegerToString(ticket)) - 4) + "_TP1"): 0.0;
      if (((OrderType() && MarketInfo(OrderSymbol(), MODE_BID) < TP2) || (! OrderType() && MarketInfo(OrderSymbol(), MODE_ASK) > TP2)) && ticket &&
      (OrderTakeProfit() != TP3 || OrderStopLoss() != TP1)) tst = OrderModify(ticket, OrderOpenPrice(), TP1, TP3, 0, clrNONE);
         break;
         case 3  :      TP2 = GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP2");
            TP3 = GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TP3");
      if (round(ordprof*100.0)/100.0 > (dtmp=(n_tcks * lot * MarketInfo(OrderSymbol(), MODE_TICKVALUE))) && OrderType() < 2 && !((itmp=mOrdersTotal(OrderSymbol(), OrderStopLoss())) % 2) &&
      OrderTakeProfit() != TP3) { bool done=GlobalVariableCheck(OrderSymbol() + "_" + DoubleToStr(OrderStopLoss()/MarketInfo(OrderSymbol(), MODE_DIGITS), 0)) &&
         (ticket_=GlobalVariableGet(OrderSymbol() + "_" + DoubleToStr(OrderStopLoss()/MarketInfo(OrderSymbol(), MODE_DIGITS), 0))) > 0 &&
         GlobalVariableCheck(OrderSymbol() + StringSubstr(IntegerToString(ticket_), StringLen(IntegerToString(ticket_)) - 4) + "_TPx");
         if (! done) {
            GlobalVariableSet(OrderSymbol() + StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4) + "_TPx", 1.0);
            GlobalVariableSet(OrderSymbol() + "_" + DoubleToStr(OrderStopLoss()/MarketInfo(OrderSymbol(), MODE_DIGITS), 0), OrderTicket()); }
//         if (OrderSelect(mOrderSelect(1, OrderSymbol(), !OrderType()), SELECT_BY_POS)) { ticket = OrderTicket();
            tst = OrderModify(ticket, OrderOpenPrice(), (OrderType() && LM < OrderOpenPrice() && LM > 0.0) || 
               (! OrderType() && LM > OrderOpenPrice()) ? LM : OrderOpenPrice(), done? TP2: TP3, 0, clrNONE);
Alert("*****2* (3) OrderSelect: "+IntegerToString(tst)+" - "+IntegerToString(ticket)+" - "+OrderCloseTime()+" - "+IntegerToString(OrderTicket())+" - "+DoubleToStr(TP2, MarketInfo(OrderSymbol(), MODE_DIGITS))+
   " - " + DoubleToStr(TP3, MarketInfo(OrderSymbol(), MODE_DIGITS)) + " - " + DoubleToStr(OrderTakeProfit(), MarketInfo(OrderSymbol(), MODE_DIGITS)) + " *******");
/*             }// }
else Alert("*****3* (3) OrderSelect: " + IntegerToString(ticket) + " - " + OrderCloseTime() + " - " + mOrderSelect(1, OrderSymbol(), !OrderType()) + " - " + IntegerToString(OrderTicket()) + " - " +
   DoubleToStr(LM, MarketInfo(OrderSymbol(), MODE_DIGITS))+" *******");*/ }
      TP1 = ticket? GlobalVariableGet(OrderSymbol() + StringSubstr(IntegerToString(ticket), StringLen(IntegerToString(ticket)) - 4) + "_TP1"): 0.0;
      if (((OrderType() && MarketInfo(OrderSymbol(), MODE_BID) < TP2) || (! OrderType() && MarketInfo(OrderSymbol(), MODE_ASK) > TP2)) && ticket &&
      OrderStopLoss() != TP1) tst = OrderModify(ticket, OrderOpenPrice(), TP1, OrderTakeProfit(), 0, clrNONE);
         break;
         case 4  :
         break;
         case 5  :
         break; }
#endif }

   while(Hour() == 22 && StringFind(Op_Symbs, ":" + IntegerToString(Day()-1)) > 0) Op_Symbs = StringSubstr(Op_Symbs, StringFind(Op_Symbs, ":" + IntegerToString(Day()-1)) + 8);
   while(Hour() == 22 && StringFind(Op_Symbs, ":|") > 0) Op_Symbs = StringSubstr(Op_Symbs, StringFind(Op_Symbs, ":|") + 2);
   while(Hour() == 22 && StringFind(Lm_Symbs, ":" + IntegerToString(Day()-1)) > 0) Lm_Symbs = StringSubstr(Lm_Symbs, StringFind(Lm_Symbs, ":" + IntegerToString(Day()-1)) + 8);
   while(Hour() == 22 && StringFind(Lm_Symbs, ":|") > 0) Lm_Symbs = StringSubstr(Lm_Symbs, StringFind(Lm_Symbs, ":|") + 2);
   if (Hour() == 22 && ! Minute() && Seconds() < 15 && ! Del_Reps) { Del_Reps = true;         Op_Symb2s = "";         FileDelete("Op_Symb2s_List.txt", 0);
      ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "date_time.bat", ">>logs\\Del_Reps_Trades.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1);
      ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram\\"+
         "Del_Reps_Trades.bat", ">>logs\\Del_Reps_Trades.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\Files\\Telegram", 1); }
   else if (Minute()) Del_Reps = false; }

void OnDeinit(const int reason) {
   if (Op_Symbs != "") { FileDelete("Op_Symbs_List.txt", 0);      Sleep(PAUSE);     copy_file(1, Op_Symbs); }
   if (Op_Symb2s != "") { FileDelete("Op_Symb2s_List.txt", 0);    Sleep(PAUSE);     copy_file(2, Op_Symb2s); }
   if (Lm_Symbs != "") { FileDelete("Lm_Symbs_List.txt", 0);      Sleep(PAUSE);     copy_file(3, Lm_Symbs); }
   EventKillTimer(); }

void OnTimer() { t_web ++;
   if (t_web >= 180) { ResetLastError();            // bool copy_FTP2() {
      int res=1, rs2, timeout=5000, ts, cpt=0;        char post[], result[];
#ifdef o_FH2
      string URL="http://www.ecomclub.com/transfert_VPS/FH2/",
#endif
#ifdef o_FH4
      string URL="http://www.ecomclub.com/transfert_VPS/FH4/",
#endif
#ifdef o_FH5
      string URL="http://www.ecomclub.com/transfert_VPS/FH5/",
#endif
#ifdef o_FH6
      string URL="http://www.ecomclub.com/transfert_VPS/FH6/",
#endif
         filename, headers;   //, terminal=arr_Terms[Terminal], admin=arr_Admins[Terminal];

      rs2 = 0;          res = WebRequest("GET", URL + "transfert.txt", NULL, NULL, timeout, post, 0, result, headers);              Sleep(2 * PAUSE);
      if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "OrderSend_Magic_Num_Ind.mq4", NULL, NULL, timeout, post, 0, result, headers);   else rs2 = 1;  Sleep(2 * PAUSE); }
      if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "OrderSend_Magic_Num.mq4", NULL, NULL, timeout, post, 0, result, headers);    else rs2 = 2;     Sleep(2 * PAUSE); }
      if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "Telegram_Gold-Cup.mq4", NULL, NULL, timeout, post, 0, result, headers);      else rs2 = 3;     Sleep(2 * PAUSE); }
//      if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "Telegram_Gold-Cup.mqh", NULL, NULL, timeout, post, 0, result, headers);      else rs2 = 4;     Sleep(2 * PAUSE); }   // v 5
      if (!rs2) { if (res<0||(res!=200&&res!=404)) Print("*** Error in WebRequest (1-"+IntegerToString(rs2)+"). Error code = ", GetLastError(), ", res = "+res, " ***");   if (res==200) rs2 = 4; }
      while(rs2) { PrintFormat("** The file has been successfully loaded ("+IntegerToString(rs2)+"), File size = %d bytes **", ArraySize(result));
         while(ts != 42 && cpt < 7) { if (StrToInteger(CharToString(result[0]))) filename = StringSubstr(CharArrayToString(result), 4);
            else  if (rs2 == 2) filename = "OrderSend_Magic_Num_Ind.mq4";  else  if (rs2 == 3) filename = "OrderSend_Magic_Num.mq4";
            else  if (rs2 == 4) filename = "Telegram_Gold-Cup.mq4";        else  if (rs2 == 5) filename = "Telegram_Gold-Cup.mqh";     else  filename = "WebResult.html";
            if (rs2 == 1) { Sleep(2 * PAUSE);      res = WebRequest("GET", URL + filename, NULL, NULL, timeout, post, 0, result, headers); }
            if (res < 0) Print("*** Error in WebRequest (" + filename + "). Error code = ", GetLastError(), " ***");
            else { PrintFormat("** The file has been successfully loaded ("+filename+"), File size = %d bytes **", ArraySize(result));
               if (StringSubstr(filename, StringLen(filename) - 4) == ".bat") filename = StringSubstr(filename, 0, StringLen(filename) - 4) + ".txt";
               int filehandle = FileOpen(filename, FILE_WRITE|FILE_BIN|FILE_COMMON);
               if (filehandle != INVALID_HANDLE) {
                  FileWriteArray (filehandle, result, 0, ArraySize(result));
                  FileClose(filehandle);        Sleep(5 * PAUSE);          if (rs2 == 1) { ts = 42;   if (StringSubstr(filename, StringLen(filename) - 4) == ".mqh") rs2 = 5; }
                  if (StringSubstr(filename, StringLen(filename) - 4) == ".txt") filename = StringSubstr(filename, 0, StringLen(filename) - 4) + ".bat";
                  if (rs2 == 2) ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\" +
                     "copy_file_i.bat", "OrderSend_Magic_Num_Ind.mq4 >>Logs\\copy_file_i.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4", 1);
                  if (rs2 == 3) ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\" +
                     "copy_file_s.bat", "OrderSend_Magic_Num.mq4 >>Logs\\copy_file_s.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4", 1);
                  if (rs2 == 4) ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\" +
                     "copy_file_e.bat", "Telegram_Gold-Cup.mq4 >>Logs\\copy_file_e.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4", 1);
                  if (rs2 == 5) ts = ShellExecuteW(0,"open","C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4\\" +
//                     "copy_file_h.bat", "Telegram_Gold-Cup.mqh >>Logs\\copy_file_h.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4", 1);
                     "copy_file_h.bat", filename + " >>Logs\\copy_file_h.txt", "C:\\Users\\Administra" + admin + "\\AppData\\Roaming\\MetaQuotes\\Terminal\\" + terminal + "\\MQL4", 1);
                  res = WebRequest("GET", URL + "file_delete.php?it=" + filename, NULL, NULL, timeout, post, 0, result, headers);
                  if (res < 0) Print("*** Error in WebRequest (2-"+(ts==42? IntegerToString(rs2): filename)+"). Error code = ", GetLastError(), " ***");
                  else PrintFormat("** The file has been successfully deleted ("+(ts==42? IntegerToString(rs2): filename)+"), File size = %d bytes **", ArraySize(result));
                  if (rs2 == 1 || rs2 == 5) { res = WebRequest("GET", URL + "file_delete.php?it=transfert.txt", NULL, NULL, timeout, post, 0, result, headers);
                     if (res < 0) Print("*** Error in WebRequest (2-"+(ts==42? IntegerToString(rs2): "transfert.txt")+"). Error code = ", GetLastError(), " ***");
                     else PrintFormat("** The file has been successfully deleted ("+(ts==42? IntegerToString(rs2): "transfert.txt")+"), File size = %d bytes **", ArraySize(result)); } }
               else Print("*** Error in FileOpen (" + filename + "). Error code=", GetLastError(), " ***"); }           cpt ++; }
         Sleep(5 * PAUSE);    ts = 0;     cpt = 0;    rs2 = 0;       res = WebRequest("GET", URL + "transfert.txt", NULL, NULL, timeout, post, 0, result, headers);     Sleep(2 * PAUSE);
         if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "OrderSend_Magic_Num_Ind.mq4", NULL, NULL, timeout, post, 0, result, headers);   else rs2 = 1;  Sleep(2 * PAUSE); }
         if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "OrderSend_Magic_Num.mq4", NULL, NULL, timeout, post, 0, result, headers);    else rs2 = 2;     Sleep(2 * PAUSE); }
         if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "Telegram_Gold-Cup.mq4", NULL, NULL, timeout, post, 0, result, headers);      else rs2 = 3;     Sleep(2 * PAUSE); }
//         if (!rs2) { if (res<0||res==404) res = WebRequest("GET", URL + "Telegram_Gold-Cup.mqh", NULL, NULL, timeout, post, 0, result, headers);      else rs2 = 4;     Sleep(2 * PAUSE); }   // v 5
         if (!rs2) { if (res<0||(res!=200&&res!=404)) Print("*** Error in WebRequest (3-"+IntegerToString(rs2)+"). Error code = ", GetLastError(), ", res = "+res, " ***");   if (res==200) rs2 = 4; } }
      t_web = 0; }
//   return(res >= 0);
}

string File_Next(bool mode,int nf,int n_rep) {
   string tel_dir=mode? "Telegram"+(n_rep? "_copied ("+IntegerToString(n_rep)+")": "")+"\\*": "Telegram\\*", filename;  // dans .\MQL4\Files
   int i=1, pos=0, last_pos=-1;     bool tst=false;

//--- get the search handle in the root of the local folder
   long search_handle=FileFindFirst(tel_dir, filename, 0);
   if (search_handle==INVALID_HANDLE) { Print("*** INVALID_HANDLE - "+tel_dir+" - "+filename+" ***");   return(""); }

//--- check if the FileFindFirst() is executed successfully
   if (search_handle!=INVALID_HANDLE) {
      //--- in a loop, check if the passed strings are the names of files or directories
      do { ResetLastError();
         //--- if it's a file, the function returns true, and if it's a directory, it returns error ERR_FILE_IS_DIRECTORY
         FileIsExist(StringSubstr(tel_dir, 0, StringLen(tel_dir)-1) + filename);
         if (GetLastError() == ERR_FILE_IS_DIRECTORY) i --;
//         PrintFormat("%d : %s name = %s", i, GetLastError()==ERR_FILE_IS_DIRECTORY? "Directory": "File", filename);
//         if (StringSubstr(filename, 0, StringLen(symbol)) == symbol) break;
         if (i == nf) { tst = true;     break; }    i ++; }    // i peut être utilisé comme numéro de fichier dans la liste

      while(FileFindNext(search_handle,filename));

//      if (!FileFindNext(search_handle, next)) filename = "last_file";
      if (! tst) filename = "last_file";

      //--- close the search handle
      FileFindClose(search_handle); }
   else { Print("Files not found!");   filename = "File not found(0)"; }

//   MessageBox(filename, NULL, MB_OK);
   return(filename); }

string line_read[][8];
int CSV_FILE_READ(int _row,int n_rep,string filename) {
   filename = ".\\Telegram_copied ("+IntegerToString(n_rep)+")\\" + filename;
   int row=_row, col=0, handle=FileOpen(filename,FILE_CSV|FILE_READ, " ");
   if(handle > 0) {
      while(True && col < 8) { string temp=FileReadString(handle);
#ifdef o_FHn
         temp = StringTrimLeft(StringTrimRight(temp));
         line_read[row][col] = temp != "" ? temp : ".";
#else
         line_read[row][col] = temp;
#endif
         if (FileIsEnding(handle)) break;
         if (FileIsLineEnding(handle)) {
            col = 0;       row ++; }
         else#ifdef o_FHn if (col < 7)#endif col ++; }      row ++;
      FileClose(handle); }
   else Alert("*** (2) File "+filename+" not found, the last error is ", GetLastError() + " ***");
   return(row); }

void copy_file(int Item,string temp) { int handle;
   string filename=Item? Item==1? "Op_Symbs_List.txt": Item==2? "Op_Symb2s_List.txt": "Lm_Symbs_List.txt": "macro_logs_copy.log";
   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) > 0) FileSeek(handle, FileSize(handle), 0);
      FileWrite(handle, temp); }
   else Print ("*** Problème : handle == INVALID_HANDLE. (2) / "+Symbol()+" / "+GetLastError()+" ***");
   FileClose(handle); }

int mOrdersTotal(string Symb,double SL) { int total=OrdersTotal(), mtotal=0;
  if (Symb == "" || Symb == NULL) return(mtotal);        datetime otime;

  for (int pos=0; pos < total; pos ++) {
      if (! OrderSelect(pos, SELECT_BY_POS)) continue;
      if (!mtotal && OrderSymbol()==Symb && OrderType() < 2 && OrderStopLoss()==0.0 && OrderMagicNumber()==mgc_num) otime = OrderOpenTime();
      if (OrderSymbol() == Symb && OrderType() < 2 && ((SL > 0.0 && SL == OrderStopLoss()) ||
         (SL == 0.0 && fabs(otime - OrderOpenTime()) < 17)) && OrderMagicNumber() == mgc_num) mtotal ++; }
  return(mtotal); }

int mOrderSelect(bool Mode,string Symb,bool Bull) { bool tst=false;
  if (Symb == "" || Symb == NULL) return(0);
  int total=OrdersTotal(), pos0=-1, ticket;        double PL0=0.0;

  for (int pos=0; pos < total; pos ++) {     // && ! tst
      if (! OrderSelect(pos, SELECT_BY_POS)) continue;         ticket = OrderTicket();
      if (OrderSymbol()==Symb && OrderMagicNumber()==mgc_num && (!Mode || (OrderStopLoss()!=OrderOpenPrice() && fabs(OrderStopLoss()-OrderOpenPrice()) > 130 * 
      MarketInfo(OrderSymbol(),MODE_TICKSIZE) && OrderType() == ! Bull && mOrdersTotal(OrderSymbol(), OrderStopLoss())))) {   // tst = true;
         tst = OrderSelect(ticket, SELECT_BY_TICKET);
         pos0 = PL0 < OrderProfit()? pos: pos0;          if (PL0 < OrderProfit()) PL0 = OrderProfit(); } }
  return(pos0); }

void BrokerDigitAdjust(string symbol) {
 int 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); }

string Symb_Check(string Symb,double LM) { lot = _lot;         ResetLastError();
   if (StringFind(Symb, "/") > 3) Symb = StringSubstr(Symb, 0, StringFind(Symb, "/"));    else if (StringFind(Symb, "/") > 0) StringReplace(Symb, "/", "");
   string s_comp=AccountCompany();
   int i_comp=StringSubstr(s_comp,0,5) == "Black"? 0: StringSubstr(s_comp,0,5) == "FX Ch"? 1: StringSubstr(s_comp,0,5) == "Trade"? 2: StringSubstr(s_comp,0,5) == "Vanta"? 3: 4;
   switch(i_comp) {
         case 0  :
      if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         Symb += "p";         if (Symb == "XBRUSDp") Symb = "BRENT";
      if (Symb=="XTIUSDp" || Symb=="USOILp") if (LM > 0.0) Symb = fabs((MarketInfo("WTI",MODE_BID)+MarketInfo("WTI",MODE_ASK))/2 - LM) > 
         fabs((MarketInfo("BRENT",MODE_BID)+MarketInfo("BRENT",MODE_ASK))/2 - LM) ? "BRENT" : "WTI";     else  Symb = "WTI";
      if (StringSubstr(Symb, 0, 3) == "GNP") Symb = "GBP" + StringSubstr(Symb, 3);        if (Symb == "US30p") { Symb = "US30";     lot = 0.1; }
         break;
         case 1  :
      if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         if (Symb == "XBRUSD") Symb = "BRENT";           // Symb += "p";
      if (Symb=="XTIUSD" || Symb=="USOIL") if (LM > 0.0) Symb = fabs((MarketInfo("WTI",MODE_BID)+MarketInfo("WTI",MODE_ASK))/2 - LM) > 
         fabs((MarketInfo("BRENT",MODE_BID)+MarketInfo("BRENT",MODE_ASK))/2 - LM) ? "BRENT" : "WTI";     else  Symb = "WTI";
      if (StringSubstr(Symb, 0, 3) == "GNP") Symb = "GBP" + StringSubstr(Symb, 3);        if (Symb == "US30") { Symb = "US30";     lot = 0.1; }
         break;
         case 2  :
         break;
         case 3  :
      if (Symb == "GOLD" || Symb == "Gold") Symb = "XAUUSD";         if (Symb == "XBRUSD") Symb = "BRENT";           // Symb += "p";
      if (Symb=="XTIUSD" || Symb=="USOIL") if (LM > 0.0) Symb = fabs((MarketInfo("WTI",MODE_BID)+MarketInfo("WTI",MODE_ASK))/2 - LM) > 
         fabs((MarketInfo("BRENT",MODE_BID)+MarketInfo("BRENT",MODE_ASK))/2 - LM) ? "BRENT" : "WTI";     else  Symb = "WTI";
      if (StringSubstr(Symb, 0, 3) == "GNP") Symb = "GBP" + StringSubstr(Symb, 3);        if (Symb == "US30") { Symb = "DJ30";     lot = 0.1; }
      if (Symb == "NASdaq100") { Symb = "NAS100";     lot = 0.1; }         if (Symb == "CRUDE") Symb = "CL-OIL";
         break; }

      if (port) { port = false;
         for (int pos=0; pos < OrdersTotal() && ! port; pos ++) port = (OrderSelect(pos, SELECT_BY_POS) && Symb == OrderSymbol() && mgc_num == OrderMagicNumber()); }
Alert("******* " + s_comp + " - " + IntegerToString(i_comp) + " - " + port + " *******");          _p = i_comp ? "" : "p";
 return(Symb); }
