Download MetaTrader 5

Strategy Tester Using Optimization Uses Wrong Dates and Times on subequent passes

To add comments, please log in or register
Marc Dixon
32
Marc Dixon  

Hi there,

I have an EA that I've been trying to back test with inconsistent results using the Optimizer.

If I use the settings that come back from the Optimizer and just run a single pass I get different results.

I decided to add some logging to a file in order to try and figure it out.  The first pass when using Optimization works as expected and the same as if I ran just using a single pass.  However, the subequent passes are filled with non-existent or incorrect dates and times and the log balloons out.  However, at the end of the subsequent pass it suddenly reverts to legitimate dates and times.

Period = 27 Feb 2017 - 2 Mar 2017

### First Pass
2017.02.27 00:00:18 - OnInit - Testing has begun. IsOptimization = true
2017.02.27 00:00:18 - lastBarTime == NULL, setting to 2017.02.27 00:00:00
2017.02.27 04:00:00 - lastBarTime has changed. Was 2017.02.27 00:00:00, setting to 2017.02.27 04:00:00
2017.02.27 08:00:00 - lastBarTime has changed. Was 2017.02.27 04:00:00, setting to 2017.02.27 08:00:00
2017.02.27 12:00:00 - lastBarTime has changed. Was 2017.02.27 08:00:00, setting to 2017.02.27 12:00:00
2017.02.27 16:00:00 - lastBarTime has changed. Was 2017.02.27 12:00:00, setting to 2017.02.27 16:00:00
2017.02.27 20:00:00 - lastBarTime has changed. Was 2017.02.27 16:00:00, setting to 2017.02.27 20:00:00
2017.02.28 00:00:00 - lastBarTime has changed. Was 2017.02.27 20:00:00, setting to 2017.02.28 00:00:00
2017.02.28 04:00:00 - lastBarTime has changed. Was 2017.02.28 00:00:00, setting to 2017.02.28 04:00:00
2017.02.28 08:00:00 - lastBarTime has changed. Was 2017.02.28 04:00:00, setting to 2017.02.28 08:00:00
2017.02.28 12:00:00 - lastBarTime has changed. Was 2017.02.28 08:00:00, setting to 2017.02.28 12:00:00
2017.02.28 16:00:00 - lastBarTime has changed. Was 2017.02.28 12:00:00, setting to 2017.02.28 16:00:00
2017.02.28 20:00:00 - lastBarTime has changed. Was 2017.02.28 16:00:00, setting to 2017.02.28 20:00:00
2017.03.01 00:00:00 - lastBarTime has changed. Was 2017.02.28 20:00:00, setting to 2017.03.01 00:00:00
2017.03.01 04:00:00 - lastBarTime has changed. Was 2017.03.01 00:00:00, setting to 2017.03.01 04:00:00
2017.03.01 08:00:00 - lastBarTime has changed. Was 2017.03.01 04:00:00, setting to 2017.03.01 08:00:00
2017.03.01 12:00:00 - lastBarTime has changed. Was 2017.03.01 08:00:00, setting to 2017.03.01 12:00:00
2017.03.01 16:00:00 - lastBarTime has changed. Was 2017.03.01 12:00:00, setting to 2017.03.01 16:00:00
2017.03.01 20:00:00 - lastBarTime has changed. Was 2017.03.01 16:00:00, setting to 2017.03.01 20:00:00
2017.03.02 00:00:00 - lastBarTime has changed. Was 2017.03.01 20:00:00, setting to 2017.03.02 00:00:00
2017.03.02 04:00:00 - lastBarTime has changed. Was 2017.03.02 00:00:00, setting to 2017.03.02 04:00:00
2017.03.02 08:00:00 - lastBarTime has changed. Was 2017.03.02 04:00:00, setting to 2017.03.02 08:00:00
2017.03.02 12:00:00 - lastBarTime has changed. Was 2017.03.02 08:00:00, setting to 2017.03.02 12:00:00
2017.03.02 16:00:00 - lastBarTime has changed. Was 2017.03.02 12:00:00, setting to 2017.03.02 16:00:00
2017.03.02 20:00:00 - lastBarTime has changed. Was 2017.03.02 16:00:00, setting to 2017.03.02 20:00:00

### Second Pass
  - OnInit - Testing has begun. IsOptimization = true
  - lastBarTime == NULL, setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
2029.11.19 12:26:50 - lastBarTime has changed. Was  , setting to  
2024.06.10 02:35:18 - lastBarTime has changed. Was  , setting to  
2024.06.10 02:35:18 - lastBarTime has changed. Was  , setting to  
2013.07.21 06:52:15 - lastBarTime has changed. Was  , setting to  
2013.07.21 06:52:15 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
2017.11.28 00:21:28 - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
  - lastBarTime has changed. Was  , setting to  
2017.02.27 07:21:50 - lastBarTime has changed. Was  , setting to 2017.02.27 04:00:00
2017.02.27 08:00:00 - lastBarTime has changed. Was 2017.02.27 04:00:00, setting to 2017.02.27 08:00:00
2017.02.27 12:00:00 - lastBarTime has changed. Was 2017.02.27 08:00:00, setting to 2017.02.27 12:00:00
2017.02.27 16:00:00 - lastBarTime has changed. Was 2017.02.27 12:00:00, setting to 2017.02.27 16:00:00
2017.02.27 20:00:00 - lastBarTime has changed. Was 2017.02.27 16:00:00, setting to 2017.02.27 20:00:00
2017.02.28 00:00:00 - lastBarTime has changed. Was 2017.02.27 20:00:00, setting to 2017.02.28 00:00:00
2017.02.28 04:00:00 - lastBarTime has changed. Was 2017.02.28 00:00:00, setting to 2017.02.28 04:00:00
2017.02.28 08:00:00 - lastBarTime has changed. Was 2017.02.28 04:00:00, setting to 2017.02.28 08:00:00
2017.02.28 12:00:00 - lastBarTime has changed. Was 2017.02.28 08:00:00, setting to 2017.02.28 12:00:00
2017.02.28 16:00:00 - lastBarTime has changed. Was 2017.02.28 12:00:00, setting to 2017.02.28 16:00:00
2017.02.28 20:00:00 - lastBarTime has changed. Was 2017.02.28 16:00:00, setting to 2017.02.28 20:00:00
2017.03.01 00:00:00 - lastBarTime has changed. Was 2017.02.28 20:00:00, setting to 2017.03.01 00:00:00
2017.03.01 04:00:00 - lastBarTime has changed. Was 2017.03.01 00:00:00, setting to 2017.03.01 04:00:00
2017.03.01 08:00:00 - lastBarTime has changed. Was 2017.03.01 04:00:00, setting to 2017.03.01 08:00:00
2017.03.01 12:00:00 - lastBarTime has changed. Was 2017.03.01 08:00:00, setting to 2017.03.01 12:00:00
2017.03.01 16:00:00 - lastBarTime has changed. Was 2017.03.01 12:00:00, setting to 2017.03.01 16:00:00
2017.03.01 20:00:00 - lastBarTime has changed. Was 2017.03.01 16:00:00, setting to 2017.03.01 20:00:00
2017.03.02 00:00:00 - lastBarTime has changed. Was 2017.03.01 20:00:00, setting to 2017.03.02 00:00:00
2017.03.02 04:00:00 - lastBarTime has changed. Was 2017.03.02 00:00:00, setting to 2017.03.02 04:00:00
2017.03.02 08:00:00 - lastBarTime has changed. Was 2017.03.02 04:00:00, setting to 2017.03.02 08:00:00
2017.03.02 12:00:00 - lastBarTime has changed. Was 2017.03.02 08:00:00, setting to 2017.03.02 12:00:00
2017.03.02 16:00:00 - lastBarTime has changed. Was 2017.03.02 12:00:00, setting to 2017.03.02 16:00:00
2017.03.02 20:00:00 - lastBarTime has changed. Was 2017.03.02 16:00:00, setting to 2017.03.02 20:00:00


Does anyone know what might be causing this and how I would fix it?

The main function that I have writing to the log file is one that detects when a new bar has formed :

bool IsNewBar(string currencyPair, ENUM_TIMEFRAMES timeFrame)
{
   bool result = false;
   static datetime lastBarTime = NULL;
   
   if (lastBarTime == NULL || iTime(currencyPair, timeFrame, 0) != lastBarTime)
   {
      // ### Log to file for testing purposes only
      if (lastBarTime == NULL) {
         WriteToTestLog("lastBarTime == NULL, setting to "
            + TimeToString(iTime(currencyPair, timeFrame, 0), TIME_DATE)
            + " " + TimeToString(iTime(currencyPair, timeFrame, 0), TIME_SECONDS)
         );
      }
      else {
         WriteToTestLog("lastBarTime has changed. Was "
            + TimeToString(lastBarTime, TIME_DATE)
            + " " + TimeToString(lastBarTime, TIME_SECONDS)
            + ", setting to "
            + TimeToString(iTime(currencyPair, timeFrame, 0), TIME_DATE)
            + " " + TimeToString(iTime(currencyPair, timeFrame, 0), TIME_SECONDS)
         );
      }
      
      lastBarTime = iTime(currencyPair, timeFrame, 0);
      result = true;
   }
   return result;
}
whroeder1
17084
whroeder1  
NULL is not a datetime.

You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.

Zero is the same as PERIOD_CURRENT which means _Period.

No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[]

To add comments, please log in or register