MQL5 - Is there a way to remove an indicator from the chart using it's handle? - page 2

 

Thanks man, but I add the indicator to the chart. 

ChartIndicatorAdd(0, 0, handle);
 

I want to make the problem a little clearer.

Change from H1 timeframe to M30.

int handle;                           

int OnInit()
{
   Print("\nOnInit of ",EnumToString(Period()));
   
   // draw indicator
   if(Period()==PERIOD_H1){
      handle=iMA(Symbol(),PERIOD_H1,20,1,MODE_SMA,PRICE_CLOSE);
      ChartIndicatorAdd(0, 0, handle);
      Print("Indicator added with handle ",handle);
   }
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   Print("OnDeinit of ",EnumToString(Period()));
   
   // delete indicator using the indicator handle
   int numIndicators = ChartIndicatorsTotal(0,0);
   
   for(int index=0; index<numIndicators; index++){
      string name = ChartIndicatorName(0,0,index);
 
      int handleOnChart=ChartIndicatorGet(0,0,name);
      Print("Handle found on chart:",handleOnChart);
      
      if(handleOnChart == handle){
         if(!ChartIndicatorDelete(0,0,name)){
            PrintFormat("Error indicator delete. Error: %d", GetLastError());
         }
         else{
            Print("Delete indicator with handle: ",handle);
            IndicatorRelease(handle);
         }
      }
   }
}


Result:


 
trustfultrading #:

I want to make the problem a little clearer.

Change from H1 timeframe to M30.


It is as expected, a handle to the indicator in memory to be used in code when creating, and when you add it to the chart it takes another handle for terminal use.
 

Thanks for your comment, but something is wrong here.

ChartIndicatorGet behaves differently in OnDeinit when changing timeframe or removing the EA.

int handle = INVALID_HANDLE;                           

int OnInit()
{
   Print("\nOnInit");
   
   handle=iMA(Symbol(),Period(),20,0,MODE_SMA,PRICE_CLOSE);
   Print("Indicator handle is ",handle);
   
   ChartIndicatorAdd(0, 0, handle);
   Print("Indicator added with handle ",handle);
   
   int handleOnChart=ChartIndicatorGet(0,0,ChartIndicatorName(0,0,0));
   Print("Handle found on chart is ",handleOnChart);
   
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   Print("\nOnDeinit");
   
   int handleOnChart=ChartIndicatorGet(0,0,ChartIndicatorName(0,0,0));
   Print("Handle found on chart is ",handleOnChart);
   
   IndicatorRelease(handle);
}


Nr 1:


Nr 2:


In case I missed something, I'd like to apologize in advance ;D

 

I twicked a bit your code and now it works. The problem was that the number of indicators changes as you start removing them, so you need to start removing from the last one to the first one, in reverse order:

  int num_windows = (int)ChartGetInteger(0, CHART_WINDOWS_TOTAL);

  for (int window = num_windows - 1; window > -1; window--)
  {
    int numIndicators = ChartIndicatorsTotal(0, window);

    for (int index = numIndicators; index >= 0; index--)
    {
      ResetLastError();

      string name = ChartIndicatorName(0, window, index);

      if (GetLastError() != 0)
      {
        PrintFormat("ChartIndicatorName error: %d", GetLastError());
        ResetLastError();
      }

      if (!ChartIndicatorDelete(0, window, name))
      {
        if (GetLastError() != 0)
        {
          PrintFormat("Delete indicator error: %d", GetLastError());
          ResetLastError();
        }
      }
      else
      {
        Print("Delete indicator with handle:", name);
      }
    }
  }
 
    int total = (int)ChartGetInteger(0, CHART_WINDOWS_TOTAL);
    for(int i = total - 1; i >= 0; i--)
       {
        for(int k = ChartIndicatorsTotal(0, i) - 1; k >= 0; k--)
           {
            string name = ChartIndicatorName(0, i, k);
            ChartIndicatorDelete(0, i, name);
           }
       }

Just a little clean way of doing it.
Similar to what Juan Calvo did.