#property indicator_chart_window

datetime CP_tim, tim_CP;         int bar;       bool Buy, Sell, CP=false;     string str;

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

int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
   const double &open[], const double &high[], const double &low[], const double &close[],
   const long &tick_volume[], const long &volume[], const int &spread[]) { 

   if (TimeCurrent() - tim_CP > 55 && (! (TimeCurrent() % 60) || (FileIsExist("Chart_Symbol_Set (5).txt", FILE_COMMON) &&
   FileGetInteger("Chart_Symbol_Set (5).txt", FILE_MODIFY_DATE, FILE_COMMON) > tim_CP)) && ! CP && Get_Cash_Power()) {// tim_CP = TimeCurrent();
      StringReplace(str, "-> ", "| ");       StringReplace(str, "*** ", "");
      StringReplace(str, "! ***", "");       StringReplace(str, " - ", " | ");
      copy_file(str); }

   if (GlobalVariableCheck(Symbol() + "_" + IntegerToString(Period()) + "_WindowScreenShot")) { ResetLastError();
      if (! WindowScreenShot("Charts_Volat_Chk_" + Symbol() + "_" + IntegerToString(Period()) + "_" + IntegerToString(Hour()) + IntegerToString(Minute()), 800, 600, -1, -1, -1))
         copy_file_("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event (3) : " + Symbol() + " - " + IntegerToString(Period()) + " - Pb. " +
            "WindowScreenShot() - " + IntegerToString(GetLastError()));          GlobalVariableDel(Symbol() + "_" + IntegerToString(Period()) + "_WindowScreenShot"); }

   return(rates_total); }

void OnTimer() { if (Get_Cash_Power()) {// tim_CP = TimeCurrent();
      StringReplace(str, "-> ", "| ");       StringReplace(str, "*** ", "");
      StringReplace(str, "! ***", "");       StringReplace(str, " - ", " | ");
      copy_file(str); } }

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

int copy_file(string temp) { int handle, it=0;
   bool tst=false;         string filename="CP_List_Sent_" + Symbol() + "_" + Period() + ".txt";
   while (! tst && it < 7) {/* if (! FileIsExist(filename, FILE_COMMON))*/ handle = FileOpen (filename, FILE_TXT|FILE_WRITE|FILE_COMMON);
//      else  handle = FileOpen (filename, FILE_TXT|FILE_READ|FILE_WRITE|FILE_COMMON);
      if (handle != INVALID_HANDLE) {// if (FileSize(handle)) FileSeek(handle, FileSize(handle), 0);
         tst = FileWrite(handle, temp);
         if (tst) {/* Print("*** VALIDÉ : FileWrite(handle, (" + it + ") / " + Symbol() + " ***");     */ tim_CP = TimeCurrent(); }
         else  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); }

int copy_file_(string filename,string temp) { int handle, it=0;
   bool tst=false;//         filename = StringSubstr(filename, 0, StringFind(filename, ".")) + ".txt";
   while (! tst && it < 7) { if (! FileIsExist(filename, FILE_COMMON)) handle = FileOpen (filename, FILE_TXT|FILE_WRITE|FILE_COMMON);
      else  handle = FileOpen (filename, FILE_TXT|FILE_READ|FILE_WRITE|FILE_COMMON);
      if (handle != INVALID_HANDLE) { if (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); }

datetime Get_Cash_Power() { bool tst=false;        datetime secs=TimeCurrent();      int j=0, k=0;       CP = true;
   Buy = false;   Sell = false;     for (bar = 1; bar < 300 && ! tst; bar ++) {// Comment("* bar = " + IntegerToString(bar) + " *");
      if (tst=((Sell=(iCustom(NULL, 0, "New folder\\Ultimate Cashpower Manual Trade Version.ex4", 0, bar) > 0.0)) ||
      (Buy=(iCustom(NULL, 0, "New folder\\Ultimate Cashpower Manual Trade Version.ex4", 1, bar) > 0.0)))) {// CreateVLine(Time[bar], Period(), Buy, 0);
         str = (! FileIsExist("CP_List_Sent_" + Symbol() + "_" + Period() + ".txt", FILE_COMMON)? "*0* ": "*** ") +
            "Manual Trade Cashpower Lifetime -> " + Symbol() + " - " + (Buy? "Buy": "Sell") + " - " + IntegerToString(bar) + " - " + Time[bar] + " - " +
            TimeToStr(Time[bar], TIME_DATE|TIME_MINUTES) + "! ***";              if (CP_tim < Time[bar]) Print(str);
         copy_file_("Charts_Volat_Chk_Events.txt", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + " - Event CP1 : " + Symbol() + " - " + IntegerToString(Period()) + " - " + (Buy? "Buy": "Sell")); }
      Sleep(50);           if (bar > 10 && (TimeCurrent() - secs) / bar >= 3.0) break; }
   if (tst) Comment("* bar = " + IntegerToString(bar-1) + " / wa=" + (bar-1 > 0? "a+": "w") + "&sb=" + Symbol() + "&tf=" + IntegerToString(Period()) +
         "&tp=" + (Buy? "Buy": "Sell") + "&stm=" + TimeToStr(Time[bar-1], TIME_DATE|TIME_MINUTES) + "&tim=" + IntegerToString(Time[bar-1]) + " *");
   else { Comment("*** Rien trouvé ! ***");           copy_file("*0* Manual Trade Cashpower Lifetime -> " + Symbol() + " - NO - 300 - 0 - " +
         TimeToStr(0, TIME_DATE|TIME_MINUTES) + "! ***"); }                         CP = false;
   if (FileIsExist("ATS_Cash_Power Ind nxt1.txt", FILE_COMMON)) FileMove("ATS_Cash_Power Ind nxt1.txt", FILE_COMMON, "ATS_Cash_Power Ind Ok.txt", FILE_COMMON);
   if (FileIsExist("ATS_Cash_Power Ind nxt2.txt", FILE_COMMON)) FileMove("ATS_Cash_Power Ind nxt2.txt", FILE_COMMON, "ATS_Cash_Power Ind Ok.txt", FILE_COMMON);
   if (FileIsExist("ATS_Cash_Power Ind next.txt", FILE_COMMON)) FileMove("ATS_Cash_Power Ind next.txt", FILE_COMMON, "ATS_Cash_Power Ind Ok.txt", FILE_COMMON);
   return(tst && (bar < 5 || ! FileIsExist("CP_List_Sent_" + Symbol() + "_" + Period() + ".txt", FILE_COMMON) || CP_tim < Time[bar-1])? (CP_tim=Time[bar-1]): 0); }

/* datetime Get_Cash_Power(int mode) { bool Buy_=Buy, Sel_=Sell, tst=false;      datetime secs=TimeCurrent();      int j=0, k=0;
   Buy = false;   Sell = false;     for (int bar=0; bar < 200 && (! tst || mode); bar ++) {// Comment("* bar = " + IntegerToString(bar) + " *");
      if (tst=((Sell=(iCustom(NULL, 0, "New folder\\Manual Trade Cashpower Lifetime.ex4", 0, bar) > 0.0)) ||
      (Buy=(iCustom(NULL, 0, "New folder\\Manual Trade Cashpower Lifetime.ex4", 1, bar) > 0.0)))) {// CreateVLine(Time[bar], Period(), Buy, 0);
if (CP_tims[1] != Time[bar]) { tim4 = TimeCurrent();           ticket_ = 0;         t_box = true;        t_P60 = true;                    // * 19-04-2022 : 12h à 19h *
Alert("*** Manual Trade Cashpower Lifetime -> " + Symbol() + " - " + (Buy? "Buy": "Sell") + " - " + IntegerToString(bar) + " - " + TimeToStr(Time[bar], TIME_DATE|TIME_MINUTES) + "! ***");
/*         if (mode) { CP_tims[k][0] = Time[bar];       CP_tims[k][1] = Sell;         j = k;         k ++; }* /       if (bar < 3) CP_tims[3] = Time[bar]; } }
      Sleep(50);        if (bar > 10 && (TimeCurrent() - secs) / bar >= 3.0) break; }           if (bar == 1 || (! tst && ! mode)) { Buy = Buy_;         Sell = Sel_; }
/*   if (tst || (mode && CP_tims[j][0])) Comment("* bar = " + IntegerToString(bar) + " / wa=" + ((mode && j) || bar? "a+": "w") + "&sb=" + Symbol() + "&tf=" + IntegerToString(Period()) +
         "&tp=" + (Buy? "Buy": "Sell") + "&stm=" + TimeToStr(mode? CP_tims[j][0]: Time[bar], TIME_DATE|TIME_MINUTES) + "&tim=" + IntegerToString(Time[bar]) + " *");
   else  Comment("*** Rien trouvé ! ***");* /         return(mode? 0: tst? (CP_tims[1]=Time[bar-1]): 0); } */
