Extend Strategy tester results in a csv file

 

Hi all,

is it posible to write the results of an optimization in a csv file?, and add some statistical informatio ( Avg. trade, Avg. lossing trade..., MAE/MFE), Is there any sample code out there?.

Regards,

Vince. 

 

Thanks WHRoeder,

 Vince. 

 
coiler:

Thanks WHRoeder,

 Vince. 

Hi,

i am returning on this, how can i detect that is the first optimitation pass to empty the stats csv file?. I read this threads:

https://forum.mql4.com/51592#702738

https://www.mql5.com/en/articles/1347 

but i can't get it. Could give me some ideas?. I'm trying to write one line per optimization pass with some custom stats. This is my code:

 

int init()
{  
            
      if (IsOptimization()) {          
      }  
      return(0);
}

int deinit(){

       int totalTrades = 0;
       double profit = 0;

       if (IsOptimization()){
               
         for(int c=0;c<OrdersHistoryTotal();c++)
         {       
             OrderSelect(c,SELECT_BY_POS,MODE_HISTORY); 
             // Compute Stats
             profit = profit + OrderProfit();
             totalTrades++;             
         }                           
         
         // Write Stats for this pass
         int handle_file=FileOpen("OptimizationStats.csv",FILE_CSV|FILE_READ|FILE_WRITE,",");           
         FileSeek(handle_file,0, SEEK_END);         
         FileWrite(handle_file,
              profit,            
              totalTrades,
         ); 
         FileClose(handle_file);
       }//IsOptimization
}

int start(){
}

This code is appending fine, but i don't know how to detect first optimization pass to empty the csv file, is appending again and again.

 Regards, Vince. 

 
coiler: how can i detect that is the first optimitation pass to empty the stats csv file?. I
Why bother, just delete the file before starting the optimization.
 

Thanks WHRoerder,

if i delete manually the file before clicking on start optimization button, file is not created again, if i close mt4 file is not created again, only if i compile the ea, close mt4 and open mt4 again, file is created again. What i am missing?.

 
coiler: if i delete manually the file before clicking on start optimization button, file is not created again, if i close mt4 file is not created again, only if i compile the ea, close mt4 and open mt4 again, file is created again. What i am missing?.
  1. If it's not created again, how could you possibly create it in the first place? Makes no sense.
  2. I guess you are leaving the handle open instead of appending to the file and closing the handle. Are you checking the return codes?
       int      CREATE   = FILE_WRITE|FILE_TXT|FILE_ANSI;
       int      APPEND   = FILE_READ|CREATE;
       string   fileName = WindowExpertName() + ".RES";
       string   SEP      = "\t";  // TAB
       string   header   = OnTester_core( true, SEP)   + SEP + "Symbol"  + SEP
                         + "Period"                    + SEP + "Eprofit" + SEP
                         + "Eprofit2"                  + SEP + "nTrades";
       string   format   =  OnTester_core(false, SEP)  + SEP + _Symbol   + SEP
                         + "%s"                        + SEP + "%.2f"    + SEP
                         + "%g"                        + SEP + "%i";
       HANDLE   handle   = FileOpen(fileName, APPEND);
       if(handle == INVALID_HANDLE){
          Alert(StringFormat("%s: FileOpen(%s): %i", fileName, _LastError) ); 
          return 0;
       }
       if(FileSize(handle) == 0)  FileWrite(handle, header);
       else                       FileSeek(handle, 0, SEEK_END);
       FileWrite(handle, StringFormat(format, as_string(tf), perTF[tf].sum(),
                                                 perTF[tf].sum2(), nTrades) );
       FileClose(handle);
    

 
WHRoeder:
  1. If it's not created again, how could you possibly create it in the first place? Makes no sense.
  2. I guess you are leaving the handle open instead of appending to the file and closing the handle. Are you checking the return codes?

Thank you again,

it was my fault, it seems when you don't change any parameter in optimization parameters and click 'start' again, the mt4 cache makes no need to create my csv file again, so, i just delete the file and change some parameters, then click start and the file it's created again with all my optimization passes. WHRoeder, wich aproach can i use to dump the optimization pass number in my file too?

Regards, Vince. 

 
Count the number of existing lines.
Reason: