My backtesting speed is too slow because of this ...

 

Hello,


I Created an Expert depending on external indicator 


while i backtesting it working too slow


I think the problem is in the indicator code


Can somebody refer to me the mistake that cause this slow?


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit
double Open[];
double Close[];
int WPR_handle;
double WPR[];
int Ichimoku_handle;
double Ichimoku_Kijunsen[];
double Ichimoku_SenkouspanA[];
double Ichimoku_SenkouspanB[];
double Low[];
double High[];

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | AAA @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   SetIndexBuffer(0, Buffer1);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_ARROW, 241);
   SetIndexBuffer(1, Buffer2);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(1, PLOT_ARROW, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   WPR_handle = iWPR(NULL, PERIOD_CURRENT, 8);
   if(WPR_handle < 0)
     {
      Print("The creation of iWPR has failed: WPR_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   Ichimoku_handle = iIchimoku(NULL, PERIOD_CURRENT, 9, 26, 52);
   if(Ichimoku_handle < 0)
     {
      Print("The creation of iIchimoku has failed: Ichimoku_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   if(CopyOpen(Symbol(), PERIOD_CURRENT, 0, rates_total, Open) <= 0) return(rates_total);
   ArraySetAsSeries(Open, true);
   if(CopyClose(Symbol(), PERIOD_CURRENT, 0, rates_total, Close) <= 0) return(rates_total);
   ArraySetAsSeries(Close, true);
   if(BarsCalculated(WPR_handle) <= 0) 
      return(0);
   if(CopyBuffer(WPR_handle, 0, 0, rates_total, WPR) <= 0) return(rates_total);
   ArraySetAsSeries(WPR, true);
   if(BarsCalculated(Ichimoku_handle) <= 0) 
      return(0);
   if(CopyBuffer(Ichimoku_handle, KIJUNSEN_LINE, 0, rates_total, Ichimoku_Kijunsen) <= 0) return(rates_total);
   ArraySetAsSeries(Ichimoku_Kijunsen, true);
   if(BarsCalculated(Ichimoku_handle) <= 0) 
      return(0);
   if(CopyBuffer(Ichimoku_handle, SENKOUSPANA_LINE, 0, rates_total, Ichimoku_SenkouspanA) <= 0) return(rates_total);
   ArraySetAsSeries(Ichimoku_SenkouspanA, true);
   if(BarsCalculated(Ichimoku_handle) <= 0) 
      return(0);
   if(CopyBuffer(Ichimoku_handle, SENKOUSPANB_LINE, 0, rates_total, Ichimoku_SenkouspanB) <= 0) return(rates_total);
   ArraySetAsSeries(Ichimoku_SenkouspanB, true);
   if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total);
   ArraySetAsSeries(Low, true);
   if(CopyHigh(Symbol(), PERIOD_CURRENT, 0, rates_total, High) <= 0) return(rates_total);
   ArraySetAsSeries(High, true);
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(100-1, rates_total-1-10)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(WPR[i]>=-10
      && Open[i]<Ichimoku_Kijunsen[i]
      && Close[i]>Ichimoku_Kijunsen[i]
      && Close[i]>Ichimoku_SenkouspanA[i]
      && Close[i]>Ichimoku_SenkouspanB[i]
      && ((Ichimoku_Kijunsen[i]>Ichimoku_SenkouspanA[i])||(Ichimoku_Kijunsen[i]>Ichimoku_SenkouspanB[i]))
      && High[1+i]<Ichimoku_Kijunsen[1+i]
      && High[2+i]<Ichimoku_Kijunsen[2+i]
      && High[3+i]<Ichimoku_Kijunsen[3+i]
      && High[4+i]<Ichimoku_Kijunsen[4+i]
      && High[5+i]<Ichimoku_Kijunsen[5+i]
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(WPR[i]<=-90
      && Open[i]>Ichimoku_Kijunsen[i]
      && Close[i]<Ichimoku_Kijunsen[i]
      && Close[i]<Ichimoku_SenkouspanA[i]
      && Close[i]<Ichimoku_SenkouspanB[i]
      && ((Ichimoku_Kijunsen[i]<Ichimoku_SenkouspanA[i])||(Ichimoku_Kijunsen[i]<Ichimoku_SenkouspanB[i]))
      && Low[1+i]>Ichimoku_Kijunsen[1+i]
      && Low[2+i]>Ichimoku_Kijunsen[2+i]
      && Low[3+i]>Ichimoku_Kijunsen[3+i]
      && Low[4+i]>Ichimoku_Kijunsen[4+i]
      && Low[5+i]>Ichimoku_Kijunsen[5+i]
      )
        {
         Buffer2[i] = High[i]; //Set indicator value at Candlestick High
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+


I think the mistake is here:


for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(100-1, rates_total-1-10)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   


Can you help me in this topic?

 
  1.    if(CopyHigh(Symbol(), PERIOD_CURRENT, 0, rates_total, High) <= 0) return(rates_total);
       ArraySetAsSeries(High, true);
       //--- main loop
       for(int i = limit-1; i >= 0; i--)
         {
          ⋮
          && High[5+i]<Ichimoku_Kijunsen[5+i]

    It only processes bars [limit-1 … 0] with a look back of five (5). Why are you reading in all bars, instead of only [limit+4 … 0] ?

  2. You can move all your set as series to OnInit like the example shows.

  3.    if(BarsCalculated(Ichimoku_handle) <= 0) 
          return(0);

    Why are you calling that multiple times?

  4. for(int i = limit-1; i >= 0; i--)
         {
          if (i >= MathMin(100-1, rates_total-1-10)) continue

    See How to do your lookbacks correctly #9#14 & #19.

  5. Ahmed Abd El Aziz: while i backtesting it working too slow
    1. EAs : Don't do per tick what you can do per bar, or on open.
      If you are waiting for a level, don't reevaluate, wait until price reaches it (or a new bar starts, and you recalculate.)
      If you are waiting for an order to close, only look when OrdersTotal (or MT5 equivalent) has changed.
                How to get backtesting faster ? - MT4 - MQL4 programming forum (2017)

    2. Indicators: Code it properly so it only recomputes bar zero (after the initial run.)
                How to do your lookbacks correctly. (2016)
                3 Methods of Indicators Acceleration by the Example of the Linear Regression - MQL5 Articles. (2011)
      Or, reduce Tools → Options (control+O) → Charts → Max bars in chart to something reasonable (like 1K.)