请教老师如何在新的指标文件中调用两次不同KD指标文件中的值,附代码。

 

KD值代码如下:

//+------------------------------------------------------------------+
//|                                                        参数KD值.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   2
//--- plot K
#property indicator_label1  "K"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot D
#property indicator_label2  "D"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDarkTurquoise
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_level1  0
#property indicator_level2  100

//---- input parameters
input int N =9;
input int M1=3;
input int M2=3;

//--- indicator buffers
double         KBuffer[];
double         DBuffer[];
double llv[],hhv[],rsv[];
double p=0,p1=0;
double f=0,f1=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,KBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,DBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,llv,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,hhv,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,rsv,INDICATOR_CALCULATIONS);
   
   ArraySetAsSeries(KBuffer,true);  
   ArraySetAsSeries(DBuffer,true);
   ArraySetAsSeries(llv,true);
   ArraySetAsSeries(hhv,true);
   ArraySetAsSeries(rsv,true); 
   
   for(int i=0;i<5;i++)
     {      
      PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,N+M1+M2);
     }

  
   string name = "KD("+ (string)N+","+(string)M1+","+(string)M2+")";
   IndicatorSetString(INDICATOR_SHORTNAME,name);
   IndicatorSetInteger(INDICATOR_DIGITS,2);  

   if(N<=0||M1<=0||M2<=0) return(INIT_FAILED);
  
   p = 1.0/M1;   p1 = 1-p;
   f = 1.0/M2;   f1 = 1-f;
//---
   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=0;
   if(rates_total<=0)return(0);
   if(prev_calculated<=0)limit=rates_total-1;
   else limit = rates_total - prev_calculated +1;

   ArraySetAsSeries(low,true);  
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(close,true);

   for(i=limit; i>=0; i--)
     {
      llv[i]=0; hhv[i]=0;
      if(i>rates_total-N) continue;
      int shift = iLowest(NULL,0,MODE_LOW,N,i);
      llv[i] =  low[shift];
      shift = iHighest(NULL,0,MODE_HIGH,N,i);
      hhv[i] = high[shift];
     }
   for(i=limit; i>=0; i--)
     {
      rsv[i] = 0;
      if(hhv[i]>0 && llv[i]>0 && (hhv[i]-llv[i])!=0)
         rsv[i] = (close[i]-llv[i])/(hhv[i]-llv[i])*100;
     }

   for(i=limit; i>=0; i--)
     {
      if(i==rates_total-1) KBuffer[i]=0;
      else
        {
         KBuffer[i] = rsv[i]*p + KBuffer[i+1]*p1;
        }
     }

   for(i=limit; i>=0; i--)
     {
      if(i==rates_total-1) DBuffer[i]=0;
      else
        {
         DBuffer[i] = KBuffer[i]*f + DBuffer[i+1]*f1;
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

调用指标代码:

//+------------------------------------------------------------------+
//|                                                           预警.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot maijin
#property indicator_label1  "maijin"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot maichu
#property indicator_label2  "maichu"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrDarkTurquoise
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
double         maijinBuffer[];
double         maichuBuffer[];
int 参考KD值_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,maijinBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,maichuBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   
   参考KD值_handle=iCustom(NULL,0,"参考KD值",9,3,3);
   参考KD值_handle=iCustom(NULL,0,"参考KD值",14,1,3);
//---
   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[])
  {
//---
   double KBuffer[];
   double DBuffer[];
   CopyBuffer(参考KD值_handle,0,0,rates_total,KBuffer);
   CopyBuffer(参考KD值_handle,1,0,rates_total,DBuffer);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+