How to get today date ? , even backtest history still know today date. - page 2

 
Fernando Carreiro:

OP wants to get local time while using the Strategy Tester, hence why the Win32 API suggestion.

Obviously, when not using the Strategy Tester, but on a Live chart, you don't need to use the Win32 API.

I mean you can get local time from the tester as well, and there's no need to deal with DLLs. But OP wrote:

Siwakon Poonsawat:

l want to get real today date ,like check from broker.

So, I suppose he wants server time.

 
Stanislav Korotky: I mean you can get local time from the tester as well, and there's no need to deal with DLLs. But OP wrote:

So, I suppose he wants server time.

If you see is posted images, you see that he wants the real local time, not the server time.

However, you say one can get the local time without Win32 API even in the Strategy Tester, but you still have not stated how to do that!

 
Fernando Carreiro:

If you see is posted images, you see that he wants the real local time, not the server time.

However, you say one can get the local time without Win32 API even in the Strategy Tester, but you still have not stated how to do that!


As I quoted above, OP wants broker time not local time.

To get local time in the tester one can use the following workaround:

/**
 * Get current local time, UTC (GMT zone), according to PC clock
 */
datetime getCurrentUTC()
{
  string tmpgv = "_UTC_TMP_GLOBAL_VARIABLE_";
  datetime now = GlobalVariableSet(tmpgv, 0);
  GlobalVariableDel(tmpgv);
  return now;
}

int OnInit()
{
  Print("Started at ", TimeToString(getCurrentUTC()));
  return INIT_SUCCEEDED;
}
 
Stanislav Korotky: To get local time in the tester one can use the following workaround:

Interesting and unconventional way to do it, but quite imaginative! Thanks!

 

Sorry, Stanislav, but using your getCurrentUTC() method in the Strategy Tester I still get the latest simulated date and not the current date of the PC. 

Can anyone confirm this is working for him/her?

 
Marcel Fitzner:

Sorry, Stanislav, but using your getCurrentUTC() method in the Strategy Tester I still get the latest simulated date and not the current date of the PC. 

Can anyone confirm this is working for him/her?

My computer time was 11:56 today (2019.10.19), Stanislav code returned :

2019.10.19 11:56:07.884    2019.01.21 00:00:00  215383 EURUSD,M5: Started at 2019.10.19 15:56

My time zone is GMT-4 currently, so it's current GMT time as announced (but certainly not "latest simulate date" as you stated). Using TimeGMTOffset() you can easily get current local time.

Or alternatively :

//+------------------------------------------------------------------+
//| Get current local (computer) time                                |
//+------------------------------------------------------------------+
datetime getCurrentLocalTime()
  {
   string tmpFilename   = "_TEMP_FILE_TO_GET_CURRENT_LOCAL_TIME";
   int handle           = FileOpen(tmpFilename, FILE_WRITE); if(handle==INVALID_HANDLE) return(0);
   datetime now         = (datetime)FileGetInteger(handle, FILE_CREATE_DATE);
   FileClose(handle);
   FileDelete(tmpFilename);
//---
   return(now);
  }
 

Ok, I've run the method from @Stanislav Korotky's, the variation suggested by @Alain Verleyen which includes the TimeGMTOffset() method and the file-operation method:

// IMPORTANT HINT: THIS IS MT5 code (sorry for posting it here, noticed too  late I'm in the wrong forum!)

datetime getCurrentUTC(bool printDebug = false)
{
  string tmpgv = "_UTC_TMP_GLOBAL_VARIABLE_";
  datetime now = GlobalVariableSet(tmpgv, 0);
  GlobalVariableDel(tmpgv);
  if (printDebug)
        Print("Current UTC: " + TimeToString(now));
  return now;
}

datetime getCurrentSystemTime(bool printDebug = false)
{
        Print("GMT offset: " + IntegerToString(TimeGMTOffset()));
        
        datetime systemTime = getCurrentUTC() + TimeGMTOffset();

        if (printDebug)
                Print("Current UTC + GMT offset: " + TimeToString(systemTime));
        
        return systemTime;
}

datetime getCurrentLocalTime(bool printDebug = false)
{
        string tmpFilename   = "_TEMP_FILE_TO_GET_CURRENT_LOCAL_TIME";
        int handle           = FileOpen(tmpFilename, FILE_WRITE); if (handle == INVALID_HANDLE) return 0;
        datetime now         = (datetime)FileGetInteger(handle, FILE_CREATE_DATE);
        FileClose(handle);
        FileDelete(tmpFilename);
        
        if (printDebug)
        Print("Time obtained from file operation: " + TimeToString(now));
        
        return now;
}


In live chart I obtain the following times:


In Strategy Tester / backtesting, starting the simulation from 01.01.2019,  I obtain these times:


For those of us who need the actual time even during backtesting (e.g. because the VIX symbol from January 2019 is no more available), only the file operation delivers the current system time.

Cheers!

 
Marcel Fitzner:

Ok, I've run the method from @Stanislav Korotky's, the variation suggested by @Alain Verleyen which includes the TimeGMTOffset() method and the file-operation method:


In live chart I obtain the following times:


In Strategy Tester / backtesting, starting the simulation from 01.01.2019,  I obtain these times:


For those of us who need the actual time even during backtesting (e.g. because the VIX symbol from January 2019 is no more available), only the file operation delivers the current system time.

Cheers!

1° Are you posting MT5 results on the MT4 forum ? Please never do that, you are misleading readers. MT5 results are different (as you said only File approach is working).

2° Your code has a bug :

datetime getCurrentSystemTime(bool printDebug = false)
{
        Print("GMT offset: " + IntegerToString(TimeGMTOffset()));
        
        datetime systemTime = getCurrentUTC() +  - TimeGMTOffset();  // Needs to be - and not +

        if (printDebug)
                Print("Current UTC + GMT offset: " + TimeToString(systemTime));
        
        return systemTime;
}

3° As I already said, BOTH approach are working with MT4.

Your code (fixed) on a live chart :

2019.11.16 15:50:55.592    215383_18 EURUSD,H1: Time obtained from file operation: 2019.11.16 15:50
2019.11.16 15:50:55.592    215383_18 EURUSD,H1: Current UTC - GMT offset: 2019.11.16 15:50
2019.11.16 15:50:55.592    215383_18 EURUSD,H1: GMT offset: 18000
2019.11.16 15:50:55.592    215383_18 EURUSD,H1: Current UTC: 2019.11.16 20:50

On a backtest :

2019.11.16 15:51:50.511    2019.01.21 00:00:00  215383_18 EURUSD,M5: Time obtained from file operation: 2019.11.16 15:51
2019.11.16 15:51:50.511    2019.01.21 00:00:00  215383_18 EURUSD,M5: Current UTC - GMT offset: 2019.11.16 15:51
2019.11.16 15:51:50.511    2019.01.21 00:00:00  215383_18 EURUSD,M5: GMT offset: 18000
2019.11.16 15:51:50.511    2019.01.21 00:00:00  215383_18 EURUSD,M5: Current UTC: 2019.11.16 20:51
Reason: