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.
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.
Forum on trading, automated trading systems and testing of trading strategies
Features of the mql5 language, subtleties and working techniques
Stanislav Korotky , 2018.10.17 21:31
Sorry, I confused it with MT4. It works in MT4, but not MT5.
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.
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.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?