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...

 

I found this useful when using the debugger on historical trading cases with Strategy Tester:


A typical case is noticing an interesting trading case unfolding at a specific time and you want to analyze how your trading strategy code best handles it to accomplish some profit.

Let's say something interesting is beginning to happen at
2024 11 19 at 18:30:00 in the historical data chart.
In this case you would specify a date range in Strategy Tester such as 2024-11-19 to 2024-11-21

   // Provide a debugger breakpoint to stop at
   // a given time in the trading range where a trading
   // case needs to be examined in detail. 

   datetime now=TimeCurrent();
   datetime here=D'2024.11.19 18:30:00';

   if (now >= here) {
     bool match=true;  // Stop at 11:45:00 !
   }

then in MetaEditor you launch debugging:
Debug->Start on History Data

This causes launch of Strategy Tester in the Debugger
beginning in 2024-11-19 at 00:00.00
and as soon as tester time has reached 2024-11-19 at 11:45:00
the breakpoint will trigger.

Then simply place a breakpoint on the match=true line where you can begin further debugging
at 11:45:00 in the historical data range:

 
Gunnar Forsgren #:

I found this useful when using the debugger on historical trading cases with Strategy Tester:


A typical case is noticing an interesting trading case unfolding at a specific time and you want to analyze how your trading strategy code best handles it to accomplish some profit.

Let's say something interesting is beginning to happen at
2024 11 19 at 18:30:00 in the historical data chart.

and then simply place a breakpoint on the match=true line where you can begin further debugging
beginning from 11:45:00   :
Off-topic. This topic is about getting the real current time when a backtest is running, not an historical time which is "current" inside a backtest.