【MT4指标】考夫曼自适应均线说明及源码(MQL4源码)
考夫曼自适应移动平均线最初是由Perry J. Kaufman在其著作Smarter Trading 精明交易者中提出的。该均线和我们通常见到的均线略微不同。我们常用的移动均线有简单移动均线、加权移动均线以及指数式移动均线三种,当然均线还有很多其他变种。对于均线我们知道他有两个比较突出的特性:长周期的均线趋势性可靠,但是有比较强的滞后性,短周期均线虽然能快速反映行情的趋势,但是由于其周期短,比较敏感的同时也会造成过多的“假”信号。那么是否有一种方法把两者优点结合起来,缺点进行规避那? 考夫曼提出了一种“自适应”的均线系统,他把快速均线和慢速均线进行结合,在市场沿一个方向快速移动时,使用快的移动平均值,而当价格在横盘的市场中拉锯时,使用慢速的移动平均值。
下面我们看一下该策略的源码:
//+------------------------------------------------------------------+
//| Kaufman.mq4 |
//| Copyright 2019, by geekquant|
//| http://www.geekquant.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, by geekquant"
#property link "http://www.geekquant.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Sienna
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Gold
//---- input parameters
extern int periodAMA=9;
extern int nfast=2;
extern int nslow=30;
extern double G=2.0;
extern double dK=2.0;
//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];
//+------------------------------------------------------------------+
int k=0, cbars=0, prevbars=0, prevtime=0;
double slowSC,fastSC;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,159);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,159);
//SetIndexDrawBegin(0,nslow+nfast);
SetIndexBuffer(0,kAMAbuffer);
SetIndexBuffer(1,kAMAupsig);
SetIndexBuffer(2,kAMAdownsig);
IndicatorDigits(4);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,pos=0;
double noise=0.000000001,AMA,AMA0,signal,ER;
double dSC,ERSC,SSC,ddK;
if (prevbars==Bars) return(0);
//---- TODO: add your code here
slowSC=(2.0 /(nslow+1));
fastSC=(2.0 /(nfast+1));
cbars=IndicatorCounted();
if (Bars<=(periodAMA+2)) return(0);
//---- check for possible errors
if (cbars<0) return(-1);
//---- last counted bar will be recounted
if (cbars>0) cbars--;
pos=Bars-periodAMA-2;
AMA0=Close;
while(pos>=0)
{
kAMAupsig=NULL;
kAMAdownsig=NULL;
if(pos==Bars-periodAMA-2) AMA0=Close;
signal=MathAbs(Close-Close);
noise=0.000000001;
for(i=0;i<periodAMA;i++)
{
noise=noise+MathAbs(Close-Close);
}
ER =signal/noise;
dSC=(fastSC-slowSC);
ERSC=ER*dSC;
SSC=ERSC+slowSC;
AMA=AMA0+(MathPow(SSC,G)*(Close-AMA0));
kAMAbuffer=AMA;
//----
ddK=(AMA-AMA0);
if ((MathAbs(ddK) > (dK*Point)) && (ddK > 0))kAMAupsig=AMA;
if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0))kAMAdownsig=AMA;
AMA0=AMA;
pos--;
}
//----
prevbars=Bars;
return(0);
}
//+------------------------------------------------------------------+
好东西,感谢分享
页:
[1]