Download MetaTrader 5

KD indicator

To add comments, please log in or register
Share your knowledge. Become a well-known developer!
yalewang
606
yalewang 2016.03.14 16:22 

I have coded KD indicator that is used in stock market into MQL4 program ,but feel difficult to code into mql5 program .

Is there someone interesting in this program ? code it and share it with all .

#property copyright ""
#property link      ""
#property version   "1.00"
#property strict
#include <MovingAverages.mqh>
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2
#property indicator_color3 White
#property indicator_width3 1
#property indicator_color4 White
#property indicator_width4 1

input int n1=100; 
input int n2=3;
input int n3=3;


double b1[];
double b2[];
double b3[];
double lup[];
double ldn[];
/////////////////////////////////////////////////////
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_LINE,STYLE_DOT,1);
   SetIndexStyle(3,DRAW_LINE,STYLE_DOT,1);
   
   SetIndexBuffer(0,b2);
   SetIndexBuffer(1,b3);
   SetIndexBuffer(2,lup);
   SetIndexBuffer(3,ldn);
   SetIndexBuffer(4,b1);
   
   SetIndexLabel(0,"k");
   SetIndexLabel(1,"d");
   SetIndexLabel(2,"up");
   SetIndexLabel(3,"dn");
   Print("initialserro=",GetLastError());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   //ArraySetAsSeries(b1,false);
   //ArraySetAsSeries(b2,false);
   //ArraySetAsSeries(b3,false);
   //ArraySetAsSeries(lup,false);
   //ArraySetAsSeries(ldn,false);
   //ArraySetAsSeries(close,false);
   //////////////////////////////////////////////
   int i;int i1;
   int mm=rates_total-prev_calculated-1;
   //while(i>=0)
   for(i=0;i<=mm;i++)  
     {
      if(i==rates_total-1) 
       { 
        b1[i]=0 ;       
        ldn[i]=20;        lup[i]=80;
       }
      else
        {         
         if((High[iHighest(NULL,0,MODE_HIGH,n1,i)]-Low[iLowest(NULL,0,MODE_LOW,n1,i)])!=0)
         b1[i]=(Close[i]-Low[iLowest(NULL,0,MODE_LOW,n1,i)])/(High[iHighest(NULL,0,MODE_HIGH,n1,i)]-Low[iLowest(NULL,0,MODE_LOW,n1,i)])*100;
         else  b1[i]=b1[i-1];
         ldn[i]=20;         lup[i]=80;

        }
      //i--;
     }
   //if(counted_bars>0)  counted_bars--;
   int limit=rates_total-prev_calculated;
   for(i1=0; i1<limit; i1++)  
   {
     b2[i1]= iMAOnArray(b1,rates_total,n2,0,MODE_EMA,i1);
   }
   for(i1=0; i1<limit; i1++)   
   {
     b3[i1]= iMAOnArray(b2,rates_total,n3,0,MODE_EMA,i1);
   }
   
   //SimpleMAOnBuffer(rates_total,prev_calculated,0,n2,b1,b2);
   //SimpleMAOnBuffer(rates_total,prev_calculated,0,n3,b2,b3);
   
   if(GetLastError()!=0) Print("runerro=",GetLastError());
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Carl Schreiber
6590
Carl Schreiber 2016.03.14 17:27  
can you explain what is the difference to the normal Stochastic indicator?
yalewang
606
yalewang 2016.03.15 15:22  
Carl Schreiber:
can you explain what is the difference to the normal Stochastic indicator?

thank you . I compare KD and Sochastic indicator ion MT4 platerm ,they are quite the same ,but use the same parameter ,the KD is slight smooth .

On MT5 platerform ,the parameters of Stochastic is different ,they can not be adjust smaller , and can not get the same lines as on MT4.

yalewang
606
yalewang 2016.03.15 15:48  

here is another stock indicator ,it is very accurate to indicate the buy enter point on daily periom .

i code it to Mql4 program ,but it is very memory_ cosumering and works very slow .

N:=3;                                                                                         //input parameter
Var1:=MA(HHV(HIGH,485),17);                                                ///HHV(HIGH,222)           the highest price in 222 period          
Var2:=MA(HHV(HIGH,222),17);                                                //MA   ma method  in 17 period
Var3:=MA(HHV(HIGH,96),17);
Var4:=MA(LLV(LOW,485),17);                                                 //LLV(LOW,485)                the lowest price in  485 period
Var5:=MA(LLV(LOW,222),17);
Var6:=MA(LLV(LOW,96),17);
Var7:=MA((Var4*0.96+Var5*0.96+Var6*0.96+Var1*0.558+Var2*0.558+Var3*0.558)/6,17);
Var8:=MA((Var4*1.25+Var5*1.23+Var6*1.2+Var1*0.55+Var2*0.55+Var3*0.65)/6,17);
Var9:=MA((Var4*1.3+Var5*1.3+Var6*1.3+Var1*0.68+Var2*0.68+Var3*0.68)/6,17);
VarA:=MA((Var7*3+Var8*2+Var9)/6*1.738,17);
VarB:=IF(DATE<=1050531,VarA,VarA*DAY);                //date is the time from when the market is opened ,i ignore it    day is the of the month
VarC:=LOW;                                                                  // low is the low price of this day 
VarD:=REF(LOW,1);                                                      //ref (low,1)   is last day's   price
VarE:=SMA(ABS(VarC-VarD),3,1)/SMA(MAX(VarC-VarD,0),N,1)*100;               //sma is ma method
VarF:=MA(IF(CLOSE*1.35<=VarB,VarE*10,VarE/10),N);
Var10:=LLV(LOW,30);
Var11:=HHV(VarF,30);
Var12:=1990831;
buypoint:=IF(DATE<=Var12,MA(IF(LOW<=Var10,(VarF+Var11*2)/2,0),N),100000000);   //drawline
I:=IF(CLOSE<REF(CLOSE,1),VOL/CAPITAL*10053,0);
best_buy_point:= ABS(EMA(I,13.3)-18)/WINNER(CLOSE)*IF((CLOSE-MA(CLOSE,6))/MA(CLOSE,6)<-0.04,1,0);

#property copyright ""
#property link      ""
#property version   "1.00"
//#property strict
#include <MovingAverages.mqh>
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_width1 2
#property indicator_color2 Green
#property indicator_width2 2
#property indicator_color3 Blue
#property indicator_width3 2
#property indicator_color4 Yellow
#property indicator_width4 2

input int n1=500; 
input int n2=250;
input int n3=90;
input int p1=21; 
input int p2=3; 
input int p3=30; 

double var1[];double var2[];double var3[];double var4[];double var5[];
double var6[];double var7[];double var8[];double var9[];double var10[];
double varA[];double varB[];double varC[];double varD[];double varE[];double varF[];
double var11[];double var12[];double var13[];double var21[];double var22[];double var23[];
double var31[];double var32[];double var33[];double var41[];
double var51[];double var52[];double var53[];double var54[];
double var61[];
double firemountin[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(31);
   SetIndexStyle(0,DRAW_LINE,2);SetIndexStyle(1,DRAW_LINE,2);
   SetIndexStyle(2,DRAW_LINE,2);SetIndexStyle(3,DRAW_LINE,2);
   SetIndexBuffer(0,var61);
   SetIndexBuffer(1,varD);
   SetIndexBuffer(2,varE);
   SetIndexBuffer(3,firemountin);
   SetIndexBuffer(4,var4);
   SetIndexBuffer(5,var5);
   SetIndexBuffer(6,var6);
   SetIndexBuffer(7,var7);
   SetIndexBuffer(8,var8);
   SetIndexBuffer(9,var9);
   SetIndexBuffer(10,var10);
   SetIndexBuffer(11,varA);
   SetIndexBuffer(12,varB);
   SetIndexBuffer(13,varC);
   SetIndexBuffer(14,var1);
   SetIndexBuffer(15,var2);
   SetIndexBuffer(16,varF);
   SetIndexBuffer(17,var11);
   SetIndexBuffer(18,var12);
   SetIndexBuffer(19,var13);
   SetIndexBuffer(20,var21);
   SetIndexBuffer(21,var22);
   SetIndexBuffer(22,var23);
   SetIndexBuffer(23,var31);
   SetIndexBuffer(24,var32);
   SetIndexBuffer(25,var33);
   SetIndexBuffer(26,var41);
   SetIndexBuffer(27,var51);
   SetIndexBuffer(28,var52);
   SetIndexBuffer(29,var53);
   SetIndexBuffer(30,var54);
   SetIndexBuffer(31,var3);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
   int    i,limit;
   //Print(0);
//---
   if(rates_total<=1000)      return(0);
   //limit=prev_calculated-1;
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)          limit++;
   for(i=0; i<limit; i++)
     {
      var11[i]=High[iHighest(NULL,0,MODE_HIGH,n1,i)];//Print(var11[0]);
      var12[i]=High[iHighest(NULL,0,MODE_HIGH,n2,i)];
      var13[i]=High[iHighest(NULL,0,MODE_HIGH,n3,i)];
      var21[i]=Low[iLowest(NULL,0,MODE_LOW,n1,i)];
      var22[i]=Low[iLowest(NULL,0,MODE_LOW,n2,i)];
      var23[i]=Low[iLowest(NULL,0,MODE_LOW,n3,i)];
     }
   
   for(i=0; i<limit; i++)  var1[i]= iMAOnArray(var11,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var2[i]= iMAOnArray(var12,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var3[i]= iMAOnArray(var13,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var4[i]= iMAOnArray(var21,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var5[i]= iMAOnArray(var22,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var6[i]= iMAOnArray(var23,Bars,p1,0,MODE_EMA,i);
   
   for(i=0; i<limit; i++)
   {
    var31[i]=(var1[i]*0.96+var2[i]*0.96+var3[i]*0.96+var4[i]*0.558+var5[i]*0.558+var6[i]*0.558)/6;
    var32[i]=(var1[i]*1.25+var2[i]*1.23+var3[i]*1.2 +var4[i]*0.55 +var5[i]*0.65 +var6[i]*0.55)/6;
    var33[i]=(var1[i]*1.3 +var2[i]*1.3 +var3[i]*1.3 +var4[i]*0.68 +var5[i]*0.68 +var6[i]*0.68)/6;
   }
   for(i=0; i<limit; i++)  var7[i]= iMAOnArray(var31,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var8[i]= iMAOnArray(var32,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var9[i]= iMAOnArray(var33,Bars,p1,0,MODE_EMA,i);
   
   for(i=0; i<limit; i++)
   {
    var41[i]=(var9[i]*3+var8[i]*2+var7[i])/6*1.738;
   }
   for(i=0; i<limit; i++)  varA[i] = iMAOnArray(var41,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  varB[i] =High[i+1];//Low[i+1];
   for(i=0; i<limit; i++)  var51[i]=MathAbs(High[i]-varB[i]);//MathAbs(Low[i]-varB[i]);
   for(i=0; i<limit; i++)  var52[i]=MathMax((High[i]-varB[i]),0);//MathMax((Low[i]-varB[i]),0);//
   for(i=0; i<limit; i++)  var53[i]= iMAOnArray(var51,Bars,p1,0,MODE_EMA,i);
   for(i=0; i<limit; i++)  var54[i]= iMAOnArray(var52,Bars,p1,0,MODE_EMA,i);
   
   for(i=0; i<limit; i++)  
   {
    if(var54[i]!=0) varC[i]=var53[i]/var54[i];//*100; 
    else varC[i]=0;//if(var54[i+1]!=0)     varC[i]=var53[i]/var54[i+1]*100; 
   }
   for(i=0; i<limit; i++)  
   {
    if(High[i]*1.35>=varA[i]) var61[i]=varC[i]*10;else var61[i]=varC[i]/10;
   }//Print("var61=",var61[20]);
   for(i=0; i<limit; i++)  varD[i]= iMAOnArray(var61,Bars,p1,0,MODE_EMA,i);
   //Print("varD=",varD[0]);
   for(i=0; i<limit; i++)  
   {
    double hm=-10000;
    for(int m=i;m<i+p3;m++)
    {
     if(varD[m]>hm) hm=varD[m];
    }
    varE[i]=hm;
   }//Print("varE=",varE[3000]);
   for(i=0; i<limit; i++) varF[i]=Low[iLowest(NULL,0,MODE_LOW,p3,i)]; //Print("varF=",varF[300]);
   for(i=0; i<limit; i++) var10[i]=1;
   for(i=0; i<limit; i++) 
   {
    if(High[i]<=varE[i])     firemountin[i]=(varD[i]+varF[i]*2)/2;
    else firemountin[i]=0;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+



To add comments, please log in or register