iMAOnArray lag when used as MACD signal

 

Hello everyone, I'm recreating a MACD (I know there's dedicated function, just wanted to calcualte by myself) and when I calculate the signal line it makes my platform freeze for a bit, everytime I change signal line period I get a 10sec lag of the platofm. Someone can help point out why that happen? Thank's. 

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;
// Replacing loop function with the one you pointed out
   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
      double fast_ = iMA(_Symbol,PERIOD_CURRENT, ma1, 0, ma_type, PRICE_CLOSE, iBar);
      double slow_ =   iMA(_Symbol,PERIOD_CURRENT, ma2, 0, ma_type, PRICE_CLOSE, iBar);
      double macd = fast_ - slow_;

      buff_macd[iBar]  = macd;

      double filter_1 = iMAOnArray(buff_macd,0,fma1,0,ma_typem,iBar);
      buff_fma1[iBar] = filter_1;

     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
I s this code from  chatgpt?
 
Moatle Thompson #:
I s this code from  chatgpt?

No, why?

 
Just for specification, if I manually add 2 MA applied on MACD it does not lag at all. 
 

Extract iMAOnArray and buff_fma1 calculation to the separate loop, after one you now have.

 
Drazen Penic #:

Extract iMAOnArray and buff_fma1 calculation to the separate loop, after one you now have.

Why should I do this?? Can you please give an example? 

 
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;
// Replacing loop function with the one you pointed out
   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
      double fast_ = iMA(_Symbol,PERIOD_CURRENT, ma1, 0, ma_type, PRICE_CLOSE, iBar);
      double slow_ =   iMA(_Symbol,PERIOD_CURRENT, ma2, 0, ma_type, PRICE_CLOSE, iBar);
      double macd = fast_ - slow_;

      buff_macd[iBar]  = macd;

     }

   for(int iBar = Bars-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
      double filter_1 = iMAOnArray(buff_macd,0,fma1,0,ma_typem,iBar);
      buff_fma1[iBar] = filter_1;

     }



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


I'm not sure how iMAOnArray works, but it looks like it can somehow detect when you change value in array, and then it recalculates complete array, instead of just last value. 

 
Drazen Penic #:


I'm not sure how iMAOnArray works, but it looks like it can somehow detect when you change value in array, and then it recalculates complete array, instead of just last value. 

This is how it works:
 
Daniel Cioca #:
This is how it works:

I know what is iMAOnArray and what it does.

Thing that documentation does not say is why it takes longer to calculate when you change array in the loop, like the OP asked. 

 
ironhak:

Hello everyone, I'm recreating a MACD (I know there's dedicated function, just wanted to calcualte by myself) and when I calculate the signal line it makes my platform freeze for a bit, everytime I change signal line period I get a 10sec lag of the platofm. Someone can help point out why that happen? Thank's. 

Hi! Checked your code. On my platform works just fine … maybe is the platform… try this

 
Drazen Penic #:


I'm not sure how iMAOnArray works, but it looks like it can somehow detect when you change value in array, and then it recalculates complete array, instead of just last value. 

Perfect solution, thank you so much. 

Reason: