Download MetaTrader 5

ChartIndicatorDelete does not always work as expected.

To add comments, please log in or register
goocreations
331
goocreations 2015.04.01 11:52 

I manually add two indicators from my expert advisor as given below. The idea is to add separate sub windows to the chart and if the expert advisore is removed from the chart, so should all the indicaotrs it created.

int handle1;
int handle2;

int OnInit()
{
        handle1 = iCustom(Symbol(), Period(), "MyIndicator");
        handle2 = iCustom(Symbol(), Period(), "MyIndicator");
  
        ChartIndicatorAdd(0, 1, handle1);
        ChartIndicatorAdd(0, 2, handle2);

        return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
        if(!ChartIndicatorDelete(0, 1, "MyIndicator")) Print("Error 1: ", GetLastError());
        if(!ChartIndicatorDelete(0, 2, "MyIndicator")) Print("Error 2: ", GetLastError());
        
        IndicatorRelease(handle1);
        IndicatorRelease(handle2);
}

If I use the code above, the first indicator is removed, but the seconds isn't. The error code is - Error 2: 4115. Now, if I change the onDeinit function as follows:

void OnDeinit(const int reason)
{
        if(!ChartIndicatorDelete(0, 2, "MyIndicator")) Print("Error 2: ", GetLastError());
        if(!ChartIndicatorDelete(0, 1, "MyIndicator")) Print("Error 1: ", GetLastError());
        
        IndicatorRelease(handle1);
        IndicatorRelease(handle2);
}

So I delete the indicator that was added LAST first and the one that was added FIRST is deleted last. Now everything works fine.


I believe there might be a bug in ChartIndicatorDelete, because the order of deletion should not matter, since the parameters for the function are unique (two different subwindows). Can a admin maybe confirm this, or am I understanding something wrong?

To add comments, please log in or register