【MT4指标】考夫曼自适应均线说明及源码(MQL4源码)

1
回复
10627
查看
[复制链接]

95

主题

18

回帖

515

积分

高级会员

积分
515
来源: 2019-7-26 15:39:50 显示全部楼层 |阅读模式
     考夫曼自适应移动平均线最初是由Perry J. Kaufman在其著作Smarter Trading 精明交易者中提出的。该均线和我们通常见到的均线略微不同。
      我们常用的移动均线有简单移动均线、加权移动均线以及指数式移动均线三种,当然均线还有很多其他变种。对于均线我们知道他有两个比较突出的特性:长周期的均线趋势性可靠,但是有比较强的滞后性,短周期均线虽然能快速反映行情的趋势,但是由于其周期短,比较敏感的同时也会造成过多的“假”信号。那么是否有一种方法把两者优点结合起来,缺点进行规避那? 考夫曼提出了一种“自适应”的均线系统,他把快速均线和慢速均线进行结合,在市场沿一个方向快速移动时,使用快的移动平均值,而当价格在横盘的市场中拉锯时,使用慢速的移动平均值。


下面我们看一下该策略的源码:
  1. //+------------------------------------------------------------------+
  2. //|                                                      Kaufman.mq4 |
  3. //|                                   Copyright 2019, by geekquant  |
  4. //|                                        http://www.geekquant.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2019, by geekquant"
  7. #property link      "http://www.geekquant.com"
  8. #property indicator_chart_window
  9. #property indicator_buffers 3
  10. #property indicator_color1 Sienna
  11. #property indicator_color2 DeepSkyBlue
  12. #property indicator_color3 Gold
  13. //---- input parameters
  14. extern int       periodAMA=9;
  15. extern int       nfast=2;
  16. extern int       nslow=30;
  17. extern double    G=2.0;
  18. extern double    dK=2.0;
  19. //---- buffers
  20. double kAMAbuffer[];
  21. double kAMAupsig[];
  22. double kAMAdownsig[];
  23. //+------------------------------------------------------------------+
  24. int    k=0, cbars=0, prevbars=0, prevtime=0;
  25. double slowSC,fastSC;
  26. //+------------------------------------------------------------------+
  27. //| Custom indicator initialization function                         |
  28. //+------------------------------------------------------------------+
  29. int init()
  30.   {
  31. //---- indicators
  32.    SetIndexStyle(0,DRAW_LINE);
  33.    SetIndexStyle(1,DRAW_ARROW);
  34.    SetIndexArrow(1,159);
  35.    SetIndexStyle(2,DRAW_ARROW);
  36.    SetIndexArrow(2,159);
  37.    //SetIndexDrawBegin(0,nslow+nfast);
  38.    SetIndexBuffer(0,kAMAbuffer);
  39.    SetIndexBuffer(1,kAMAupsig);
  40.    SetIndexBuffer(2,kAMAdownsig);
  41.    IndicatorDigits(4);
  42. //----
  43.    return(0);
  44.   }
  45. //+------------------------------------------------------------------+
  46. //| Custom indicator deinitialization function                       |
  47. //+------------------------------------------------------------------+
  48. int deinit()
  49.   {
  50.    return(0);
  51.   }
  52. //+------------------------------------------------------------------+
  53. //| Custom indicator iteration function                              |
  54. //+------------------------------------------------------------------+
  55. int start()
  56.   {
  57.    int    i,pos=0;
  58.    double noise=0.000000001,AMA,AMA0,signal,ER;
  59.    double dSC,ERSC,SSC,ddK;
  60.    if (prevbars==Bars) return(0);
  61. //---- TODO: add your code here
  62.    slowSC=(2.0 /(nslow+1));
  63.    fastSC=(2.0 /(nfast+1));
  64.    cbars=IndicatorCounted();
  65.    if (Bars<=(periodAMA+2)) return(0);
  66. //---- check for possible errors
  67.    if (cbars<0) return(-1);
  68. //---- last counted bar will be recounted
  69.    if (cbars>0) cbars--;
  70.    pos=Bars-periodAMA-2;
  71.    AMA0=Close[pos+1];
  72.    while(pos>=0)
  73.      {
  74.       kAMAupsig[pos]  =NULL;
  75.       kAMAdownsig[pos]=NULL;
  76.       if(pos==Bars-periodAMA-2) AMA0=Close[pos+1];
  77.       signal=MathAbs(Close[pos]-Close[pos+periodAMA]);
  78.       noise=0.000000001;
  79.       for(i=0;i<periodAMA;i++)
  80.         {
  81.          noise=noise+MathAbs(Close[pos+i]-Close[pos+i+1]);
  82.         }
  83.       ER =signal/noise;
  84.       dSC=(fastSC-slowSC);
  85.       ERSC=ER*dSC;
  86.       SSC=ERSC+slowSC;
  87.       AMA=AMA0+(MathPow(SSC,G)*(Close[pos]-AMA0));
  88.       kAMAbuffer[pos]=AMA;
  89. //----
  90.       ddK=(AMA-AMA0);
  91.       if ((MathAbs(ddK) > (dK*Point)) && (ddK > 0))  kAMAupsig[pos]  =AMA;
  92.       if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0))  kAMAdownsig[pos]=AMA;
  93.       AMA0=AMA;
  94.       pos--;
  95.      }
  96. //----
  97.    prevbars=Bars;
  98.    return(0);
  99.   }
  100. //+------------------------------------------------------------------+
复制代码

20190726.png

回复

使用道具 举报

41

主题

32

回帖

248

积分

中级会员

积分
248
2019-7-30 03:34:22 来自手机 显示全部楼层
好东西,感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信