Code Library / Moving Average
MQL5 Adaptive Moving Average
Kaufman Adaptive Moving Average (KAMA) for MQL5.
Language: MQL5 · Category: Moving Average · Tags: adaptive MA, KAMA, MQL5, indicator, noise filter
Source code
double KAMA(const double &p[], int period, int fastP, int slowP, int shift) {
static double prev=0;
if(shift>=ArraySize(p)-period){prev=p[shift];return prev;}
double dir=MathAbs(p[shift]-p[shift+period]),vol=0;
for(int i=shift;i<shift+period;i++) vol+=MathAbs(p[i]-p[i+1]);
double er=vol!=0?dir/vol:0;
double sc=MathPow(er*(2.0/(fastP+1)-2.0/(slowP+1))+2.0/(slowP+1),2);
prev=prev+sc*(p[shift]-prev); return prev;
}How to use this snippet
This is a building block, not a finished trading system. Paste it into your own MQL5 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.