Help please with adaptive moving average indicator

 

I am trying to develop an adaptive moving average indicator based on Perry Kaufman's method. So far, I have modelled the indicator in excel and I am now trying to code it in MQL4. I haven't gotten very far and could do with some help if anyone would be kind enough?

Here is my code to date:

//+------------------------------------------------------------------+
//|                                                     K_AMA_V1.mq4 |
//|                                         Copyright © 2010, Oneday |
//|                                                   |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Oneday"
#property link      

#property indicator_chart_window

extern int Momentum_period = 10;//setting used in Kaufman example

double   Momentum; //compares current close with close x bars before
double   one_day_difference;// calculate differnce in value of current close and previous close
double   Mom_Per_day_difference;// calculate the the absolute value of the current close + the momentum period number of closes
double   Efficiency_ratio;// calculate the absolute value of Momentum/Mom_Per_day_difference
double   Smoothing_variable;//=(Efficiency_ratio*(FAST_SMOOTH-SLOW_SMOOTH)+SLOW_SMOOTH)^2

double   MomBuffer[];
int      counter_val;//USED IN LOOPS

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,MomBuffer);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexDrawBegin(0,Momentum_period);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   double Fast_smooth_rate = (2.0/(2.0+1.0));//PK recommends value of 2
   Print("Fast_smooth_rate is ",DoubleToStr(Fast_smooth_rate,4));
   double Slow_smooth_value = (2.0/(30.0+1.0));//PK recommends value of 30
   Print("Slow_smooth_value is ",Slow_smooth_value);
   //double MomBuff = Close[10];
   //Print("MomBuff = ",MomBuff);
   
   int indexNUM;//bar index number
   int countedBARS = IndicatorCounted();//defining value for counted bars variable
   
   if(Bars<=Momentum_period) return(0);
   
   if(countedBARS<1)
      for(indexNUM=1;indexNUM<=Momentum_period;indexNUM++) MomBuffer[Bars-indexNUM]=0.0;
   
   indexNUM = Bars - Momentum_period - 1;//index number 
   
   indexNUM=Bars-Momentum_period-1;
   if(countedBARS>=Momentum_period) indexNUM=Bars-countedBARS-1;
   while(indexNUM>=0)//calculate the 10 day momemtum
     {
      MomBuffer[indexNUM]=Close[indexNUM]-Close[indexNUM+Momentum_period];
      indexNUM--;
     }
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

I have adapted the custom momentum indicator to calculate the momentum over a 10 day period along with some of the other variable values. What I need to do next is calculate the absolute 1 day differences in value during the 10 day period then add these up to calculate the 10 day difference. Here is how I do this with excel:

In order to achieve this, I think that I need to use a variation of the momentum calculation to calculate the 1 day difference then, collect these in an array?? This is where I could really do with some help or ideas - how do I do this with MQL4?

Thanks in advance

 

is something wrong with the kama already posted here: https://www.mql5.com/en/code/9167 or do you want to reinvent the wheel?

//z

 
zzuegg:

is something wrong with the kama already posted here: https://www.mql5.com/en/code/9167 or do you want to reinvent the wheel?

//z


What's wrong with reinventing the wheel?

You are correct that I do appear to be doing this, the reason being that in Kaufman's original example, he goes on to further calculations for buy and sell signals. I am trying to work through the various stages so that I can incorporate this into the indicator as I want to add it to my weekly charts and use it as a trend filter on the daily charts.

Thank you Zzuegg for the link - I'll try and figure out how it works (still struggling with MQL4!) so that I can adapt it to my needs.

Cheers

 
oneday:


What's wrong with reinventing the wheel?

You are correct that I do appear to be doing this, the reason being that in Kaufman's original example, he goes on to further calculations for buy and sell signals. I am trying to work through the various stages so that I can incorporate this into the indicator as I want to add it to my weekly charts and use it as a trend filter on the daily charts.

Thank you Zzuegg for the link - I'll try and figure out how it works (still struggling with MQL4!) so that I can adapt it to my needs.

Cheers

Still struggling to understand Bars and loop counting. Here is a snippet of the momentum code:

int start()
  {
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=MomPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=MomPeriod;i++) MomBuffer[Bars-i]=0.0;
//----
   i=Bars-MomPeriod-1;
      //Print("bars =",Bars);
      //Print("i = ",i);
      //Print("count = ",counted_bars);
   if(counted_bars>=MomPeriod) i=Bars-counted_bars-1;
      //Print("i = ",i);
   while(i>=0)
     {
      MomBuffer[i]=Close[i]-Close[i+MomPeriod];
      i--;
     }
   return(0);
  }

Could somebody please clarify this for for me, is: i=Bars-MomPeriod-1; the first value of the while loop counter?

At the end of the first cycle this value is reduced by 1?

Does this continue until the: if(counted_bars>=MomPeriod) i=Bars-counted_bars-1; is true - because "i" will be set to zero if true?

Cheers