跳至内容

ChartIndicatorDelete

ChartIndicatorDelete

从指定的图表窗口中移除具有指定名称的指示器。该命令将被添加到图表消息队列中,并且只有在所有之前的命令被处理之后才会执行。

bool  ChartIndicatorDelete(
   long           chart_id,              // chart id
   int            sub_window,            // number of the subwindow
   const string   indicator_shortname    // short name of the indicator
   );

参数

chart_id

[in] 图表ID。0表示当前图表。

sub_window

[in] 图表子窗口的编号。0表示主图表子窗口。

const indicator_shortname

[in] 指示器的简称,通过IndicatorSetString()函数将名称设置在INDICATOR_SHORTNAME属性中。要获取指示器的简称,请使用ChartIndicatorName()函数。

返回值

如果命令已添加到图表队列,则返回true;否则返回false。要获取错误详情,请使用GetLastError()函数。

注意

如果图表子窗口中存在两个具有相同简称的指示器,则首先删除的行将被删除。

如果此图表上的其他指示器基于正在被删除的指示器的数值,这些指示器也将被删除。

如果指示器的简称未明确设置,则在编译期间需要指定包含指示器源代码文件的名称。

指示器的简称应正确形成。它将通过IndicatorSetString()函数写入INDICATOR_SHORTNAME属性中。建议简称应包含指示器的所有输入参数值,因为通过ChartIndicatorDelete()函数从图表中删除的指示器是通过简称来识别的。

初始化失败后删除指示器的示例:

//+------------------------------------------------------------------+
//|                                    Demo_ChartIndicatorDelete.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
//--- plot Histogram
#property indicator_label1  "Histogram"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      first_param=1;
input int      second_param=2;
input int      third_param=3;
input bool     wrong_init=true;
//--- indicator buffers
double         HistogramBuffer[];
string         shortname;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   int res=INIT_SUCCEEDED;
//--- Link the HistogramBuffer array to the indicator buffer
   SetIndexBuffer(0,HistogramBuffer,INDICATOR_DATA);
//--- Construct a short indicator name based on input parameters
   shortname=StringFormat("Demo_ChartIndicatorDelete(%d,%d,%d)",
                          first_param,second_param,third_param);
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- If forced completion of an indicator is set, return a non-zero value
   if(wrong_init) res=INIT_FAILED;
   return(res);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- Starting position for working in a loop
   int start=prev_calculated-1;
   if(start<0) start=0;
//--- Fill in the indicator buffer with values
   for(int i=start;i<rates_total;i++)
     {
      HistogramBuffer[i]=close[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Handler of the Deinit event                                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   PrintFormat("%s: Deinitialization reason code=%d",__FUNCTION__,reason);
   if(reason==REASON_INITFAILED)
     {
      PrintFormat("An indicator with a short name %s (file %s) deletes itself from the chart",shortname,__FILE__);
      int window=ChartWindowFind();
      bool res=ChartIndicatorDelete(0,window,shortname);
      //--- Analyse the result of call of ChartIndicatorDelete()
      if(!res)
        {
         PrintFormat("Failed to delete indicator %s from window #%d. Error code %d",
                     shortname,window,GetLastError());
        }
     }
  }

另请参阅

ChartIndicatorName(), ChartIndicatorsTotal(), IndicatorSetString()

最后更新于