//#property strict
#include "..\include\SuperIndicatorParser.mqh"

string err_mess="",name1="Last-Order",name2="Last-Size",name3="Last-File";
bool cp_ok,cp_ok_;         int c_run,err_flag;
double vPoint=0.0,vAsk=0.0,vBid=0.0,LastOrder,LastSize,LastFile;

int OnInit() { EventSetTimer(5);
   if (! GlobalVariableCheck(name1)) GlobalVariableSet(name1, 0);

   err_flag = 1;     c_run = 2;
//   if (! GlobalVariableCheck("c_run")) 
   GlobalVariableSet("c_run", c_run);
   return(0); }

void OnTick()
{  if (! (TimeCurrent() % 5)) { c_run = 2;      GlobalVariableSet("c_run", c_run); }
   else  c_run = int (GlobalVariableGet("c_run"));
   if (c_run < 2) {
      if (c_run) { Alert ("FIN du 'SuperIndicatorParser_EA' ! **");
         c_run = 0;   GlobalVariableSet("c_run", c_run); }
      return; }
   else { c_run = 0;    GlobalVariableSet("c_run", c_run);

//Print("*** trace 1 - "+IntegerToString(c_run)+" ***");
   SuperIndicatorParser parser;
   if(parser.parse()){
      if (parser.Total()) Print("*** SuperIndicatorParser parser.Total() - "+IntegerToString(parser.Total())+" ***");
      for(int i=parser.Total()-1; i>=0; i--){
         Signal *s = parser[i];
//         int ticket = OrderSend(
           string temp = "* SuperIndicatorParser -"+s.symbol+"-"+IntegerToString(s.order_type)+"-"+DoubleToStr(s.price_entry,5)+"-"+DoubleToStr(s.price_sl,5)+"-"+DoubleToStr(s.price_tp,5)+"*";
           copy_file(temp);      Print (temp);
//         if(ticket < 0){
//            Print(_LastError);
//         }
      }

//test général AUDNZDp,M15: Table parts 0 -> IL *
//test général AUDNZDp,M15: Table parts 1 -> 0 *
//test général AUDNZDp,M15: Table parts 2 -> 00:00:21.981 *
//test général AUDNZDp,M15: Table parts 3 -> FSU_Signals *
//test général AUDNZDp,M15: Table parts 4 -> (USDJPY,M30) *
//test général AUDNZDp,M15: Table parts 5 -> Alert: *
//test général AUDNZDp,M15: Table parts 6 -> FS *
//test général AUDNZDp,M15: Table parts 7 -> 4.01 *
//test général AUDNZDp,M15: Table parts 8 -> Ultimate *
//test général AUDNZDp,M15: Table parts 9 -> PERIOD_M30 *
//test général AUDNZDp,M15: Table parts 10 -> USDJPY *
//test général AUDNZDp,M15: Table parts 11 -> SELL *
//test général XRPUSD,H1: 0 00:00:09.984 ATS Assistant 2.1 XRPUSD,H1: Alert: ATS Assistant 2.1: ATS ASSISTANT ALERT: (GRADE B, LONG, EURNZD, M5, Trigger: MN EXP) MARKET SNAPSHOT: TS: L3, DEV:0, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.24 01:00:00, Price:1.80253
//test général XRPUSD,H1: Table parts -> 32 *
//test général XRPUSD,H1: Table parts 0 -> 0 *
//test général XRPUSD,H1: Table parts 1 -> 00:00:09.984 *
//test général XRPUSD,H1: Table parts 2 -> ATS *
//test général XRPUSD,H1: Table parts 3 -> Assistant *
//test général XRPUSD,H1: Table parts 4 -> 2.1 *
//test général XRPUSD,H1: Table parts 5 -> XRPUSD,H1: *
//test général XRPUSD,H1: Table parts 6 -> Alert: *
//test général XRPUSD,H1: Table parts 7 -> ATS *
//test général XRPUSD,H1: Table parts 8 -> Assistant *
//test général XRPUSD,H1: Table parts 9 -> 2.1: *
//test général XRPUSD,H1: Table parts 10 -> ATS *
//test général XRPUSD,H1: Table parts 11 -> ASSISTANT *
//test général XRPUSD,H1: Table parts 12 -> ALERT: *
//test général XRPUSD,H1: Table parts 13 -> (GRADE_B,_LONG,_EURNZD,_M5,_Trigger:_MN_EXP) *
//test général XRPUSD,H1: Table parts 14 -> MARKET *
//test général XRPUSD,H1: Table parts 15 -> SNAPSHOT: *
//test général XRPUSD,H1: Table parts 16 -> TS: *
//test général XRPUSD,H1: Table parts 17 -> L3, *
//test général XRPUSD,H1: Table parts 18 -> DEV:0, *
//test général XRPUSD,H1: Table parts 19 -> TF1:H4, *
//test général XRPUSD,H1: Table parts 20 -> TF2:D1, *
//test général XRPUSD,H1: Table parts 21 -> TF3:W1, *
//test général XRPUSD,H1: Table parts 22 -> Time:2023.07.24 *
//test général XRPUSD,H1: Table parts 23 -> 01:00:00, *
//test général XRPUSD,H1: Table parts 24 -> Price:1.80253 *
//test général XRPUSD,H1: Fichier copié -> 51 *

//Print("*** trace 2 - "+IntegerToString(c_run)+" ***");
      int i=0, handle, k;     double dTime;
      LastOrder = GlobalVariableGet(name1);
      handle = FileOpen ("copy_log.log",FILE_TXT|FILE_READ);
      while (! FileIsEnding(handle) && i < 300) {
         string temp = FileReadString(handle);
         StringReplace(temp, "\t", " ");
         string tmp=StringSubstr(temp, StringFind(temp, "("), StringFind(temp, ")") + 1 - StringFind(temp, "(")), tmp2=tmp;
         StringReplace(tmp, " ", "_");
         StringReplace(temp, StringSubstr(temp, StringFind(temp, "("), StringFind(temp, ")") + 1 - StringFind(temp, "(")), tmp);
         string parts[], part2s[];        ArrayResize(parts, 24, 2);       ArrayResize(part2s, 5, 1);
         k = StringSplit(temp, ' ', parts);
         StringReplace(tmp, "_", " ");    StringReplace(tmp, ")", "");     StringReplace(tmp, "(", "");
         StringReplace(tmp, ", ", ",");      StringSplit(tmp, ',', part2s);
         if (parts[2] == "ATS" && parts[3] == "Assistant") {
            MqlDateTime log_time;//, local;
            TimeToStruct(StringToTime(parts[1]), log_time);
//            TimeLocal(local);
//            log_time.year = local.year;
//            log_time.mon = local.mon;
//            log_time.day = local.day;
            datetime time = StructToTime(log_time);
//            datetime time2 = StructToTime(local);
            temp = StringSubstr(parts[1],StringFind(parts[1],".")+1);
//            Alert ("* Last Order Time is "+IntegerToString(time)+temp+" - "+IntegerToString(time2)+" *");   return;
            dTime = floor(StringToDouble(IntegerToString(time)+temp));
            if (LastOrder < dTime && k > 10) {
//             vPoint = MarketInfo(OrderSymbol(),MODE_POINT);
               vAsk = MarketInfo(parts[10],MODE_ASK);
               vBid = MarketInfo(parts[10],MODE_BID);
//               int ticket = OrderSend(parts[10], (parts[11]=="SELL"?1:0), 0.1, (parts[11]=="SELL"?vBid:vAsk), 5, 0.0, 0.0);
//               if (ticket > 0)
//               temp = "* SuperIndicatorParser -"+IntegerToString(time)+"-"+parts[1]+"-"+parts[10]+"-"+parts[11]+"-"+DoubleToStr((parts[11]=="SELL"?vBid:vAsk),5)+"-0.0-0.0*";
               temp = "* SuperIndicatorParser - "+IntegerToString(time)+" - "+TimeToStr(time, TIME_DATE|TIME_MINUTES)+" - "+parts[1]+" - "+parts[10]+" - "+parts[11]+" - "+tmp2+" *";
               Print(temp);         copy_file(temp);  /*
(GRADE A, SHORT, GBPAUD, M30, Trigger: MN EXP) MARKET SNAPSHOT: TS: L2, DEV:0, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 01:00:00, Price:1.90307
(GRADE A, LONG, CADJPY, M30, Trigger: MJ EXP STK) MARKET SNAPSHOT: TS: L0, DEV:0, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 02:00:00, Price:107.482
(GRADE A, LONG, CADJPY, M30, Trigger: LL) MARKET SNAPSHOT: TS: S0, DEV:11.7, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 05:00:00, Price:107.311
+  GRADE A - Trigger: MJ EXP  +  GRADE A - Trigger: EXP IND
(MJ BOX EXP, CADJPY, M30) MARKET SNAPSHOT: TS: L0, DEV:0, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 02:00:00, Price:107.482
(MN BOX EXP, AUDNZD, M30) MARKET SNAPSHOT: TS: S6, DEV:0, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 03:00:00, Price:1.08603
(Buy LL, CADJPY, M30) MARKET SNAPSHOT: TS: S0, DEV:11.8, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 05:00:00, Price:107.310
(Sell LL, CADJPY, M30) MARKET SNAPSHOT: TS: S0, DEV:0, TF1:H4, TF2:D1, TF3:W1, Time:2023.07.25 08:00:00, Price:107.486  +  EXP IND
part2s[0] = "GRADE A/B";     part2s[1] = "SHORT/LONG";   part2s[2] = "GBPAUD";   part2s[3] = "M30";   part2s[4] = "Trigger: ---";
part2s[0] = "MJ BOX EXP";    part2s[1] = "GBPAUD";       part2s[2] = "M30"; */
               
               GlobalVariableSet(name1, dTime);
               c_run = 0;     GlobalVariableSet("c_run", c_run);
         }  }    i ++; }
      FileClose(handle); }
   else Print("* SuperIndicatorParser Error - "+IntegerToString(_LastError)+" - "+IntegerToString(GetLastError())+" - "+err_mess+" *");
 } }

void OnTimer() { c_run = 2;      GlobalVariableSet("c_run", c_run); }

void OnDeinit(const int reason) { EventKillTimer();
   c_run = 2;   GlobalVariableSet("c_run", c_run);
Alert ("FIN du 'SuperIndicatorParser_EA' ! ** "+IntegerToString(reason));
   return; }

void copy_file(string tmp) { int handle;
   string filename="EA-write-file_Orders.txt";
   if (!FileIsExist(filename)) handle = FileOpen (filename,FILE_TXT|FILE_WRITE);
   else   handle = FileOpen (filename,FILE_TXT|FILE_READ|FILE_WRITE);
   if (handle != INVALID_HANDLE) {
       if (FileSize(handle)) FileSeek(handle, FileSize(handle), 0);
       FileWrite(handle, tmp); }
//   else Print ("Problčme : handle == INVALID_HANDLE. (2) / "+vSymbol+" ***");
   FileClose(handle);
}
