ChartIndicatorDelete

从指定图表窗口删除指定名称的指标。

bool  ChartIndicatorDelete(

   long           chart_id,              // 图表 id

   int            sub_window             // 子窗口数量

   const string   indicator_shortname    // 指标的缩略名

   );

参数

chart_id

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

sub_window

[in]  图表子窗口的数量。0代表主图表的子窗口。

const indicator_shortname

[in]  通过IndicatorSetString()函数在INDICATOR_SHORTNAME属性设置的指标的缩略名。若要获得指标的缩略名,请使用ChartIndicatorName()函数。

返回值

如果指标成功删除,返回true。否则返回false。若要获得错误信息,请使用GetLastError()函数。

注意

如果相同缩略名的两个指标都在图表子窗口,这行的第一个将被删除。

如果该图表上的另一个指标基于正被删除的指标值,这种指标也将被删除。

不要混淆指标缩略名和使用函数iCustom()IndicatorCreate()创建指标时指定的文件名。如果指标缩略名没有明确设置,那么包含指标源代码的文件名将在编译期被指明。

删除来自图表的指标不表示其计算部分将被从程序端内存删除。若要释放指标句柄,请使用IndicatorRelease()函数。

指标缩略名应该正确设置。它将使用IndicatorSetString()函数被写入INDICATOR_SHORTNAME属性。建议缩略名应该包含指标所有输入参数的值,因为通过ChartIndicatorDelete()函数从图表删除的指标通过缩略名识别。

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

//+------------------------------------------------------------------+

//|                                    Demo_ChartIndicatorDelete.mq5 |

//|                        Copyright 2011, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2011, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1

//--- 绘制柱状图

#property indicator_label1  "Histogram"

#property indicator_type1   DRAW_HISTOGRAM

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- 输入参数

input int      first_param=1;

input int      second_param=2;

input int      third_param=3;

input bool     wrong_init=true;

//--- 指标缓冲区

double         HistogramBuffer[];

string         shortname;

//+------------------------------------------------------------------+

//| 自定义指标初始化函数                                                |

//+------------------------------------------------------------------+

int OnInit()

  {

   int res=INIT_SUCCEEDED;

//--- 连接柱状图缓冲区数组到指标缓冲区

   SetIndexBuffer(0,HistogramBuffer,INDICATOR_DATA);

//--- 基于输入参数设置指标缩略名

   shortname=StringFormat("Demo_ChartIndicatorDelete(%d,%d,%d)",

                          first_param,second_param,third_param);

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//--- 如果设置指标强制完成,返回非零值

   if(wrong_init) res=INIT_FAILED;

   return(res);

  }

//+------------------------------------------------------------------+

//| 自定义指标迭代函数                                                 |

//+------------------------------------------------------------------+

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 start=prev_calculated-1;

   if(start<0) start=0;

//--- 填写指标缓冲区的值

   for(int i=start;i<rates_total;i++)

     {

      HistogramBuffer[i]=close[i];

     }

//--- 返回prev_calculated值为了下次调用

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Deinit 事件的处理程序                                              |

//+------------------------------------------------------------------+

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);

      //--- 分析调用ChartIndicatorDelete()的结果

      if(!res)

        {

         PrintFormat("Failed to delete indicator %s from window #%d. Error code %d",

                     shortname,window,GetLastError());

        }

     }

  }

另见

ChartIndicatorAdd()ChartIndicatorName()ChartIndicatorsTotal()iCustom()IndicatorCreate(), IndicatorSetString()