Please help Slow Indicator

 

Hi experts. Your expert guidance requested.

I am a newbie and recently learnt MQL5. This is my 2nd code i have developed.

The super trend indicator often does not load. It also vanishes when i change timeframes.

It appears when i compile the it again from Meta Editor.

Please help me to identify where am I missing.   

#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   1
//--- plot SuperTrend
#property indicator_label1  "SuperTrend"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrRed, clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2



//Calling ATR_Manjit
int Handle;
input int ATR_Range=14;
input int ATRShift = 0;

//--- input parameters
input int      Multiplier=3;

//--- indicator buffers
double         ST_LineBuffer[];
double         ST_ColorBuffer[];
double         BUpper_LineBuffer[];
double         BLower_LineBuffer[];
double         FUpper_LineBuffer[];
double         FLower_LineBuffer[];
double         Value_ATRBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ST_LineBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ST_ColorBuffer,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,BUpper_LineBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,BLower_LineBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,FUpper_LineBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,FLower_LineBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,Value_ATRBuffer,INDICATOR_CALCULATIONS);

//Start plotting from
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ATR_Range-1);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//Name of Indicator in data window   
   string shortname = StringFormat("SuperTrend ATR (%d, %d)",ATR_Range,Multiplier);
   PlotIndexSetString(0,PLOT_LABEL,shortname);

//Calling Values from iATR Indicator
   Handle = iCustom(_Symbol,PERIOD_CURRENT,"ATR_Manjit",ATR_Range,ATRShift);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator de-initialization function                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  { IndicatorRelease(Handle);  }
//+------------------------------------------------------------------+
//| 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[])
  {
//Checking for start point of calculation
   int first = ATR_Range-1;
   if(prev_calculated > 0)
     { first = prev_calculated-1;  }

//Getting & Calling values in Handle
   if(CopyBuffer(Handle,0,0,rates_total-1,Value_ATRBuffer) <=0)
     { return(0);   }

//Calculatinf Basic Upper and Lower Lines
   for(int i=first ; i<=rates_total-1 ; i++)
     {
      BUpper_LineBuffer[i] = ((high[i] + low[i])/2)+(Multiplier * Value_ATRBuffer[i]);
      BLower_LineBuffer[i] = ((high[i] + low[i])/2)-(Multiplier * Value_ATRBuffer[i]);

//Calculating Final Upper Line
      if(i == ATR_Range-1)
        {  FUpper_LineBuffer[i] = BUpper_LineBuffer[i];  }
      else
        {
         if((BUpper_LineBuffer[i] < FUpper_LineBuffer[i-1])||(close[i-1] > FUpper_LineBuffer[i-1]))
           {   FUpper_LineBuffer[i] = BUpper_LineBuffer[i];    }
         else
           {   FUpper_LineBuffer[i] = FUpper_LineBuffer[i-1];  }
        }

//Calculating Final Lower Line
      if(i == ATR_Range-1)
        {  FLower_LineBuffer[i] = BLower_LineBuffer[i]; }
      else
        {
         if((BLower_LineBuffer[i] > FLower_LineBuffer[i-1])||(close[i-1] < FLower_LineBuffer[i-1]))
           {   FLower_LineBuffer[i] = BLower_LineBuffer[i];    }
         else
           {   FLower_LineBuffer[i] = FLower_LineBuffer[i-1];  }
        }

//Calculating Super Trend Lines
      if((ST_LineBuffer[i-1] == FUpper_LineBuffer[i-1]))
        {
         if(close[i] <= FUpper_LineBuffer[i])
           {
            ST_LineBuffer[i] = FUpper_LineBuffer[i];
            ST_ColorBuffer[i]= 0;            
           }
          else
            {
            ST_LineBuffer[i] = FLower_LineBuffer[i];
            ST_ColorBuffer[i]= 1;             
            } 
        }

      if(ST_LineBuffer[i-1] == FLower_LineBuffer[i-1])
      {  
         if(close[i] >= FLower_LineBuffer[i])
          {         
          ST_LineBuffer[i] = FLower_LineBuffer[i];
          ST_ColorBuffer[i]= 1;           
          }
          else
            {
             ST_LineBuffer[i] = FUpper_LineBuffer[i];
             ST_ColorBuffer[i]= 0;
            }  
      }
}
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Copy only new values after the first run.

  
//Checking for start point of calculation
   int first = (prev_calculated==0) ? ATR_Range-1 : (rates_total!=prev_calculated) ? prev_calculated : prev_calculated-1;;
//Getting & Calling values in Handle
   int toCopy = (rates_total!=prev_calculated) ? rates_total-prev_calculated : 1;
   if(CopyBuffer(Handle,0,0,toCopy,Value_ATRBuffer) <=0)
     { return(0);   }

//Calculatinf Basic Upper and Lower Lines
   for(int i=first ; i<rates_total ; i++)
     {  
 
Ernst Van Der Merwe #:

Copy only new values after the first run.

Many thanks dear. Performance improved a lot. However, still not as fast as ATR. Also certain times, miscalculations are plotted.

Thanks for your guidance.

 
manjit-s #:

Many thanks dear. Performance improved a lot. However, still not as fast as ATR. Also certain times, miscalculations are plotted.

Thanks for your guidance.

Welcome. Calculating initial values outside the loop will only make a slight difference, so speed would depend on the custom indicator's code ("ATR_Manjit").

//Getting & Calling values in Handle
   int toCopy = (rates_total!=prev_calculated) ? rates_total-prev_calculated : 1;
   if(CopyBuffer(Handle,0,0,toCopy,Value_ATRBuffer) <=0)
     { return(0);   }

//Checking for start point of calculation
   int first =  (rates_total!=prev_calculated) ? prev_calculated : prev_calculated-1;

//Initial values
   if( prev_calculated==0)
     {
      first = ATR_Range-1;
      BUpper_LineBuffer[first] = FUpper_LineBuffer[first] = ((high[first] + low[first])/2)+(Multiplier * Value_ATRBuffer[first]);
      BLower_LineBuffer[first] = FLower_LineBuffer[first] = ((high[first] + low[first])/2)-(Multiplier * Value_ATRBuffer[first]);
      ST_LineBuffer[first] = (close[first] <= FUpper_LineBuffer[first]) ?  FUpper_LineBuffer[first] :  FLower_LineBuffer[first];
      first++;
     }

//Calculatinf Basic Upper and Lower Lines
   for(int i=first ; i<rates_total ; i++)
     {  
      BUpper_LineBuffer[i] = ((high[i] + low[i])/2)+(Multiplier * Value_ATRBuffer[i]);
      BLower_LineBuffer[i] = ((high[i] + low[i])/2)-(Multiplier * Value_ATRBuffer[i]);

//Calculating Final Upper Line
      if((BUpper_LineBuffer[i] < FUpper_LineBuffer[i-1])||(close[i-1] > FUpper_LineBuffer[i-1]))
        {   FUpper_LineBuffer[i] = BUpper_LineBuffer[i];    }
      else
        {   FUpper_LineBuffer[i] = FUpper_LineBuffer[i-1];  }

//Calculating Final Lower Line
      if((BLower_LineBuffer[i] > FLower_LineBuffer[i-1])||(close[i-1] < FLower_LineBuffer[i-1]))
        {   FLower_LineBuffer[i] = BLower_LineBuffer[i];    }
      else
        {   FLower_LineBuffer[i] = FLower_LineBuffer[i-1];  }
 
Ernst Van Der Merwe #:

Welcome. Calculating initial values outside the loop will only make a slight difference, so speed would depend on the custom indicator's code ("ATR_Manjit").

Many Many thanks dear. You have been very helpful and kind to spend time and support me.

After analyzing your inputs and studying my code, i observed that I was not calculating SuperTrend for very starting bar (i-1).

The code is working perfectly well after correcting.

Many many thanks sir.