Bug? MQLInfoInteger(MQL_OPTIMIZATION) appears not to be working

 

I run an EA in the Strategy Tester in Optimization Mode. From OnTesterPass() I open a CSV file, that should have a different name depending on whether it's the Optimisation phase or Forward phase. I use MQLInfoInterger() to find this out. But even though OnTesterPass could only be triggered when optimising, MQLInfoInteger(MQL_OPTIMIZATION)  is false. See code below. Has anyone seen this before? Idea ideas?

When it's run in optimisation mode or forward mode, the file name end up like this: TEST_Opt_0_Forw_0_Tester_0.csv

////////////////////////////////////////////////////////////////////////////////////////////////
void OnTesterPass () {
   CreateOptimizationReport();
}
////////////////////////////////////////////////////////////////////////////////////////////////
void CreateOptimizationReport () {
   static int passes_count=0;                // Pass counter
   int        parameters_count=0;            // Number of Expert Advisor parameters
   string     string_to_write="";            // String for writing
   string     name            ="";           // Public name/frame label
   ulong      pass            =0;            // Number of the optimization pass at which the frame is added
   long       id              =0;            // Public id of the frame
   double     value           =0.0;          // Single numerical value of the frame
   string     parameters_list[];             // List of the Expert Advisor parameters of the "parameterN=valueN" form
   string     parameter_names[];             // Array of parameter names

   passes_count++;
   FrameNext(pass,name,id,value,stat_values);
   FrameInputs(pass,parameters_list,parameters_count);

   //--- At the first pass, generate the optimization report file with headers
   if (passes_count == 1) {
      string headers="PassNo,MaxSequenceLevels,R2,PipsPerTrade,TradesPerWeek,AvgHoldingHours,OptimizationMinTradesPerWeek,OptimizationMaxTradesPerWeek,OptimizationMaxAvgHoldingHours,OptimizationMinProfitFactor,OptimizationMaxSequenceLevels,OptimizationMinR2,OptimizationMinPipsPerTrade";   
      
      string fileName;
      string strategyDescription = "TEST";
      if (MQLInfoInteger(MQL_OPTIMIZATION) && !MQLInfoInteger(MQL_FORWARD)) fileName = "PB/"+strategyDescription+"_back.csv";
      else if (MQLInfoInteger(MQL_FORWARD)) fileName = "PB/"+strategyDescription+"_forward.csv";
      else if (MQLInfoInteger(MQL_TESTER)) fileName = "PB/"+strategyDescription+"_single.csv";
      else fileName = "PB/"+strategyDescription+"_Opt_"+MQLInfoInteger(MQL_OPTIMIZATION)+"_Forw_"+MQLInfoInteger(MQL_FORWARD)+"_Tester_"+MQLInfoInteger(MQL_TESTER)+".csv";
      
      OptimizationFileHandle = FileOpen(fileName, FILE_CSV|FILE_READ|FILE_WRITE|FILE_ANSI|FILE_COMMON,",");
      
      if (OptimizationFileHandle != INVALID_HANDLE) {
         FileWrite(OptimizationFileHandle, headers);
      }
   }
   for(int i=0; i<STAT_VALUES_COUNT; i++) {
      StringAdd(string_to_write, DoubleToString(stat_values[i],2)+",");
   }
   
   WriteOptimizationResults(string_to_write);
}

 
I'm using MT5 build 4620
 
fxsaber #:

Hi, sorry but I am not sure what you mean with this. Just because FRAME_MODE is true, does that means that OPTIMIZATION or FORWARD will not true? I have several functions in the EA that is using those variables, for example to avoid creating the panel when optimising.
So when OnTesterPass() executes, how can I find out if it's currently in the OPTIMIZATION phase or the FORWARD phase?

 
Goesta Torsten Hulden #:

Hi, sorry but I am not sure what you mean with this. Just because FRAME_MODE is true, does that means that OPTIMIZATION or FORWARD will not true? I have several functions in the EA that is using those variables, for example to avoid creating the panel when optimising.
So when OnTesterPass() executes, how can I find out if it's currently in the OPTIMIZATION phase or the FORWARD phase?

When you click the Start button in the Tester, two copies of the advisor are launched - in the Tester (OPTIMIZATION == true) and in the Terminal (FRAME_MODE == true).
 
fxsaber #:

So when in OnTesterPass(), is there a way to find out whether I am currently in the OPTIMIZATION phase or the FORWARD phase?

Cause even if FRAME_MODE is true, we will be in one of those phases, so I am surprised that they would both show false. The doc says that FRAME_MODE and TESTER can not be true at the same time, and OPTIMISATION and VISUAL can't be true at the same time, but I haven't seen anything saying that FRAME_MODE and OPTIMIZATION can't be true at the same time.

 
Goesta Torsten Hulden #:

Hi, sorry but I am not sure what you mean with this. Just because FRAME_MODE is true, does that means that OPTIMIZATION or FORWARD will not true?

You can find more details in the docs or the algotrading book:


MT5 tester modes

MQL5 Book: Trading automation / Testing and optimization of Expert Advisors / Auto-tuning: ParameterGetRange and ParameterSetRange
MQL5 Book: Trading automation / Testing and optimization of Expert Advisors / Auto-tuning: ParameterGetRange and ParameterSetRange
  • www.mql5.com
In the previous section, we learned how to pass an optimization criterion to the tester. However, we missed one important point. If you look into...
 
Goesta Torsten Hulden #:

So when in OnTesterPass(), is there a way to find out whether I am currently in the OPTIMIZATION phase or the FORWARD phase?

Cause even if FRAME_MODE is true, we will be in one of those phases, so I am surprised that they would both show false. The doc says that FRAME_MODE and TESTER can not be true at the same time, and OPTIMISATION and VISUAL can't be true at the same time, but I haven't seen anything saying that FRAME_MODE and OPTIMIZATION can't be true at the same time.

If FRAME_MODE is true, you are running an optimization by definition, there is no need to check it.

To know if a pass come from forward or "normal" optimization, pass it with your frame data.

 
Alain Verleyen #:

If FRAME_MODE is true, you are running an optimization by definition, there is no need to check it.

To know if a pass come from forward or "normal" optimization, pass it with your frame data.

Thank you Alain, I tried to do what you describe, but so far I have not got it to work.

In OnTester() I set one of the values in the array to MQLInfoInteger(MQL_FORWARD) before the array is passed with a new frame.

In OnTesterPass() I receive the value. But it is false for frames coming both from OPTIMIZATION and from FORWARD. I print all the frames to a CSV file together with a timestamp that let's me know if it came from OPT or FORWARD, and isForward is false for all frames.

A pointer to what I might be doing wrong would be very appreciated.

Thanks

double OnTester() {

   // stat_values is an array of double
   stat_values[0] = maxSeqNo;
   stat_values[1] = r4;
   stat_values[2] = pipsPerTrade;
   stat_values[3] = tradesPerWeek;
   stat_values[4] = avgHoldingHours;
   stat_values[5] = OptimizationMinTradesPerWeek;
   stat_values[6] = OptimizationMaxTradesPerWeek;
   stat_values[7] = OptimizationMaxAvgDuration;
   stat_values[8] = OptimizationMinProfitFactor;
   stat_values[9] = OptimizationMaxSequenceLevels;
   stat_values[10] = OptimizationMinR2;
   stat_values[11] = OptimizationMinPipsPerTrade;
   stat_values[12] = MQLInfoInteger(MQL_FORWARD);
   
   FrameAdd("Statistics",1,0,stat_values);

   ...

   return(0);
}



void OnTesterPass () {
   ...

   FrameNext(pass,name,id,value,stat_values);
   FrameInputs(pass,parameters_list,parameters_count);
   bool isForward = (bool)stat_values[12];
   
   ...
}
 
Goesta Torsten Hulden #:

Thank you Alain, I tried to do what you describe, but so far I have not got it to work.

In OnTester() I set one of the values in the array to MQLInfoInteger(MQL_FORWARD) before the array is passed with a new frame.

In OnTesterPass() I receive the value. But it is false for frames coming both from OPTIMIZATION and from FORWARD. I print all the frames to a CSV file together with a timestamp that let's me know if it came from OPT or FORWARD, and isForward is false for all frames.

A pointer to what I might be doing wrong would be very appreciated.

Thanks

 
 
Alain Verleyen #:

 

Hi Alain, I assume you showed me that pic to show that it works, and that is great news. But it doesn't tell me much about how to do it, or what I have done wrong.

Any chance you could share the code that produced that print? That would be fantastic.

Thanks