What is the Proper way to Force the indicator to calculate only the last 5000 candles? Thanks in advance

Christos Papageorgiou
154
Christos Papageorgiou  
#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=rates_total-34;
   else calc_limit=rates_total-prev_calculated;
  
   ArrayResize(neuralArr, rates_total);
  
   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,1400);
   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);
   
}

//+==================================================================================================+
I have tried many ways to limit the indicator to use only the last 5000 bars for calculations. Thanks in advance for your help
Keith Watford
Moderator
26702
Keith Watford  
   if(prev_calculated==0) // First execution of the OnCalculate() function after the indicator start
        calc_limit=MathMin(rates_total-34,MaxBars);
   else calc_limit=rates_total-prev_calculated;
Christos Papageorgiou
154
Christos Papageorgiou  
Keith Watford:

Thank you very much for your answer Keith! For some reason the indicator is not responding. I have also changed the rates_total in the rest of the code but nothing..
Maybe I have to run 2 for loops

Lorentzos Roussos
24794
Lorentzos Roussos  
Christos Papageorgiou:

Thank you very much for your answer Keith! For some reason the indicator is not responding. I have also changed the rates_total in the rest of the code but nothing..
Maybe I have to run 2 for loops 

The NeuralArray constant resizing is slowing the calculations down.
I think you can remove it ( ArrayResize(neuralArr, rates_total); )

Christos Papageorgiou
154
Christos Papageorgiou  
Lorentzos Roussos:

The NeuralArray constant resizing is slowing the calculations down.
I think you can remove it ( ArrayResize(neuralArr, rates_total); )

Thank you very much for the advice. I will try it. The main problem is that despite the No of Bars I select from the options e.g. 50000 the rates_total is always 359050.

Anthony Garot
4842
Anthony Garot  
Christos Papageorgiou:

Thank you very much for the advice. I will try it. The main problem is that despite the No of Bars I select from the options e.g. 50000 the rates_total is always 359050.

rates_total will always be the number of bars available in your downloaded history. You can delete the historical data and start over. You can also set your "Max bars in chart" to 5,000, which will limit how much data is downloaded from the server.

(Tools -> Options -> Charts -> Max bars in chart)

But this isn't an answer to your original question.

Christos Papageorgiou
154
Christos Papageorgiou  
Anthony Garot:

rates_total will always be the number of bars available in your downloaded history. You can delete the historical data and start over. You can also set your "Max bars in chart" to 5,000, which will limit how much data is downloaded from the server.

(Tools -> Options -> Charts -> Max bars in chart)

But this isn't an answer to your original question.

Thanks for your answer Anthony. I have already done that. It's a deep neural network indicator so I think there is no solution to my problem(about the loading time). Just wanted to limit the bars to increase the performance.

Lorentzos Roussos
24794
Lorentzos Roussos  
Christos Papageorgiou:

Thanks for your answer Anthony. I have already done that. It's a deep neural network indicator so I think there is no solution to my problem(about the loading time). Just wanted to limit the bars to increase the performance.

You can instruct it to learn on points of interest (whether its highs / lows , or points you know had a good R:R -one that interests you) , the contingency is you have to retest and monitor the solutions found on these POI's.

Christos Papageorgiou
154
Christos Papageorgiou  
Lorentzos Roussos:

You can instruct it to learn on points of interest (whether its highs / lows , or points you know had a good R:R -one that interests you) , the contingency is you have to retest and monitor the solutions found on these POI's.

I have finally make some changes to Deep Neural Network itself. I'm doing all the calculations on Python 3 with Tensorflow GPU and pass the prices directly to indicator.