Help with indicator massive lag

 

Hi everyone, I managed to build an indicator that fully fits my needs, unfortunately it makes mt4 massively lag which is quite a problem. Can someone help me identify why that's the case? Thank's. 


// Setup
#property copyright "Eddy"
#property description "Average multi-timeframe candle expected range"
#property strict
#property show_inputs
#property indicator_chart_window

// Define number of buffers
#property indicator_buffers 15

// Allow user to change indicator style


//--- Indicator Settings
//Enum indicator mode

//Enum line witdht
enum ENUM_LINE_WIDTH
  {
   _1=1,  //1
   _2=2,  //2
   _3=3,  //3
   _4=4,  //4
   _5=5,  //5
  };

input string   none_1            = "------------------------";       //1H candlesticks parameters
input int session_start = 07; //1H candle session start
input int session_finish= 17; //1H candle session start
input int period_atr_tf1 = 10; //Number of 1H sessions to average
input string   none_2            = "------------------------";       //Higher timeframes 
input int period_atr_tf3 = 36; //Period ATR 4H
input int i_ADRperiod = 5; //Period ATR Daily
input int period_atr_tf4 = 3; //Period ATR Weekly


input string   none_3            = "------------------------";       //Other settings
input double dev_prc = 30;   //Devitaion percentage


int i_tf1_open = 60; //TF 1= hour chart
int i_tf2_open    = 1440;
int i_tf3_open = 240; //TF3 = 4H 
int i_tf4_open = 10080; //TF4 = Weekly
int i_tf5_open = 43200; //TF4 = Weekly


//--- Global variable declarations


// Indicator buffers
double buff_tf1_open[],
       buff_tf1_a1[],
       buff_tf1_a2[],
       buff_tf1_b1[],
       buff_tf1_b2[],
       buff_tf2_open[],
       buff_tf2_a1[],
       buff_tf2_a2[],
       buff_tf3_open[],
       buff_tf3_a1[],
       buff_tf3_a2[],
       buff_tf4_open[],
       buff_tf4_a1[],
       buff_tf4_a2[],
       buff_tf5_open[];

//--- Event handling functions

// Initialisation event handler
int OnInit(void)
  {
// Set number of significant digits (precision)
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

// Set Indicator buffers
   SetIndexBuffer(0, buff_tf1_open, INDICATOR_DATA);
   SetIndexBuffer(1, buff_tf1_a1, INDICATOR_DATA);
   SetIndexBuffer(2, buff_tf1_a2, INDICATOR_DATA);
   SetIndexBuffer(3, buff_tf1_b1, INDICATOR_DATA);
   SetIndexBuffer(4, buff_tf1_b2, INDICATOR_DATA);

   SetIndexBuffer(5, buff_tf2_open, INDICATOR_DATA);
   SetIndexBuffer(6, buff_tf2_a1, INDICATOR_DATA);
   SetIndexBuffer(7, buff_tf2_a2, INDICATOR_DATA);

   SetIndexBuffer(8, buff_tf3_open, INDICATOR_DATA);
   SetIndexBuffer(9, buff_tf3_a1, INDICATOR_DATA);
   SetIndexBuffer(10, buff_tf3_a2, INDICATOR_DATA);

   SetIndexBuffer(11, buff_tf4_open, INDICATOR_DATA);
   SetIndexBuffer(12, buff_tf4_a1, INDICATOR_DATA);
   SetIndexBuffer(13, buff_tf4_a2, INDICATOR_DATA);

   SetIndexBuffer(14, buff_tf5_open, INDICATOR_DATA);


//Set indicator style
   SetIndexStyle(0,DRAW_LINE,0,2,clrNavy);
   SetIndexStyle(1,DRAW_LINE,0,1,clrNavy);
   SetIndexStyle(2,DRAW_LINE,0,1,clrNavy);
   SetIndexStyle(3,DRAW_LINE,2,1,clrNavy);
   SetIndexStyle(4,DRAW_LINE,2,1,clrNavy);

   SetIndexStyle(5,DRAW_LINE,0,4,clrOrange);
   SetIndexStyle(6,DRAW_LINE,2,1,clrOrange);
   SetIndexStyle(7,DRAW_LINE,2,1,clrOrange);

   SetIndexStyle(8,DRAW_LINE,0,3,clrMediumSeaGreen);
   SetIndexStyle(9,DRAW_LINE,2,1,clrMediumSeaGreen);
   SetIndexStyle(10,DRAW_LINE,2,1,clrMediumSeaGreen);

   SetIndexStyle(11,DRAW_LINE,0,4,clrTomato);
   SetIndexStyle(12,DRAW_LINE,2,1,clrTomato);
   SetIndexStyle(13,DRAW_LINE,2,1,clrTomato);

   SetIndexStyle(14,DRAW_LINE,0,4,clrPurple);

   return INIT_SUCCEEDED;  // Successful initialisation of indicator
  };

// Calculation event handler
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[]
)
  {
// Define maximum index for the buffer array

   int lookback = 100; //trying to display 1D open on last 100 1h candles
// Replacing loop function with the one you pointed out
   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {


      //ADR

      double final_adr = CalculateATR(i_tf2_open,iBar,i_ADRperiod);      

      //ATR TF1
      int index_atr_tf1 =  iBarShift(NULL,i_tf1_open,Time[iBar],false);

      double sum_atr_tf1=0,atr_tf1=0;
      int tf1_amount = 0;

      for(int i=index_atr_tf1+1; i<=index_atr_tf1+(period_atr_tf1*24); i++)
        {
         if(TimeHour(iTime(NULL,i_tf1_open,i))>=session_start&&TimeHour(iTime(NULL,i_tf1_open,i))<session_finish)
           {
            sum_atr_tf1+=iHigh(NULL,i_tf1_open,i)-iLow(NULL,i_tf1_open,i);
            tf1_amount++;
           }
        }

      atr_tf1 = sum_atr_tf1/tf1_amount;
      
      //ATR TF3 -4 
      
      double atr_tf3 = CalculateATR(i_tf3_open,iBar,period_atr_tf3);
      double atr_tf4 = CalculateATR(i_tf4_open,iBar,period_atr_tf4);
      
      
      ///Open lines

      ///TF1 functions
      int index_tf1 =  iBarShift(NULL,i_tf1_open,Time[iBar],false);
      double tf_1 = iOpen(NULL,i_tf1_open,index_tf1);
      
      if(iBarShift(NULL,i_tf1_open,Time[iBar],false)!= iBarShift(NULL,i_tf1_open,Time[iBar+1],false))
        {
         buff_tf1_open[iBar+1] = EMPTY_VALUE;
         buff_tf1_a1[iBar+1]   = EMPTY_VALUE;
         buff_tf1_a2[iBar+1]   = EMPTY_VALUE;
         buff_tf1_b1[iBar+1]   = EMPTY_VALUE;
         buff_tf1_b2[iBar+1]   = EMPTY_VALUE;
        }
        
      buff_tf1_open[iBar] = tf_1;
      buff_tf1_a1[iBar]   = tf_1+(atr_tf1/2)*(dev_prc/100) ;
      buff_tf1_a2[iBar]   = tf_1-(atr_tf1/2)*(dev_prc/100) ;
      buff_tf1_b1[iBar]   = tf_1+atr_tf1/2 ;
      buff_tf1_b2[iBar]   = tf_1-atr_tf1/2 ;  

      /// 4H & Daily functions
      AverageRangeToBuffer(i_tf2_open,final_adr,iBar,buff_tf2_open,buff_tf2_a1,buff_tf2_a2);
      AverageRangeToBuffer(i_tf3_open,atr_tf3,iBar,buff_tf3_open,buff_tf3_a1,buff_tf3_a2);
      AverageRangeToBuffer(i_tf4_open,atr_tf4,iBar,buff_tf4_open,buff_tf4_a1,buff_tf4_a2);     
      AverageRangeToBuffer(i_tf5_open,iBar,buff_tf5_open); 
      

      
     }
   return rates_total-1; // Recalculate current bar next tick.
// Return value of prev_calculated for next call
  };


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }

//+------------------------------------------------------------------+


double CalculateATR(int timeframe,int index_forloop,int period){

      int index_atr =  iBarShift(NULL,timeframe,Time[index_forloop],false);
      double sum_adr=0;

      for(int i=index_atr+1; i<=index_atr+period; i++)
        {
         sum_adr += MathMax(iHigh(NULL,timeframe,i),iClose(NULL,timeframe,i+1))-MathMin(iLow(NULL,timeframe,i),iClose(NULL,timeframe,i+1));
        }

      return(sum_adr/period);
      
};



void AverageRangeToBuffer(int timeframe,double atr, int forloop_index, double &buffer_open[], double &buffer_a1[],double &buffer_a2[]){


      int index_ =  iBarShift(NULL,timeframe,Time[forloop_index],false);
      double tf_ = iOpen(NULL,timeframe,index_);
    

      if(iBarShift(NULL,timeframe,Time[forloop_index],false)!= iBarShift(NULL,timeframe,Time[forloop_index+1],false))
        {
         buffer_open[forloop_index+1] = EMPTY_VALUE;
         buffer_a1[forloop_index+1] = EMPTY_VALUE;
         buffer_a2[forloop_index+1] = EMPTY_VALUE;
        }

      buffer_open[forloop_index] = tf_;
      buffer_a1[forloop_index]   = tf_+(atr/2);
      buffer_a2[forloop_index]   = tf_-(atr/2);


}

void AverageRangeToBuffer(int timeframe, int forloop_index, double &buffer_open[]){


      int index_ =  iBarShift(NULL,timeframe,Time[forloop_index],false);
      double tf_ = iOpen(NULL,timeframe,index_);
    

      if(iBarShift(NULL,timeframe,Time[forloop_index],false)!= iBarShift(NULL,timeframe,Time[forloop_index+1],false))
        {
         buffer_open[forloop_index+1] = EMPTY_VALUE;
        }

      buffer_open[forloop_index] = tf_;
}
 
Can anyone please help? 
 
  1.   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
    ⋮
          int index_atr_tf1 =  iBarShift(NULL,i_tf1_open,Time[iBar],false);

    Do not post code that will not even compile. MT5 has no predefined variables Bars and Time[].

  2. int i_tf1_open = 60; //TF 1= hour chart
    int i_tf2_open    = 1440;
    int i_tf3_open = 240; //TF3 = 4H 
    int i_tf4_open = 10080; //TF4 = Weekly
    int i_tf5_open = 43200; //TF4 = Weekly

    Do not hard code constants. Use the ENUM_TIMEFRAMES enumerations

 
ironhak #:
Can anyone please help? 

I'm only a beginner and what I'm reading might be outdated so just FYI. 
Anyhow I think MQL4 only allows 8 Buffers and you have 15. I assume you meant to post in MQL4 forum section ? 
Anyhow 
Hope this helps. 

https://docs.mql4.com/customind

Custom Indicators - MQL4 Reference
Custom Indicators - MQL4 Reference
  • docs.mql4.com
Custom Indicators - MQL4 Reference
 
@Agent86 #: I'm only a beginner and what I'm reading might be outdated so just FYI. Anyhow I think MQL4 only allows 8 Buffers and you have 15.
MQL allows for 512 buffers IndicatorBuffers - Custom Indicators - MQL4 Reference
 
Hi everyone, I'm really sorry this was a mql4 code, I posted on the wrong section. Anyway I managed to reduce the lag thank. 
Reason: