Indicator is slow and don't work in other time than 1 hour, why?

 

Hi, 

I was trying to built the Acceleration Bands indicator. I finished it, it looks okay, except by it does not seems to work in other time than 1 hour and takes long time to plot into the chart. 

If someone knows why, please help. I will provide it for free in this forum.

 Thank you

 rodrigosm

//+------------------------------------------------------------------+
//|                                       Acceleration_Bands.mq5     |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 2
#property indicator_label1  "Bands upper"
#property indicator_label2  "Bands lower"

#include <MovingAverages.mqh>


//--- External Buffers to plot
double               HighBand[];
double               LowBand[];
//--- Auxiliar Buffers to use into the calculus
double               AuxHighBand[];
double               AuxLowBand[];

//--- Parameters
input int                  periodo=20;
input double               Deviation=2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   //--- Bind the Array to the indicator buffer with index 0
   SetIndexBuffer(0,HighBand,INDICATOR_DATA);
   SetIndexBuffer(1,LowBand,INDICATOR_DATA);
   SetIndexBuffer(2,AuxHighBand,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,AuxLowBand,INDICATOR_CALCULATIONS);

//--- Set the line drawing
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
   PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_LINE);

//--- Set the style line
   PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_DOT);
   PlotIndexSetInteger(1,PLOT_LINE_STYLE,STYLE_DOT);
//--- Set line color
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrRed);
   PlotIndexSetInteger(1,PLOT_LINE_COLOR,clrRed);
//--- Set line thickness
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);
   PlotIndexSetInteger(1,PLOT_LINE_WIDTH,1);
//--- Set labels for the line
   PlotIndexSetString(0,PLOT_LABEL,"Up Band Average");
   PlotIndexSetString(1,PLOT_LABEL,"Down Band Average");
//--- indexes draw begin settings
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,periodo);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,periodo);
//---
   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[])
  {
//--- check for bars count
   if(rates_total<periodo)
      return(0);
//--- Start of our main calcululation
  int pos;
  
  if(prev_calculated>1) pos=prev_calculated-1;
  else pos=0;
  
  for(int i=pos;i<rates_total && !IsStopped();i++)
      {

//--- First part of our upBand - without average
      AuxHighBand[i]=high[i]*(1+Deviation*(high[i]-low[i])/((high[i]+low[i])/2));
//--- Second part of our upBand - with average
           
      ExponentialMAOnBuffer(rates_total,
                            prev_calculated,
                            1,  // index, starting from which data for smoothing are available 
                            periodo,  // period of the exponential average
                            AuxHighBand,       // buffer to calculate average
                            HighBand);  // into this buffer locate value of the average
                            
//--- First part of our LowBand - without average
      AuxLowBand[i]=low[i]*(1-Deviation*(high[i]-low[i])/((high[i]+low[i])/2));
//--- Second part of our LowBand - with average
           
      ExponentialMAOnBuffer(rates_total,
                            prev_calculated,
                            1,  // index, starting from which data for smoothing are available 
                            periodo,  // period of the exponential average
                            AuxLowBand,       // buffer to calculate average
                            LowBand);  // into this buffer locate value of the average
//---                           

     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Your indicator was written in non effective way. You should read the article 3 Methods of Indicators Acceleration by the Example of the Linear Regression.
 

Did you mean non efficient way, Rosh ? 

There's already a loop right inside ExponentialMAOnBuffer() function, and what makes your CI very slow, is that you call this function twice from inside a main loop. A 2 loops within a main loop. I don't blame you. This is typical MQL4 programmers, where he would simply call iMA () function specify MODE_EMA, and just changing last parameter of bar shift. :( 

 

 
IMa fails on a different period unless you have that period open on a 2nd window
https://www.mql5.com/en/forum/6760
BUG REPORT - Multi timeframe IMA copybuffer error
  • www.mql5.com
BUG REPORT - Multi timeframe IMA copybuffer error.