• Sign up >>

trying to find profitable weeks on backtest

To add comments, please log in or register

I would like some help trying to find out how many weeks are profitable after a backtest is made, i usually do 20 weeks backtest, I tried to build an array of profits and dates like OnTester documentation article, after that I tried split these results on another week array with no luck, any suggestion will be appreciated.

//| Tester function                                                  |
input datetime start = 01/01/2019;
input datetime end = 01/01/2020;

double OnTester()

//--- get trade results to the array 
   double array[]; 
   datetime date; 
   int trades=ArraySize(array); 
//--- average result per week
   int week = 24*7*3600;   
   double sum_w = 0.0;
   int week_prof = 0;
   for (i = 0; i< ArraySize(array); i++)
        for (datetime j = start; j< end; j= j+ week)
            if (date < week[j])         
            sum_w[j] = array[i]++;
            if (sum_w[j] > 0) week_prof++  


//| Get the array of profits/losses from deals                       | 

bool GetTradeResultsToArray(double &pl_results[],datetime &date) 
//--- request the complete trading history 
      return (false); 
   uint total_deals=HistoryDealsTotal(); 
//--- set the initial size of the array with a margin - by the number of deals in history 
//--- counter of deals that fix the trading result - profit or loss 
   int counter=0; 
   ulong ticket_history_deal=0; 
//--- go through all deals 
   for(uint a=0;a<total_deals;a++) 
      //--- select a deal  
         ENUM_DEAL_ENTRY deal_entry  =(ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY); 
         long            deal_type   =HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE); 
         double          deal_profit =HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT); 
         datetime          deal_time =(datetime)HistoryDealGetInteger(ticket_history_deal,DEAL_TIME); 
         //--- we are only interested in trading operations         
         if((deal_type!=DEAL_TYPE_BUY) && (deal_type!=DEAL_TYPE_SELL)) 
         //--- only deals that fix profits/losses 
            //--- write the trading result to the array and increase the counter of deals 
//--- set the final size of the array 
   return (true); 
Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC" using only Open, High, Low and Close prices of minute bars; detailed modeling in "Every tick" mode, as well as the most accurate "Every tick based on real ticks" mode applying actual historical data. Comparing the results allows us to assess...
Anthony Garot
Anthony Garot  

You cannot add values to a dimensionless array. You passed a dimensionless array into your function, but you set the size -after- putting all the items into it.

(1) The expedient way to do this is to statically fix array to a large enough size ahead of time.

double array[1000];     // size to a large enough value

(2) Another way is to call ArrayResize(pl_results, counter + 1) before each time you add the next deal_profit value. The downside of this is you could run into performance issues for large numbers. You can run the Profiler to see if that matters to you. You could always improve the performance by allocating blocks instead of singles ahead of time.

(3) A third way is to use dynamic arrays. See CArrayDouble for more information.

To add comments, please log in or register