关于KDJ指标的MQL4的实现

0
回复
9718
查看
[复制链接]

53

主题

8

回帖

836

积分

管理员

积分
836
来源: 2019-11-26 23:39:02 显示全部楼层 |阅读模式
简单的说,KDJ 有四个要计算的东西。RSV ,K, D, J

RSV: 中文翻译过来就是 未成熟值,这个值实际上也不神秘。
它其实就是著名的威廉指数。计算公式如下:

设:周期为 Period
RSV = (close - low[i, Period]) / (high[i, Period] - low[i, Period])

这个计算也是这个指标的核心部分,K D J 这三个参数都依赖与 RSV.
我举个例子,关于如果计算 RSV

假设,下面是一个日线
9 8 7 6 5 4 3 2 1 0    (i)  表示离今天的天数
3 1 2 1 2 3 4 1 2 4    (price) 表示价格

最右边的就是今天的价格,为 4元
我设置 周期为 Period = 3
low[0, 3] = min(price[0], price[1], price[2]) 往前数3天,包括今天,然后求价格的最小值
low[0, 3] = min(4, 2 , 1) = 1

同样:
high[0, 3] = max (4, 2, 1) = 4
close 就是当天收盘价格 close[0] = 4
带入上面的公式就可以求出 RSV
RSV = (4 - 1)/ (4 - 1) * 100 = 100
这个RSV 是和超买超买相关的一个值。

有了这RSV,其他的就很容易计算
K = (2/3) * K[i + 1] + (1/3) * RSV  注意,如果i 表示今天,那么 i+ 1 就表示昨天
D = (2/3) * D[i + 1] + (1/3) * K
J = 3D - 2K

你会发现这样一个事实,那就是如果 RSV 大了 20, 那么只有 1/3 被传递到 K
只有 1/9 被传递到 D, 所以,后面的K , D 的变化节奏会比 RSV 慢一些。
  1. //+------------------------------------------------------------------+
  2. //|                                                          KDJ.mq4 |
  3. //|                                        http://www.metaquotes.net |
  4. //+------------------------------------------------------------------+
  5. #property indicator_separate_window
  6. #property indicator_minimum 0
  7. #property indicator_maximum 100
  8. #property indicator_buffers 8
  9. #property indicator_level1 20
  10. #property indicator_level2 80
  11. #property indicator_level3 50

  12. #property indicator_color1 MediumSlateBlue
  13. #property indicator_color2 0x00ff00
  14. #property indicator_color3 Red
  15. #property indicator_color4 Yellow

  16. //---- input parameters
  17. extern int       KdjPeriod=30;
  18. //---- buffers
  19. double RSV[],K[],D[],J[];
  20. //+------------------------------------------------------------------+
  21. //| Custom indicator initialization function                         |
  22. //+------------------------------------------------------------------+
  23. int init()
  24. {
  25.    string short_name;
  26.    short_name="KDJ(" + KdjPeriod + ") " + "King Wang";

  27.    IndicatorShortName(short_name);
  28.    SetIndexLabel(0,"RSV:");
  29.    SetIndexLabel(1,"K");
  30.    SetIndexLabel(2,"D:");
  31.    SetIndexLabel(3,"J");

  32.    IndicatorDigits(Digits-2);

  33.    SetIndexStyle(0,DRAW_LINE);
  34.    SetIndexBuffer(0,RSV);
  35.    
  36.    SetIndexStyle(1,DRAW_LINE);
  37.    SetIndexBuffer(1,K);

  38.    SetIndexStyle(2,DRAW_LINE);
  39.    SetIndexBuffer(2,D);

  40.    SetIndexStyle(3,DRAW_LINE);
  41.    SetIndexBuffer(3,J);

  42.    return(0);
  43. }

  44. //+------------------------------------------------------------------+
  45. //| Custom indicator deinitialization function                       |
  46. //+------------------------------------------------------------------+

  47. int deinit()
  48. {
  49.    return(0);
  50. }

  51. //+------------------------------------------------------------------+
  52. //| Custom indicator iteration function                              |
  53. //+------------------------------------------------------------------+
  54. int start()
  55. {
  56.    int counted_bars=IndicatorCounted();
  57.    if (counted_bars < 0) {
  58.        Alert("IndicatorCounted error.");
  59.        return;
  60.    }

  61.    int i;
  62.    if(Bars <= KdjPeriod) return(0);
  63.    int limit = Bars-counted_bars-1;

  64.    i = limit;
  65.    double MaxHigh=0,MinLow=0;
  66.    while(i >= 0)
  67.    {
  68.       MaxHigh=High[iHighest(NULL,0,MODE_HIGH, KdjPeriod, i)];
  69.       MinLow=Low[iLowest(NULL,0,MODE_LOW, KdjPeriod, i)];
  70.       RSV[i]=(Close[i]-MinLow)/(MaxHigh-MinLow)*100;
  71.       i--;
  72.    }

  73.    i = limit;
  74.    while(i >= 0)
  75.    {
  76.        if (i == Bars - 1) { //第一个点
  77.            K[i] = RSV[i];
  78.            i--;
  79.            continue;
  80.        }
  81.        K[i] = (2 * K[i+1] + RSV[i]) / 3;
  82.        i--;
  83.    }

  84.    i = limit;
  85.    while(i >= 0)
  86.    {
  87.        if (i == Bars - 1) { //第一个点
  88.            D[i] = RSV[i];
  89.            i--;
  90.            continue;
  91.        }
  92.        D[i] = (2 * D[i+1] + K[i]) / 3;
  93.        i--;
  94.    }

  95.    i = limit;
  96.    while (i >= 0)
  97.    {
  98.       J[i]=3*D[i]-2*K[i];
  99.       if(J[i] < 0)   J[i]=0;
  100.       if(J[i] > 100) J[i]=100;
  101.       i--;
  102.    }
  103.    return(0);
  104. }
复制代码



回复

使用道具 举报

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