首页 > 解决方案 > 如何使用指标计数以及如何将关键事件转换为 MQL4 中的代码

问题描述

我只是在学习编码并且有几个问题。有人可以告诉我如何做这件事吗?

1- 如何防止我的自定义指标在图表上重复?如果它已经在图表上并且我会将它再次放到图表上,我希望它检测到第一个并中止启动第二个。

像这样的东西:但工作:)

int OnInit()  
{   
    int indicators_total = ChartIndicatorsTotal(0,0);
    for(int i = 0; ndicators_total > i; i++)
      {
        if(ChartIndicatorName(0,0,i)==IndicatorName)
        return(INIT_FAILED); (AND THEN EXIT)
      }
}

2 - 我如何检测图表上是否有多个同名指标?

3 - 如果图表上不存在指标“x”(=0),如何编写“if”语句来做某事?

像这样的东西:但工作:)

 if(IndicatorName==0)
    {
     Print("INDI ",IndicatorName, " NOT DETECTED");
    }

4 - 有没有办法将键盘事件(F11 - 全屏)放入代码中?为了让我的自定义指标能够像“图表比例”一样检测全屏显示ChartGetInteger(0,CHART_SCALEFIX);

标签: mql4

解决方案


欢迎来到 SOF。看counter下面。这个想法是计算图表上的指标数量。您要添加的副本将在此列表中出现一次(此副本)或更多(您正在尝试将新副本添加到现有副本)。此示例适用于主图表(子窗口 = 0)。

  int OnInit()
    {
     const string indName = WindowExpertName(); // name of your indicator
     int counter=0; 
     for(int i=ChartIndicatorsTotal(0,0)-1;i>=0;i--)
       {
        //printf("%i %s: i=%d/%d, %s",__LINE__,__FILE__,i,ChartIndicatorsTotal(0,0),ChartIndicatorName(0,0,i));
        if(ChartIndicatorName(0,0,i)==indName)
           counter++;
       }
     if(counter>1)
       {
        Print("already exist!");
        return INIT_FAILED;
       }
  //--- indicator buffers mapping
     // do all other indicator preparation work here
  //---
     return(INIT_SUCCEEDED);
    }

推荐阅读