求助,写了个指标,有4条线,为什么只显示2条?

 

求助,写了个指标,有4条线,为什么只显示2条?

代码如下:

//+------------------------------------------------------------------+
//|                                                     KDJ.mq5 |
//|                                   Copyright 2024,TradingVUE Ltd. |
//|                                       https://www.tradingvue.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024,TradingVUE Ltd."
#property link      "https://www.tradingvue.net"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   4

#property indicator_label1  "ph"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_DASHDOTDOT
#property indicator_width1  1

#property indicator_label2  "pl"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrLime
#property indicator_style2  STYLE_DASHDOTDOT
#property indicator_width2  1

#property indicator_label3  "sl Buy"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrWhite
#property indicator_style3  STYLE_DASH
#property indicator_width3  1

#property indicator_label4  "sl Sell"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrGray
#property indicator_style4  STYLE_DASH
#property indicator_width4  1

int periodK = 13;
int periodD = 3;
int smoothK = 4; 

// Buffer
double dBuffer[],kBuffer[];
double ph[],pl[];
double stoploss_long[],stoploss_short[];

double piv_high;
double piv_low;

int stoch_handle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   SetIndexBuffer(0,dBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(1,kBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,ph,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,pl,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,stoploss_long,INDICATOR_DATA);
   SetIndexBuffer(5,stoploss_short,INDICATOR_DATA);

//--- set buffers as series, most recent entry at index [0]

   ArraySetAsSeries(dBuffer,true);
   ArraySetAsSeries(kBuffer,true);
   ArraySetAsSeries(ph,true);
   ArraySetAsSeries(pl,true);
   ArraySetAsSeries(stoploss_long,true);
   ArraySetAsSeries(stoploss_short,true);

//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,periodK+periodD+10);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,periodK+periodD+10);
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,periodK+periodD+10);
   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,periodK+periodD+10);

   IndicatorSetString(INDICATOR_SHORTNAME,"KDJ");

   //--- get  handles
   stoch_handle=iStochastic(NULL,0,periodK,periodD,smoothK,MODE_SMA,STO_LOWHIGH);
   
   ChartSetInteger(ChartID(),CHART_SHOW_VOLUMES,false);
   ChartSetInteger(ChartID(),CHART_SHOW_GRID,false);
   
//---
   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[])
  {
//---
   if(rates_total<periodK)
      return(0);
      
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   
//--- not all data may be calculated
   int calculated;

   calculated=BarsCalculated(stoch_handle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtFastMaHandle has been calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }

//--- calculate how many bars need to be recalculated
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }

//--- get buffer values
   if(CopyBuffer(stoch_handle,SIGNAL_LINE,0,to_copy,kBuffer)<=0)
     {
      Print("缓冲区0复制失败! Error",GetLastError());
      return(0);
     }
   if(CopyBuffer(stoch_handle,MAIN_LINE,0,to_copy,dBuffer)<=0)
     {
      Print("缓冲区1复制失败! Error",GetLastError());
      return(0);
     }
      
//--- set limit for which bars need to be (re)calculated
   int limit;
   if(prev_calculated==0 || prev_calculated<0 || prev_calculated>rates_total)
      limit=rates_total-(periodK+periodD+10);
   else
      limit=rates_total-prev_calculated;
//--- older bars ([1]) are needed to set the color of the current bar
   if(limit>rates_total-(periodK+periodD+10)) limit=rates_total-(periodK+periodD+10);

//--- calculate MACD buffer
   for(int i=limit;i>=0;i--)
   {
      if(high[i]<high[i+1]&&high[i+1]>high[i+2])
         piv_high = high[i+1];
      if(low[i]>low[i+1]&&low[i+1]<low[i+2])   
         piv_low = low[i+1] ;
      ph[i]= piv_high;
      pl[i]= piv_low; 
      
      if(kBuffer[i]<80&&kBuffer[i]>dBuffer[i]&&kBuffer[i+1]<dBuffer[i+1])
         stoploss_long[i] = low[i]<pl[i] ? low[i] : pl[i];
      else
         stoploss_long[i] = stoploss_long[i+1];
   
      if(kBuffer[i]>20&&kBuffer[i]<dBuffer[i]&&kBuffer[i+1]>dBuffer[i+1])
         stoploss_short[i] = high[i]>ph[i] ? high[i] : ph[i];
      else
         stoploss_short[i] = stoploss_short[i+1];  
   }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Jian Wen Sun:

求助,写了个指标,有4条线,为什么只显示2条?

代码如下:

从代码来看,问题可能出在指标缓冲区的设置和绘制方面。你已经声明了4个绘制(plot)对象,并且设置了6个缓冲区(buffers)

可能是
  • 缓冲区没有正确映射到绘制对象:虽然声明了 indicator_buffers 6 和 indicator_plots 4 ,但在 OnInit 函数中,你只映射了 stoploss_long 和 stoploss_short 到 INDICATOR_DATA ,而没有将其他的 ph 和 pl 缓冲区映射到任何一个绘制对象。

  • 绘图索引设置不正确:你在 PlotIndexSetInteger 中使用了绘图索引 1、2、3 和 4,但实际应该是 0 、 1 、 2 和 3 来对应代码中的 indicator_label1 到 indicator_label4 。

    建议你试试

  • 需要将所有的绘制缓冲区映射到绘图对象。
  • 确保 ph 和 pl 这些缓冲区被正确地映射到绘制对象,并正确设置 PlotIndexSetInteger 。
  •  

    修改一下代碼 四條線