Get now timestamp during backtest

 

Hi everybody,

I need to get the current datetime in a string, possibly in a SQL format (e.g. 2019-04-07 15:37:02).

I've tried three methods of MQL5 standard library: 

  • TimeCurrent
  • TimeGMT
  • TimeLocal

but during backtest they all return the same date that is not the actual date.

I need to get the current datime (not an historical datetime referred to backtest).

Do you have any advices?

Thanks in advance, 

Robert

 
arsenico42: I need to get the current datime (not an historical datetime referred to backtest).

Perhaps you should read the manual.
  1. During testing in the strategy tester, TimeCurrent() is simulated according to historical data.
              Date and Time / TimeCurrent - Reference on algorithmic/automated trading language for MetaTrader 5

  2. Simulation of Time in the Strategy Tester

    During testing, the local time TimeLocal() is always equal to the server time TimeTradeServer(). In turn, the server time is always equal to the time corresponding to the GMT time - TimeGMT(). This way, all of these functions display the same time during testing.

    The lack of a difference between the GMT, the Local, and the server time in the Strategy Tester is done deliberately in case there is no connection to the server. The test results should always be the same, regardless of whether or not there is a connection. Information about the server time is not stored locally, and is taken from the server.

              MQL5 programs / Testing Trading Strategies - Reference on algorithmic/automated trading language for MetaTrader 5
 

Option 1: global variable

datetime currentTick = iTime(_Symbol,0,0);


2020.12.02 01:12:00

Option 2: create a testfile and get the timestamp of file creation

   string tmpFileName      = "_TEMP_FILE";
   int handleTmpFile       = FileOpen(tmpFileName, FILE_WRITE); if(handleTmpFile==INVALID_HANDLE);
   datetime now            = (datetime)FileGetInteger(handleTmpFile, FILE_CREATE_DATE);
   MqlDateTime nowMql;
   TimeToStruct(now,nowMql);
   FileClose(handleTmpFile);
   FileDelete(tmpFileName);

I found Option 2 once in this forum...