Automated Optimization procedure EA using library ? - page 2

 

Please help, a little advice. I can not find the error ....

I read the "optimization report" (file htm) of metatrader end I'm trying to export the value in a file csv, but i have this result:

The second row is wrong (A2:H2) and the ProficFactor (C2:C6) is wrong (the rest i correct). The record of the htm report are 4.

This is the code:

....
file=FileOpen(FileReport,FILE_READ,0x7F) ; 
....


 while(FileIsEnding(file)==false)                                //thus far did not begin the end of the file - cycle
        {
         FileLine=FileReadString(file);                            //let us read line from the file of the report
         index=StringFind(FileLine, "title", 20);                  //let us find necessary line and will establish starting point
         if(index>0)
              {
               ArrayResize(ArrayStrg,NumStr+1);                    //we increase the size of the massif
               ArrayStrg[NumStr]=FileLine;                         //we write into the massif of line from the file
               Print ("FileLine",FileLine); 
               NumStr++;
              }
          }
          
    FileClose(file);                                               //let us shut the file
    //FileDelete(FileReport);                                        //let us remove file in order not to procreate the copy
    ArrayResize(ArrayData,NumStr);                                 //let us establish the size of massif in a quantity of the counted from the file lines

    for(text=0;text<NumStr;text++)
       {
        select=ArrayStrg[text]; 



//-------------------------------------------------------------------------
//                     Working the text of the report  (we separate flies from the cutlets)       
        //---------------------position is the passage------------------------------
        ClStep=StringFind(select, "; \">",20)+4;                                       //let us find the beginning of the position
        ClStepRazm=StringFind(select, "</td>",ClStep);                                 //let us find the end of the position
        CycleStep = StringSubstr(select, ClStep, ClStepRazm-ClStep);                   //we count the sum
        //---------------- Ïîçèöèÿ Ïðèáûëü -------------------------------
        GrProf=StringFind(select,"<td class=mspt>",ClStepRazm);                                  //let us find the beginning of the position
        GrProfRazm=StringFind(select, "</td>",GrProf);                                 //let us find the end of the position
        GrossProfit = StringSubstr(select, GrProf+15,GrProfRazm-(GrProf+15));            //we count the sum
         //-------------Ïîçèöèÿ Âñåãî Ñäåëîê -----------------------------
        TotTrad=StringFind(select, "<td>",GrProfRazm);                                 //let us find the beginning of the position
        TotTradRazm=StringFind(select, "</td>",TotTrad);                               //let us find the end of the position
        TotalTrades = StringSubstr(select, TotTrad+4,TotTradRazm-(TotTrad+4));         //we count the sum
        //-------------Ïîçèöèÿ Ïðèáûëüíîñòü--------------------------------
        ProfFact=StringFind(select, "<td class=mspt>",TotTradRazm);                               //let us find the beginning of the position
        ProfFactRazm=StringFind(select, "</td>",ProfFact);                             //let us find the end of the position
        ProfitFactor = StringSubstr(select, ProfFact+15,ProfFactRazm-(ProfFact+15));     //we count the sum
       //-------------Ïîçèöèÿ Ìàò Îæèäàíèå---------------------------------
        ExpPay=StringFind(select, "<td class=mspt>",ProfFactRazm);                                //let us find the beginning of the position
        ExpPayRazm=StringFind(select, "</td>",ExpPay);                                 //let us find the end of the position
        ExpectedPayoff = StringSubstr(select, ExpPay+15,ExpPayRazm-(ExpPay+15));         //we count the sum
      
        //------------------------------------------------------------------
Print ("select",select);     
Print (ClStep+"-"+ClStepRazm+"-"+CycleStep);
Print (GrProf+"-"+GrProfRazm+"-"+GrossProfit);
Print (TotTrad+"-"+TotTradRazm+"-"+TotalTrades);
Print (ProfFact+"-"+ProfFactRazm+"-"+ProfitFactor);
Print (ExpPay+"-"+ExpPayRazm+"-"+ExpectedPayoff);
     
        
        //-------------Ïîçèöèè ïåðåìåííûõ----------------------------------------
      //  P1=StringFind(select, Per1,20);                                                //let us find the beginning of the position
        P1=StringFind(select, Per1,20);                                                //let us find the beginning of the position
        P1k=StringFind(select, ";",P1);                                                //let us find the end of the position
        Perem1 = StringSubstr(select,P1+StringLen(Per1)+1,P1k-(P1+1+StringLen(Per1))); //we count the variable
        P2=StringFind(select, Per2,20);                                                //let us find the beginning of the position
        P2k=StringFind(select, ";",P2);                                                //let us find the end of the position
        Perem2 = StringSubstr(select,P2+StringLen(Per2)+1,P2k-(P2+1+StringLen(Per2))); //we count the variable
        P3=StringFind(select, Per3,20);                                                //let us find the beginning of the position
        P3k=StringFind(select, ";",P3);                                                //let us find the end of the position
        Perem3 = StringSubstr(select,P3+StringLen(Per3)+1,P3k-(P3+1+StringLen(Per3))); //we count the variable
        P4=StringFind(select, Per4,20);                                                //let us find the beginning of the position
        P4k=StringFind(select, ";",P4);                                                //let us find the end of the position
        Perem4 = StringSubstr(select,P4+StringLen(Per4)+1,P4k-(P4+1+StringLen(Per4))); //we count the variable
 //-----------------------let us transfer into the numerical size----------------------------

       TotalTradesTransit=StrToDouble(TotalTrades);
       GrossProfitTransit=NormalizeDouble(StrToDouble(GrossProfit),0);
       ExpectedPayoffTran=NormalizeDouble(StrToDouble(ExpectedPayoff),2);
       nodubl=true;
     if(MinTr < TotalTradesTransit && MaxTr > TotalTradesTransit){                    // Filter by the amount of trades 
         PrFactDouble = NormalizeDouble(StrToDouble(ProfitFactor),2);
          if(PrFactDouble==0){PrFactDouble=1000;}                                       //Replace 0 in the AOP_Profit_Factor for proper analysis
//-------------- Filter data having identical values -------------------       
           for(Dubl=0;Dubl<=text;Dubl++)
            {                                               //Start the loop searching for identical values
               if(GrossProfitTransit == ArrayData[Dubl][1])
                  {                            //check whether the results for maximal profit coincide
                     if(TotalTradesTransit == ArrayData[Dubl][2])
                        {                         //check whether the results for the amount of trades coincide
                           if(PrFactDouble == ArrayData[Dubl][3])
                              {                            //check whether the results for Profit Factor coincide
                                 if(ExpectedPayoffTran == ArrayData[Dubl][4])
                                    {                   //check whether the results for expected payoff coincide
                                       //nodubl=false;        // If everything coincides, flag it as coincided
                                    }   
                              }
                         }
                   }
            }  
 }  
            
//---------------- Write the filtered data in the array ----------------------

            if(nodubl)
            {
              ArrayData_TXT[ResizeArayNew][1]=DoubleToStr(GrossProfitTransit,2);                                
              ArrayData_TXT[ResizeArayNew][2]=DoubleToStr(TotalTradesTransit,0);
              ArrayData_TXT[ResizeArayNew][3]=DoubleToStr(PrFactDouble,2);
              ArrayData_TXT[ResizeArayNew][4]=DoubleToStr(ExpectedPayoffTran,2);
              ArrayData_TXT[ResizeArayNew][5]=Perem1;
              ArrayData_TXT[ResizeArayNew][6]=Perem2;
              ArrayData_TXT[ResizeArayNew][7]=Perem3;
              ArrayData_TXT[ResizeArayNew][8]=Perem4;
              ArrayData_TXT[ResizeArayNew][9]= TimeToStr(DT_INI_P,TIME_DATE)+"-"+TimeToStr(DT_FINE_P,TIME_DATE) ;
              ResizeArayNew++;
            } 
 
 //---------------- FIN Write the filtered data in the array TEXT----------------------   
       }




  //||||||||||||||||||||||||||||||| write variables into a intermediate file ||||||||||||||||||||||||||||||||||||                 
   int FileTst;
   string trs1,trs2,trs3,trs6,trs7,trs8,trs9,trs4,trs5, trs10;
   string File.csv=FileReport+".csv";
   FileTst=FileOpen(File.csv,FILE_CSV|FILE_WRITE,0x7F);           //open a file according to notes
   FileWrite(FileTst,"GrossProfit ; TotalTrades ; ProfitFactor ; ExpectedPayoff ;"+Per1+";"+Per2+";"+Per3+";"+Per4+";"+"Periodo"); 
   if(FileTst>0){
      for(i=0; i<ResizeArayNew; i++){
           trs2 = ArrayData_TXT[i][1];             
           trs3 = ArrayData_TXT[i][2];
           trs4 = ArrayData_TXT[i][3];             
           trs5 = ArrayData_TXT[i][4]; 
           trs6 = ArrayData_TXT[i][5];                       
           trs7 = ArrayData_TXT[i][6];
           trs8 = ArrayData_TXT[i][7];
           trs9 = ArrayData_TXT[i][8];
           trs10 = ArrayData_TXT[i][9];
        FileWrite(FileTst,trs2+";"+trs3+";"+trs4+";"+trs5+";"+trs6+";"+trs7+";"+trs8+";"+trs9+";"+trs10);                                   //èç ïåðåìåííîé â ôàéë
      } 
      FileClose(FileTst);                                          //Close the file
   }
if(FileTst<0){Print("5: KO write" + File.csv);} else {Print ("4: OK write "+File.csv);}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
GlobalVariableSet( "Opt_step", 0.0);
Print ("Opt_step: ",Glob); 

      }  //the end of the function 
 
 

This is a part of log

(thank's you)

23:08:30 Opt_4 EURUSD,H1: loaded successfully
23:08:30 Opt_4 EURUSD,H1 inputs: TimeTest=23.45; TestDay=5; 
23:08:30 Opt_4 EURUSD,H1: 1:OK write file ini
23:08:32 Opt_4 EURUSD,H1: Wait...1
23:08:35 Opt_4 EURUSD,H1: Wait...2
23:08:35 Opt_4 EURUSD,H1: 4: KO copy ini:"F:\save\Trading\METATRADER\Alpari NZ MT4\experts\files\optimise.ini" "F:\save\Trading\METATRADER\MT4_TEST" /y
23:08:35 Opt_4 EURUSD,H1: PathTerminal F:\save\Trading\METATRADER\Alpari NZ MT4\experts\files
23:08:35 Opt_4 EURUSD,H1: FileOptim1 \optimise.ini
23:08:35 Opt_4 EURUSD,H1: 2: OK copy ini: "F:\save\Trading\METATRADER\Alpari NZ MT4\experts\files\optimise.ini" "F:\save\Trading\METATRADER\MT4_TEST" /y
23:08:35 Opt_4 EURUSD,H1: 3: OK Terminal
23:08:45 Opt_4 EURUSD,H1: Wait...3
23:08:47 Opt_4 EURUSD,H1: Wait...4
23:08:47 Opt_4 EURUSD,H1: file0
23:08:47 Opt_4 EURUSD,H1: 4: KO copy report: "F:\save\Trading\METATRADER\MT4_TEST\F1EURUSD_2009.12.31_2011.12.31.htm" "F:\save\Trading\METATRADER\Alpari NZ MT4\experts\files" /y
23:08:47 Opt_4 EURUSD,H1: PathTester F:\save\Trading\METATRADER\MT4_TEST
23:08:47 Opt_4 EURUSD,H1: FileReport1 \F1EURUSD_2009.12.31_2011.12.31.htm
23:08:47 Opt_4 EURUSD,H1: 4: OK copy report: \F:\save\Trading\METATRADER\MT4_TEST"\F1EURUSD_2009.12.31_2011.12.31.htm" "F:\save\Trading\METATRADER\Alpari NZ MT4\experts\files" /y
23:08:47 Opt_4 EURUSD,H1: FileLine    <title>Strategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr align=right><td title="FastEMA=11; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">3</td><td class=mspt>278.06</td><td>87</td><td class=mspt>2.98</td><td class=mspt>3.20</td><td class=mspt>285.70</td><td class=mspt>2.85</td></tr>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr bgcolor="#E0E0E0" align=right><td title="FastEMA=10; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">2</td><td class=mspt>220.45</td><td>80</td><td class=mspt>2.39</td><td class=mspt>2.76</td><td class=mspt>285.46</td><td class=mspt>2.85</td></tr>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr align=right><td title="FastEMA=9; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">1</td><td class=mspt>110.25</td><td>78</td><td class=mspt>1.42</td><td class=mspt>1.41</td><td class=mspt>486.28</td><td class=mspt>4.80</td></tr>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr bgcolor="#E0E0E0" align=right><td title="FastEMA=12; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">4</td><td class=mspt>87.26</td><td>82</td><td class=mspt>1.28</td><td class=mspt>1.06</td><td class=mspt>371.86</td><td class=mspt>3.62</td></tr>
23:08:47 Opt_4 EURUSD,H1: select    <title>Strategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: 3--1- <title>Strategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: -1--1-ategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: -1--1- <title>Strategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: -1--1-ategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: -1--1-ategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: select<tr align=right><td title="FastEMA=11; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">3</td><td class=mspt>278.06</td><td>87</td><td class=mspt>2.98</td><td class=mspt>3.20</td><td class=mspt>285.70</td><td class=mspt>2.85</td></tr>
23:08:47 Opt_4 EURUSD,H1: 185-186-3
23:08:47 Opt_4 EURUSD,H1: 191-212-278.06
23:08:47 Opt_4 EURUSD,H1: 217-223-87
23:08:47 Opt_4 EURUSD,H1: 228-247-2.98
23:08:47 Opt_4 EURUSD,H1: 252-271-3.20
 

The problem clearly lies in the constrution of the output data array especially the first row of the array. So either a StringFind() is failing or the first parameter of the array construction is wrong. Those are the first thoughts on the problem

To track it dow I would be looking at

   FileWrite(FileTst,"GrossProfit ; TotalTrades ; ProfitFactor ; ExpectedPayoff ;"+Per1+";"+Per2+";"+Per3+";"+Per4); 

And where these lines are called from.

That is as far as I can go at the moment

 

Thank's Ickyrus.

Yes, i think the problem is in the constution of the first row of the array.

I have insert a print in this cycle of while:

 while(FileIsEnding(file)==false)                                //thus far did not begin the end of the file - cycle
        {
         FileLine=FileReadString(file);                            //let us read line from the file of the report
         index=StringFind(FileLine, "title", 20);                  //let us find necessary line and will establish starting point
         if(index>0)
              {
               ArrayResize(ArrayStrg,NumStr+1);                    //we increase the size of the massif
               ArrayStrg[NumStr]=FileLine;                         //we write into the massif of line from the file
               Print ("FileLine",FileLine); 
               NumStr++;
              }
          }
          

i have this in the log:

23:08:47 Opt_4 EURUSD,H1: FileLine    <title>Strategy Tester: MACD Sample_1</title>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr align=right><td title="FastEMA=11; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">3</td><td class=mspt>278.06</td><td>87</td><td class=mspt>2.98</td><td class=mspt>3.20</td><td class=mspt>285.70</td><td class=mspt>2.85</td></tr>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr bgcolor="#E0E0E0" align=right><td title="FastEMA=10; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">2</td><td class=mspt>220.45</td><td>80</td><td class=mspt>2.39</td><td class=mspt>2.76</td><td class=mspt>285.46</td><td class=mspt>2.85</td></tr>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr align=right><td title="FastEMA=9; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">1</td><td class=mspt>110.25</td><td>78</td><td class=mspt>1.42</td><td class=mspt>1.41</td><td class=mspt>486.28</td><td class=mspt>4.80</td></tr>
23:08:47 Opt_4 EURUSD,H1: FileLine<tr bgcolor="#E0E0E0" align=right><td title="FastEMA=12; TakeProfit=50; Lots=0.1; MACDOpenLevel=3; MACDCloseLevel=2; MATrendPeriod=26; SlowEMA=26; SignalSMA=9; TrailingStop=30; SetHour=0; SetMinute=1; ">4</td><td class=mspt>87.26</td><td>82</td><td class=mspt>1.28</td><td class=mspt>1.06</td><td class=mspt>371.86</td><td class=mspt>3.62</td></tr>
But, i don't konw how to remove the problem from the first row ( <title>Strategy Tester: MACD Sample_1</title>) ....
 

While it is not necessarily a good idea to be fixing the code for a particular case I would as a 'quick fix' read the first line to 'throw it away' thus

file=FileOpen(FileReport,FILE_READ,0x7F) ; 
....

FileLine=FileReadString(file);                                  //<<<<<<  added this read line to remove the first line
while(FileIsEnding(file)==false)                                //thus far did not begin the end of the file - cycle
        {
         FileLine=FileReadString(file);                            //let us read line from the file of the report
         index=StringFind(FileLine, "title", 20);                  //let us find necessary line and will establish starting point
         if(index>0)
              {
               ArrayResize(ArrayStrg,NumStr+1);                    //we increase the size of the massif
               ArrayStrg[NumStr]=FileLine;                         //we write into the massif of line from the file
               Print ("FileLine",FileLine); 
               NumStr++;
              }
          }
          
    FileClose(file);                                               //let us shut the file
          
Reason: