calling twice a function with a "static int" causes problems

 

I want to use LWMA (the official LWMA in Custom Moving Averages.mq4) twice with two PERIODS, one short and one long,  and those averages will be in 2 buffers, SHORTBUFFER and LONGBUFFER,  to print them. BUT, LWMA has a "static int" and so the results of the first SHORTBUFFER are messed up. The results of the LONGBUFFER are fine.


so first I just define my arrays

double SHORTBUFFER[];

double LONGBUFFER[];


   int    draw_beginSHORT=InpMAPeriodSHORT-1;

   SetIndexDrawBegin(0,draw_beginSHORT);
//--- indicator buffers mapping
   SetIndexBuffer(0, SHORTBUFFER );


   int    draw_beginLONG=InpMAPeriodLONG-1;

   SetIndexDrawBegin(1,draw_beginLONG);
//--- indicator buffers mapping
   SetIndexBuffer(1, LONGBUFFER );


and I populate the buffers like this


CalculateLWMA(rates_total,prev_calculated, ""Some Generic array of which I want LWMA with short period",  SHORTBUFFER, InpMAPeriodSHORT );

CalculateLWMA(rates_total,prev_calculated, ""Some Generic array of which I want LWMA with long period", LONGBUFFER,  InpMAPeriodLONG );


[If I reverse the order of the calls, ie first populating LONGBUFFER, then SHORTBUFFER, then it is the LONGBUFFER which has garbage values]

and i modify slightly the official LWMA to get

//+------------------------------------------------------------------+
//|  linear weighted moving average                                  |
//+------------------------------------------------------------------+
void CalculateLWMA(int rates_total,int prev_calculated, double &GENERICARRAY[], &ARRAYtoPOPULATE[], CustomPERIOD)
  {
   int        i,limit;
   static int weightsum;
   double     sum;
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      weightsum=0;
      limit=CustomPERIOD;
      //--- calculate first visible value
      double firstValue=0;
      for(i=0;i<limit;i++)
        {
         int k=i+1;
         weightsum+=k;
         firstValue+=k*GENERICARRAY[i];
        }
      firstValue/=(double)weightsum;
      ARRAYtoPOPULATE[limit-1]=firstValue;
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total && !IsStopped(); i++)
     {
      sum=0;
      for(int j=0;j<CustomPERIOD;j++)
         sum+=(CustomPERIOD-j)*GENERICARRAY[i-j];
      ARRAYtoPOPULATE[i]=sum/weightsum;
     }
//---
  }
If I remove the ''static'' in the LWMA, then I get an error '' zero divide''' at line 
ARRAYtoPOPULATE[i]=sum/weightsum;
 

Hi and good morning,

have a look at the MQL4\Include\MovingAverages.mph - maybe this could help you.

Best regards

 

That's interesting I tried to populate my Buffer with the MA_arrays, but they don't get updated to the current bar. 


and I put this into this loop, like I did for " somearray__to_take_average_of "


ArraySetAsSeries( somearray__to_take_average_of, true);
int UnchangedBars = IndicatorCounted();
int ChangedBars= Bars - UnchangedBars;
for (int q=ChangedBars-1; q >= 0; q--) {

BUFFER[q]= ExponentialMA[q,  InpMAperiod,  BUFFER[q-1],  somearray__to_take_average_of ] 

 
}
 
If I remove the ''static'' in the LWMA, then I get an error '' zero divide''' at line 

The "zero divide" is easy to solve without static.


if(weightsum != 0)
Reason: