Can anyone Please Help me with that Custom Indicator the Back Testing is extremely slow. Thanks in advance

 
//+==================================================================================================+
//|                                                                          NN Indicator EURUSD.mq5 |
//|                                                               Copyright 2018, C. K. Papageorgiou |
//|                                                                                             none |
//+==================================================================================================+
#property copyright "Copyright 2019, C. K. Papageorgiou"
#property link      "none"
#property version   "1.00"
#property indicator_separate_window

#property indicator_plots 1
#property indicator_buffers 1
#property indicator_color1 clrLime
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1

#import "NNTrainDLL_EURUSD.dll"
   int initializeTrainedNN(string nnFile);
   int computeNNIndicator(double& ind1[], double& ind2[],double& ind3[], int size, double& result[], int rates);  
#import


int INPUT_WINDOW = 6;
int PREDICT_WINDOW = 1;
int MaxBars = 5000;

double ind1Arr[], ind2Arr[], ind3Arr[]; 
double neuralArr[];

int hStochastic;
int hWilliamsR;

int hNeuralMA;

//+==================================================================================================+
//| Custom indicator initialization function                                                         |
//+==================================================================================================+
int OnInit()
{
   //Clear Chart
   ObjectsDeleteAll(ChartID(),-1,-1);
   
   //Indicator Shortname
   IndicatorSetString(INDICATOR_SHORTNAME,"NN LOG:");
   
   //Indicator buffers mapping
   SetIndexBuffer(0, neuralArr, INDICATOR_DATA);
   
   ArrayResize(ind1Arr, INPUT_WINDOW);
   ArrayResize(ind2Arr, INPUT_WINDOW);
   ArrayResize(ind3Arr, INPUT_WINDOW);
     
   ArrayInitialize(neuralArr, 0.0);
   
   ArraySetAsSeries(ind1Arr, true);   
   ArraySetAsSeries(ind2Arr, true);  
   ArraySetAsSeries(ind3Arr, true);
  
   ArraySetAsSeries(neuralArr, true);   
            
   hStochastic = iStochastic(Symbol(),Period(), 8, 5, 5, MODE_EMA, STO_LOWHIGH);
   hWilliamsR = iWPR(Symbol(),Period(), 21);
   
   Print(TerminalInfoString(TERMINAL_COMMONDATA_PATH)+"\\MQL5\\Files\\step5_network_"+_Symbol+".eg");
   initializeTrainedNN(TerminalInfoString(TERMINAL_COMMONDATA_PATH)+"\\MQL5\\Files\\step5_network_"+_Symbol+".eg");
   
   return(0);
}

//+==================================================================================================+
//| 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 calc_limit;
   
   if(prev_calculated==0) // First execution of the OnCalculate() function after the indicator start
        calc_limit=MathMin(rates_total-1,MaxBars);
   else calc_limit=rates_total-prev_calculated;
  
   for (int i=0; i<calc_limit; i++) 
   {
      CopyBuffer(hStochastic, 0, i, INPUT_WINDOW, ind1Arr);
      CopyBuffer(hStochastic, 1, i, INPUT_WINDOW, ind2Arr);
      CopyBuffer(hWilliamsR,  0, i, INPUT_WINDOW, ind3Arr);    
      
   computeNNIndicator(ind1Arr, ind2Arr, ind3Arr, INPUT_WINDOW, neuralArr, rates_total-i);
      
      
   //NN Label
   ObjectCreate(ChartID(),"NN_Indicator Button",OBJ_BUTTON,0,0,0);
   ObjectSetString(ChartID(),"NN_Indicator Button",OBJPROP_TEXT,(string)(i+1) +"-"+(string)rates_total);
   ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_BACK,false);
   ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_XSIZE,180);
   ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_XDISTANCE,2000);
   ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_YDISTANCE,600);
   ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_STATE,false);
   if((i+1)==calc_limit)
   {
      ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_BGCOLOR,clrLime);
      ObjectSetString(ChartID(),"NN_Indicator Button",OBJPROP_TEXT,"DONE");
   }
   if((i+1)<calc_limit)
   {
      ObjectSetInteger(ChartID(),"NN_Indicator Button",OBJPROP_BGCOLOR,clrBlueViolet);
      ObjectSetString(ChartID(),"NN_Indicator Button",OBJPROP_TEXT,(string)(i+1) +"-"+(string)rates_total);
   }
      
   }
   
   
//+==================================================================================================+   
   //Return value of prev_calculated for next call
   return(rates_total);
   
}

//+==================================================================================================+

The indicator is working fine but makes the back testing extremely slow. Can someone please help me with this? Thanks in advance.

 
Christos Papageorgiou:

The indicator is working fine but makes the back testing extremely slow. Can someone please help me with this? Thanks in advance.

You can try doing some "profiling" : https://www.metatrader5.com/en/metaeditor/help/development/profiling

Code profiling - Developing programs - MetaTrader 5
Code profiling - Developing programs - MetaTrader 5
  • www.metatrader5.com
Profiling means collecting program parameters during its execution. During a profiling, the execution time and the number of calls of individual functions and program code lines are measured. With this tool, the programmer is able to find and optimize the slowest code sections. Profiling can be performed on the normal chart of the trading...
 
Seng Joo Thio:

You can try doing some "profiling" : https://www.metatrader5.com/en/metaeditor/help/development/profiling

Thank you very much for your reply Seng. I have tested many things before my post. I can't change the External NN Performance the Indicator calls. I have to limit the Calculation Bars to 21 but I can't find a way to do it. Somehow I have to replace rates_total and Limit Bars to 21. 

Reason: