//extern bool o_Script =  false;
//extern bool tot_Forex = false;
//extern int tot_Charts = 4;
extern int tim_Charts = 5;

string Symbs="";        bool t_mod=false;
int tot_Charts=4, i_=0, totCharts=tot_Charts, im;        datetime t_tim;

int OnInit() { EventSetTimer(tim_Charts * 60);        return(INIT_SUCCEEDED); }

void OnTick() { if (! (TimeCurrent() % (tim_Charts * 60))) {
      t_mod = true;        Symbs = "";       i_ = 0;        totCharts = tot_Charts; }

  if (t_mod || (! OrdersTotal() && TimeCurrent() - t_tim > 120)) {
   string sep="|", temp, tmp, line_read[];            ushort u_sep=StringGetCharacter(sep, 0);

   int fl=4;      string Symb, Symb_;     if (Symbs == "") {
/*      while(FileIsExist("Charts_Volatility_Check-b"+IntegerToString(fl)+".txt", FILE_COMMON)) {
         CSV_FILE_READ("Charts_Volatility_Check-b"+IntegerToString(fl)+".txt");
         for (int i=0; i < ArraySize(line_read_); i ++) if (line_read_[i] != "" && line_read_[i] != NULL &&
            Symb != line_read_[i]) { Symbs += line_read_[i] + "|";         Symb = line_read_[i]; }
         fl += 4; }
      CSV_FILE_READ("Charts_Volatility_Check-c.txt"); */

      CSV_FILE_READ("Charts_Volatility_Check (2).txt");
      for (int i=0; i < ArraySize(line_read_); i ++) if (line_read_[i] != "" && line_read_[i] != NULL &&
         Symb != line_read_[i]) { Symbs += line_read_[i] + "|";         Symb = line_read_[i]; } }
//if (t_mod && ! (TimeCurrent() % 60)) Alert("*** " + ArraySize(line_read_) + " - " + Symbs + " ***");

   int k=StringSplit(Symbs, u_sep, line_read);        tmp = " | ";      im = k - 1;

   long prevChart=ChartFirst();        int per, cpt=0;      string Symb2s="";
   while(cpt < 25) { if ((per=ChartPeriod(prevChart)) < PERIOD_D1 && per &&
      Symb != ChartSymbol(prevChart)) { Symb = ChartSymbol(prevChart);   // ChartIndicatorsTotal(prevChart, 0)
         Symb2s += ChartSymbol(prevChart) + " | "; }
      prevChart = ChartNext(prevChart);         cpt ++; }            cpt = 0;

   temp = " ";       for (int pos=0; pos < OrdersTotal(); pos ++)
      if (OrderSelect(pos, SELECT_BY_POS) && StringFind(temp, OrderSymbol()) < 0) temp += OrderSymbol() + " | ";

   int cpt2=0, it=0;       i = i_;        prevChart = ChartFirst();
      while(cpt < 25 && i < fmin(k - 1, totCharts)) { while(it < 4) { bool tst=false;
         if ((per=ChartPeriod(prevChart)) > PERIOD_M15 && per < PERIOD_D1) { Symb = ChartSymbol(prevChart);
            if (! it && ! i) while((StringFind(temp, StringTrimRight(StringTrimLeft(line_read[i]))) > 0 ||
                  StringFind(Symb2s, StringTrimRight(StringTrimLeft(line_read[i]))) > 0) && i < k - 1) i ++;
//            Symb = StringTrimRight(StringTrimLeft(line_read[i]));
            for (pos = 0; pos < OrdersTotal() && ! tst; pos ++) tst = (OrderSelect(pos, SELECT_BY_POS) && Symb == OrderSymbol());
            if (! tst) { if (StringFind(Symbs, Symb) < 0 || i == i_ || (i > i_ && (Symb_ == Symb || StringFind(Symb2s, Symb) >= 0)) ||
               (it < 2 && per != PERIOD_M30) || (it == 2 && per != PERIOD_H1) || (it == 3 && per != PERIOD_H4)) { if (i < k - 1) {
//Alert("******* (0): " + Symb + " - " + it + " - " + i + " - " + StringTrimRight(StringTrimLeft(line_read[i])) + " - " + ChartPeriod(prevChart) + " *******");
                     StringReplace(Symb2s, Symb, StringTrimRight(StringTrimLeft(line_read[i])));         if (! it) Symb_ = Symb;
                     if (StringFind(Symbs, Symb) < 0 || (it < 2 && per != PERIOD_M30) || (it == 2 && per != PERIOD_H1) || (it == 3 && per != PERIOD_H4)) {
                        ChartSetSymbolPeriod(prevChart, StringTrimRight(StringTrimLeft(line_read[i])), it < 2? PERIOD_M30: it == 2? PERIOD_H1: PERIOD_H4);     Sleep(200);
                        ChartSaveTemplate(prevChart, "Charts_Volat_Chk_" + StringTrimRight(StringTrimLeft(line_read[i])) + "_" + IntegerToString(per));/*        ResetLastError();
                        if (! WindowScreenShot("Charts_Volat_Chk_" + StringTrimRight(StringTrimLeft(line_read[i])) + "_" + IntegerToString(per), 800, 600, -1, -1, -1))
                           copy_file("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event (3) : " + StringTrimRight(StringTrimLeft(line_read[i])) + " - Pb. " +
                              "WindowScreenShot() - " + IntegerToString(GetLastError()), FILE_COMMON, true); */
                        copy_file("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event 3 : " + StringTrimRight(StringTrimLeft(line_read[i])), FILE_COMMON, true);
                        GlobalVariableSet(StringTrimRight(StringTrimLeft(line_read[i])) + "_" + IntegerToString(per) + "_WindowScreenShot", 1.0); }
                     else  copy_file("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event 3c : " +
                           StringTrimRight(StringTrimLeft(line_read[i])) + " - " + StringFind(Symbs, Symb) + " - " + it + " - " + per, FILE_COMMON, true);
/*                     if (StringFind(AccountCompany(), "Eightcap") >= 0) ChartApplyTemplate(prevChart, "temp ATS - Cashpower ++.tpl");
                     else  ChartApplyTemplate(prevChart, ! it? "temp ATS - CP - EPP + RSI - M30.tpl": it == 1? "temp ATS - CP - EPP + RSI - M30b.tpl":
                           it == 2? "temp ATS - CP - EPP + RSI - H1.tpl": "temp ATS - CP - EPP + RSI - H4.tpl");        Sleep(200); */
                     if (it == 3) i ++; }
                  else  break; }          else { copy_file("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event 3b : " +
                        StringTrimRight(StringTrimLeft(line_read[i])) + " - " + StringFind(Symbs, Symb) + " - " + i + " - " + i_ + " - " + Symb_ + " - " + Symb + " - " +
                        it + " - " + per, FILE_COMMON, true);        if (per == PERIOD_H4) { it = 3;   i ++; } } }
               else { copy_file("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event 3a : " +
                     StringTrimRight(StringTrimLeft(line_read[i])) + " - " + tst, FILE_COMMON, true);             if (per == PERIOD_H4) { it = 3;    i ++; } }
            while(! tst && cpt2 < 200) { Sleep(100);           cpt2 ++;
               tst = (StringFind(Symbs, ChartSymbol(prevChart)) > 0 || ChartSymbol(prevChart) == StringTrimRight(StringTrimLeft(line_read[i-1])) ||
                  ChartSymbol(prevChart) == StringTrimRight(StringTrimLeft(line_read[i]))); } }
         prevChart = ChartNext(prevChart);            cpt2 = 0;         int cpt3=0;
         while(! ChartPeriod(prevChart) && cpt2 < 200) { while(! ChartPeriod(prevChart) && cpt3 < 200) { Sleep(100);         cpt3 ++; }
            Sleep(100);          cpt2 ++;          if (! ChartPeriod(prevChart)) prevChart = ChartNext(prevChart); }
         it ++; }          it = 0;        cpt ++; }         t_mod = false;       i_ = i;        totCharts += tot_Charts;

      t_tim = TimeCurrent(); } }

void OnDeinit(const int reason) { EventKillTimer(); }

void OnTimer() { t_mod = true;         Symbs = "";       i_ = 0;       totCharts = tot_Charts; }

string line_read_[5];
bool CSV_FILE_READ(string filename) { string filenam2= StringSubstr(filename, 0, StringFind(filename, ".")) +
      IntegerToString(Period())+"-"+StringSubstr(Symbol(), 0, 1)+StringSubstr(Symbol(), 2, 1)+StringSubstr(Symbol(), 3, 1)+StringSubstr(Symbol(), 5, 1)+".txt";
   if (FileIsExist(filenam2, FILE_COMMON)) FileDelete(filenam2, FILE_COMMON);             Sleep(150);
   FileCopy(filename, FILE_COMMON, filenam2, FILE_COMMON);        Sleep(250 + (Seconds() <= 5? 150: 0));       filename = filenam2;
   int col=0, handle=FileOpen(filename, FILE_CSV|FILE_READ|FILE_COMMON, "|");
   if(handle > 0) { while(True) { string temp=StringTrimLeft(StringTrimRight(FileReadString(handle)));
         line_read_[col] = temp;         if (FileIsEnding(handle)) break;         col ++; }         FileClose(handle); }
   else Alert("*** File "+filename+" not found, the last error is ", GetLastError() + " ***");
   if (FileIsExist(filenam2, FILE_COMMON)) FileDelete(filenam2, FILE_COMMON);
   return(handle > 0); }

//       StringInit(str, fmax(line_size[i], StringLen(DoubleToStr(dtmp, 0))) - fmin(line_size[i], StringLen(DoubleToStr(dtmp, 0))), ' ');
//       temp += DoubleToStr(dtmp, 0) + (line_size[i] > StringLen(DoubleToStr(dtmp, 0))? str: "") + " | "; }

int copy_file(string filename,string temp,bool filc,bool add) { int handle, it=0;
   bool tst=false;//         filename = StringSubstr(filename, 0, StringFind(filename, ".")) + ".txt";
   while (! tst && it < 7) { if (filc) handle = FileOpen (filename, add? FILE_TXT|FILE_READ|FILE_WRITE|FILE_COMMON:
         FILE_TXT|FILE_WRITE|FILE_COMMON);         else  handle = FileOpen (filename, FILE_TXT|FILE_WRITE);
      if (handle != INVALID_HANDLE) { if (add && FileSize(handle)) FileSeek(handle, FileSize(handle), 0);
         tst = FileWrite(handle, temp);
         if (! tst) Print ("Problème : FileWrite(handle, ("+it+" - "+IntegerToString(GetLastError())+") / "+Symbol()+" ***");
         FileClose(handle); }
      else Print ("Problème : handle == INVALID_HANDLE. ("+it+" - "+IntegerToString(GetLastError())+") / "+Symbol()+" ***");     it ++; }
   return(handle); }
