Extreme lag plotting a simple moving average...

 

Hello everyone, I don't understand why this simple indicator cause so much lag... I have 150'000 bars on chart and loading this indicator literally freeze my mt4 forcing me to restart it, I can't understand why if I load the default MA indicator that does not happen. 

Here's the code: 

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 lookback = 1000; 
   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {

      buff_ma_1[iBar] =  iMA(Symbol(),0,ma_period,iBar,1,0,0);
      
     }
   return rates_total-1; // Recalculate current bar next tick.
// Return value of prev_calculated for next call
  };


Someone can explain? Thanks'. 

 
  1. ironhak: Someone can explain? Thanks'. 
    buff_ma_1[iBar] =  iMA(Symbol(),0,ma_period,iBar,1,0,0);

    Because, you are creating 150'000 different moving averages. One with no displacement (iBar=zero), one with a displacement of one, etc. As opposed to one moving average and reading different bars.

    iMA(_Symbol, _Period, ma_period, 0, MODE_EMA, PRICE_CLOSE, iBar);
  2. Do not hard code things. Use the proper constants.

  3. Your look back for moving averages is the MA length.
              How to do your lookbacks correctly #9#14 & #19 (2016)

 
William Roeder #:
  1. Because, you are creating 150'000 different moving averages. One with no displacement (iBar=zero), one with a displacement of one, etc. As opposed to one moving average and reading different bars.

  2. Do not hard code things. Use the proper constants.

  3. Your look back for moving averages is the MA length.
              How to do your lookbacks correctly #9#14 & #19 (2016)

Thank you William, I have a few questions: 

  1. What's the difference between using `Symbol()`and `_Symbol`? And between "0" and `_Period`? 
  2. What do you mean with " Do not hard code things" ? 

I would just like to understand this for future because many times I write codes that makes my platform extremely slow. Thank you so much. 

 
ironhak #: What do you mean with " Do not hard code things" ?
buff_ma_1[iBar] =  iMA(Symbol(),0,ma_period,iBar,1,0,0);
                                                   ^ what does zero mean here?
                                                 ^ what does one mean here?
                                ^ what does zero mean here?

Don't hard code constants, use the enumerations provided.

iMA(_Symbol, _Period, ma_period, 0, MODE_EMA, PRICE_CLOSE, iBar);
                                              ^--------------^ is there any guessing what this means?
                                    ^-------------^ is there any guessing?
             ^-----^ is there any guessing? Is PERIOD_CURRENT also not clear?
 
William Roeder #:

Don't hard code constants, use the enumerations provided.

As you can see on documentation there are respective values: 

Thought that typing "1" is equal to type "MODE_EMA". Clearly it's not, thank you. 

 
ironhak #:

As you can see on documentation there are respective values: 

Thought that typing "1" is equal to type "MODE_EMA". Clearly it's not, thank you. 

Yes it is.

William's point is that by using the actual Enums it makes the code much easier to read. That is most important when asking other people to check your code.

If I see  MODE_EMA, I know exactly what it is. If I see 1, I may not remember and probably can't be bothered to check the documentation to see what it equates to. So I will be one less person likely to help you with any problem.

 
ironhak #:

As you can see on documentation there are respective values: 

Thought that typing "1" is equal to type "MODE_EMA". Clearly it's not, thank you. 

They are equal, that is not the point.

It is unreadable. In your call, you have two zeros and they mean two totally different things.

It is not reliable. If you convert MT4 to MT5 and you use 60 instead of PERIOD_H1, your code will no longer work.

 
Everything clear, thank’s so much for your help guys.
Reason: