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

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

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

void OnStart()
{  // prévoir de remettre cette ligne dans le OnInit()
   if (! GlobalVariableCheck(name1)) GlobalVariableSet(name1, 0);
//Print("*** trace 1 - "+DoubleToStr(c_run,0)+" ***");
   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 - "+DoubleToStr(c_run,0)+" ***");
      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, "("));
         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, "(", "");
         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]+" - "+parts[13]+" *";
               Print (temp);        copy_file (temp);
               GlobalVariableSet(name1, dTime);
         }  }    i ++; }
      FileClose(handle);
   }
   else Print("* SuperIndicatorParser Error - "+IntegerToString(_LastError)+" - "+IntegerToString(GetLastError())+" - "+err_mess+" *");
}

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);
}
