Search code examples
mql4

How to use Indicator count and how to turn key event into code in MQL4


I'm just learning to code and have few questions. Can someone tell me please how to do this thing;

1- How to prevent my custom indicator being duplicated on the chart? If it's already on the chart and I will drop it on to the chart again, I want it to detect the first one and abort launching the second one.

something like this: but working :)

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 - How do I detect if there is more than one indicator with the same name on the chart?

3 - How to write an "if" statement to do something if indicator "x" is not present (=0) on the chart?

Something like this: but working :)

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

4 - And is there a way to put keyboard event (F11 - full screen) into code? To make my custom indicator be able to detect when full screen is on just like with the "chart scale" ChartGetInteger(0,CHART_SCALEFIX); ?


Solution

  • Welcome to SOF. Look at counter below. The idea is to count number of indicators on the chart. The one you are adding will be in this list either once (this copy) or more (you are trying to add a new copy to an existing one). This example is for main chart (subwindow = 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);
        }