MT4指标三线KDJ指标下载

在MT4上,是没有三线KDJ指标的,分享KDJ指标源码:

  1 #property copyright "Copyright 2020"
  2 #property link      "https://www.mql5.com"
  3 #property version   "1.00"
  4 #property strict
  5 #property indicator_separate_window
  6 #property indicator_buffers 3
  7 #property indicator_plots   3
  8 //--- plot KLine
  9 #property indicator_label1  "KLine"
 10 #property indicator_type1   DRAW_LINE
 11 #property indicator_color1  clrWhite
 12 #property indicator_style1  STYLE_SOLID
 13 #property indicator_width1  1
 14 //--- plot DLine
 15 #property indicator_label2  "DLine"
 16 #property indicator_type2   DRAW_LINE
 17 #property indicator_color2  clrGold
 18 #property indicator_style2  STYLE_SOLID
 19 #property indicator_width2  1
 20 //--- plot JLine
 21 #property indicator_label3  "JLine"
 22 #property indicator_type3   DRAW_LINE
 23 #property indicator_color3  clrDarkViolet
 24 #property indicator_style3  STYLE_SOLID
 25 #property indicator_width3  1
 26 
 27 #property indicator_levelstyle STYLE_DOT
 28 #property indicator_levelcolor clrSilver
 29 #property indicator_level1  0
 30 #property indicator_level2  20
 31 #property indicator_level3  50
 32 #property indicator_level4  80
 33 #property indicator_level5  100
 34 
 35 
 36 //---- input parameters
 37 input int N =9;//%K 周期
 38 input int M1=3;//%D 周期
 39 input int M2=3;//慢速
 40 //--- indicator buffers
 41 double         KBuffer[];
 42 double         DBuffer[];
 43 double         JBuffer[];
 44 double llv[],hhv[],rsv[];
 45 double p=0,p1=0;
 46 double f=0,f1=0;
 47 //+------------------------------------------------------------------+
 48 //| Custom indicator initialization function                         |
 49 //+------------------------------------------------------------------+
 50 int OnInit()
 51   {
 52 //--- indicator buffers mapping
 53    IndicatorBuffers(6);
 54    SetIndexBuffer(0,KBuffer);
 55    SetIndexBuffer(1,DBuffer);
 56    SetIndexBuffer(2,JBuffer);
 57    SetIndexBuffer(3,llv,INDICATOR_CALCULATIONS);
 58    SetIndexBuffer(4,hhv,INDICATOR_CALCULATIONS);
 59    SetIndexBuffer(5,rsv,INDICATOR_CALCULATIONS);
 60 
 61    for(int i=0; i<6; i++)
 62      {
 63       SetIndexDrawBegin(i,N+M1+M2);
 64      }
 65 
 66 
 67    SetLevelValue(0,0);
 68    SetLevelValue(1,20);
 69    SetLevelValue(2,50);
 70    SetLevelValue(3,80);
 71    SetLevelValue(4,100);
 72 
 73    string name = "KDJ("+ (string)N+","+(string)M1+","+(string)M2+")";
 74    IndicatorShortName(name);
 75 
 76    IndicatorDigits(2);
 77 
 78    if(N<=0||M1<=0||M2<=0)
 79       return(INIT_FAILED);
 80 
 81    p = 1.0/M1;
 82    p1 = 1-p;
 83    f = 1.0/M2;
 84    f1 = 1-f;
 85 
 86 
 87 
 88 //---
 89    return(INIT_SUCCEEDED);
 90   }
 91 //+------------------------------------------------------------------+
 92 //| Custom indicator iteration function                              |
 93 //+------------------------------------------------------------------+
 94 int OnCalculate(const int rates_total,
 95                 const int prev_calculated,
 96                 const datetime &time[],
 97                 const double &open[],
 98                 const double &high[],
 99                 const double &low[],
100                 const double &close[],
101                 const long &tick_volume[],
102                 const long &volume[],
103                 const int &spread[])
104   {
105 //---
106    int i,limit=0;
107    if(rates_total<=0)
108       return(0);
109    if(prev_calculated<=0)
110       limit=rates_total-1;
111    else
112       limit = rates_total - prev_calculated +1;
113 
114    for(i=limit; i>=0; i--)
115      {
116       llv[i]=0;
117       hhv[i]=0;
118       if(i>rates_total-N)
119          continue;
120       int shift = iLowest(NULL,0,MODE_LOW,N,i);
121       llv[i] =  low[shift];
122       shift = iHighest(NULL,0,MODE_HIGH,N,i);
123       hhv[i] = high[shift];
124      }
125    for(i=limit; i>=0; i--)
126      {
127       rsv[i] = 0;
128       if(hhv[i]>0 && llv[i]>0 && (hhv[i]-llv[i])!=0)
129          rsv[i] = (close[i]-llv[i])/(hhv[i]-llv[i])*100;
130      }
131 
132    for(i=limit; i>=0; i--)
133      {
134       if(i==rates_total-1)
135          KBuffer[i]=0;
136       else
137         {
138          KBuffer[i] = rsv[i]*p + KBuffer[i+1]*p1;
139         }
140      }
141 
142    for(i=limit; i>=0; i--)
143      {
144       if(i==rates_total-1)
145          DBuffer[i]=0;
146       else
147         {
148          DBuffer[i] = KBuffer[i]*f + DBuffer[i+1]*f1;
149         }
150      }
151 
152    for(i=limit; i>=0; i--)
153      {
154       JBuffer[i] = 3*KBuffer[i] - 2*DBuffer[i];
155      }
156 
157 
158 //--- return value of prev_calculated for next call
159    return(rates_total);
160   }
161 //+------------------------------------------------------------------+

 

posted @ 2021-10-25 12:17  迷失的阿尔法  阅读(1591)  评论(0)    收藏  举报