dynamic change of ma period - efficient formula

 

Hello,

i am looking for an efficient formular to change period of a moving average.

This article 

 https://www.mql5.com/en/code/75 

 

 seems to promise this but after my opinion this is not the case as the recursion used

 

VIDYA_Buffer[i]=price[i]*ExtF*mulCMO+VIDYA_Buffer[i-1]*(1-ExtF*mulCMO);

 

would not work if period really changed - in particular, if period would be raised.

 

Has anyone worked out an efficient formula to do this . It should work even if period is raised or lowered by more than 1.

Thank you 

Variable Index Dynamic Average (VIDYA)
Variable Index Dynamic Average (VIDYA)
  • votes: 10
  • 2010.02.03
  • MetaQuotes Software Corp.
  • www.mql5.com
This oscillator measures the ratio between the sum of positive increments and sum of negative increments for a certain period.
 
chinaski:

Hello,

i am looking for an efficient formular to change period of a moving average.

This article 

 https://www.mql5.com/en/code/75 

 

 seems to promise this but after my opinion this is not the case as the recursion used

 

 

would not work if period really changed - in particular, if period would be raised.

 

Has anyone worked out an efficient formula to do this . It should work even if period is raised or lowered by more than 1.

Thank you 

Do you have any suggestion?


 
This is my suggestion. Comments appreciated
void simple_ma_dynamic(int rates_total
                            ,int prev_calculated
                            ,int begin
                            ,const double &price[]
                            ,int period		    // current period
                            ,int period_prev        // period from last call
                            ,double &dataBuffer[]) export
  {
  
    if(period + begin > rates_total || period_prev + begin > rates_total)
        return;
    int i,limit;
    //--- first calculation or number of bars was changed
    if(prev_calculated==0)// first calculation
    {
        limit=period+begin;
        //--- set empty value for first limit bars
        for(i=0;i<limit-1;i++) 
            dataBuffer[i]=0.0;
        //--- calculate first visible value
        double firstValue=0;
        for(i=begin;i<limit;i++)
            firstValue+=price[i];
        firstValue/=period;
        dataBuffer[limit-1]=firstValue;
    }
    else 
        limit=prev_calculated-1;
    /// period not changed or first call
    if(period == period_prev || period_prev == 0)
    {        
        for(i=limit;i<rates_total && !IsStopped();i++)
        {
            dataBuffer[i]=dataBuffer[i-1]+(price[i]-price[i-period])/period;
        }
       
    }
    /// period changed
    else 
    {
        double sum=0;
	/// slow but exactly...efficient algorithms welcome
        for(i=rates_total-period;i < rates_total;++i)
            sum += price[i];
        dataBuffer[rates_total-1]=sum / period;
    }
    //---
  }

 
chinaski:
This is my suggestion. Comments appreciated

This filter (I do not know the name of this) is in ATR, by MetaQuotes:

ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-ExtPeriodATR])/ExtPeriodATR;

And I think it`s better than the most used EMA to get less lag:

double pr=2.0/(AtrPeriod+1);
AtrBuffer[i]=TempBuffer[i]*pr+AtrBuffer[i+1]*(1-pr);

In VIDYA, I tried others things in the place of CMO, but no success...





 
Well, i see no period changing...?
 
chinaski:
Well, i see no period changing...?

No... Just less lag. EMA has less lag than your filter, but other, has less peaks. I think.

But you can try something like this for period changing: Dr. Tradelove or How I Stopped Worrying and Created a Self-Training Expert Advisor.

=)

 

Sorry, it was not about EMA or Simple or what ever.

 It is about an efficient dynamic MA, EMA or Simple or smoothed.

So basically an MA which can change its period on each call and this efficiently as possible (what mostly means recursive).


Thank you for link above but it is also not really related to this question. 

 
the code above is doing what i need, however, possibly there is some faster code.
Reason: