How to get Local Time while using strategy tester

 

Hello friends

I want to know how can I get LocalTime() Or TimeCurrent()  while using strategy tester.

As Default during testing in the Strategy Tester, TimeCurrent() is simulated according to historical data.

I need the time that are not refers to historical data. 

is there any DLL to get current computer Time or server Time?

Operation of Programs in the Strategy Tester - MQL4 programs - MQL4 Reference
Operation of Programs in the Strategy Tester - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Operation of Programs in the Strategy Tester - MQL4 programs - MQL4 Reference
 
 

Hello 

That was a brilliant idea.

Thank you

 

the correct way to do so is this :

datetime GetTimeLocal()
{ 
  static const string Name = "time local";

  if (MQLInfoInteger(MQL_TESTER))
  {
   if (GlobalVariableTemp(Name) == true)
   {   
      GlobalVariableDel(Name);
      GlobalVariableSet(Name,"000");
      return GlobalVariableTime(Name);
   }
   else
   {
      GlobalVariableSet(Name,"000");
      return GlobalVariableTime(Name);
   }
  }
  
  return 0;
}
 
// Returns TimeLocal even in Tester
#include <WinAPI\sysinfoapi.mqh>
datetime GetTimeLocal()
  {
   if(MQLInfoInteger(MQL_TESTER))
     {
      SYSTEMTIME   system_time;
      GetLocalTime(system_time);

      MqlDateTime st;
      st.year     = system_time.wYear;
      st.mon      = system_time.wMonth;
      st.day      = system_time.wDay;
      st.hour     = system_time.wHour;
      st.min      = system_time.wMinute;
      st.sec      = system_time.wSecond;
      return StructToTime(st);
     }
   else
     {
      return TimeLocal();
     }
  }

int OnInit()
  {
   Print("Real TimeLocal: ", GetTimeLocal());
   return (INIT_SUCCEEDED);
  }

DLL imports must be enabled.


 
Jean Francois Le Bas #:

the correct way to do so is this :

No need to perform so complicated tricks. GlobalVariableTime returns a time of any latest access, so simplify to:

datetime GetTimeLocal()
{ 
  static const string name = "_time_local_";

  GlobalVariableTemp(name); // use temporary global variable instead of persistent!
  GlobalVariableGet(name);  // reading is enough to change the timestamp
  ResetLastError();         // clean up possible error code (valid situation if the variable does already exist)
  return GlobalVariableTime(name);
}

Also please note that global variables store double-s, so you should not call GlobalVariableSet(Name, "000") with a string.

 
amrali #:

DLL imports must be enabled.

Why DLLs if there exists a way without DLLs?

 
Stanislav Korotky # :

Why DLLs if there exists a way without DLLs?

Still doesn't work with MT5.

This topic while started as an MQL4 one, is on an MQL5 section which can lead to some confusion.

 
Alain Verleyen #:

Still doesn't work with MT5.

This topic while started as an MQL4 one, is on an MQL5 section which can lead to some confusion.

Oh, yes, forgot about this, I moved to the temporary file solution:

datetime GetTimeLocal()
{ 
  static const string name = "_time_local_";

  int h = FileOpen(name, FILE_WRITE);
  datetime tmp = (datetime)FileGetInteger(h, FILE_ACCESS_DATE);
  FileClose(h);
  FileDelete(name);

  return tmp;
}

Still no DLLs.

 
Stanislav Korotky #:

Oh, yes, forgot about this, I moved to the temporary file solution:

Still no DLLs.

Nice and portable