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

 

I want to add/remove (toggle) an indicator using a CButton button from a CAppDialog.

Let's say the indicator is Donchian Channels, which I use iCustom() to load.

Adding the indicator is trivial. I simply use iCustom() to get the handle then use ChartIndicatorAdd().

Removing the indicator (as of right now) requires multiple steps. It works, but it's clunky.

1. Find the indicator short name. Generally I know the left side of this only. For example, the Donchian Channels indicator I use has this for the short name:

Donchian( DonchianPeriod = 20)

As you can see, this can change if I change the period. So I loop through all windows on the chart, then loop through all indicators on those windows. If I search for "Donchian" on the left side, I get a match. Then I return the full short name.

2. Remove the indicator using the full short name: ChartIndicatorDelete()

3. Release the handle using IndicatorRelease()

4. Set the handle = INVALID_HANDLE so the button will act as a toggle.

This all works.

So my question is: is there a way I could simply remove the indicator from the chart using only handle?

 
Not to my knowledge.
 
Alain Verleyen:
Not to my knowledge.
Thanks for your response. I figured this was the case, but I wanted to be sure.
 
Anthony Garot:

I want to add/remove (toggle) an indicator using a CButton button from a CAppDialog.

Let's say the indicator is Donchian Channels, which I use iCustom() to load.

Adding the indicator is trivial. I simply use iCustom() to get the handle then use ChartIndicatorAdd().

Removing the indicator (as of right now) requires multiple steps. It works, but it's clunky.

1. Find the indicator short name. Generally I know the left side of this only. For example, the Donchian Channels indicator I use has this for the short name:

Donchian( DonchianPeriod = 20)

As you can see, this can change if I change the period. So I loop through all windows on the chart, then loop through all indicators on those windows. If I search for "Donchian" on the left side, I get a match. Then I return the full short name.

2. Remove the indicator using the full short name: ChartIndicatorDelete()

3. Release the handle using IndicatorRelease()

4. Set the handle = INVALID_HANDLE so the button will act as a toggle.

This all works.

So my question is: is there a way I could simply remove the indicator from the chart using only handle?

You don't have to care the short name or the left part of the short name at all if you look for the handle that you have on the chart. You can do it the following:

1. Loop through all windows on the chart and then all indicators on this window.

2. for each indicator, get it's handle by it's name 

3. check if the handle you got equals the handle you have - and remove it by handle. then perform the steps 3-4 in your post.



   long total=ChartGetInteger(ChartID(),CHART_WINDOWS_TOTAL);
   for(int h=0;h<total;h++)
      for(int i=0;i<ChartIndicatorsTotal(0,h);i++)
        {
         if(ChartIndicatorGet(0,h,ChartIndicatorName(0,h,i))==MA_handle)
           {
            if(!ChartIndicatorDelete(0,h,ChartIndicatorName(0,h,i)))
              {
               Print("error indicator delete, rc=",GetLastError());
              }
            else
              {
               IndicatorRelease(MA_handle);
               MA_handle=INVALID_HANDLE;
              }
           }
        }
*-updated the first line in code to the 2nd form of ChartGetInteger, thus remove the subwindow id parameter.
 
Amir Yacoby:

You don't have to care the short name or the left part of the short name at all if you look for the handle that you have on the chart. You can do it the following:

1. Loop through all windows on the chart and then all indicators on this window.

2. for each indicator, get it's handle by it's name 

3. check if the handle you got equals the handle you have - and remove it by handle. then perform the steps 3-4 in your post.

*-updated the first line in code to the 2nd form of ChartGetInteger, thus remove the subwindow id parameter.
Yes it can be considered as removed by handle. Nice.
 
Amir Yacoby:

This is a definite improvement. Thank you!

 

Hi,

I'm trying to use this method to delete a indicator from the chart. But its not working for me.
I just want to display the MA indicator on the H1 timeframe. Is there something I'm missing?
Somehow I cant get the indicator handle from the H1 timeframe in OnDeinit. Therefore I can't delete it and the indicator will be drawn on e.g. the M30 chart.

int handle;                           

int OnInit()
{
   handle=iMA(Symbol(),PERIOD_H1,20,1,MODE_SMA,PRICE_CLOSE);
   
   // draw indicator
   if(Period()==PERIOD_H1){
      ChartIndicatorAdd(0, 0, handle);
   }
   
   return(INIT_SUCCEEDED);
}


void OnDeinit(const int reason)
{
   // delete indicator using the indicator handle
   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=0; index<numIndicators; index++){
         string name = ChartIndicatorName(0,window,index);
    
         int handleOnChart=ChartIndicatorGet(0,window,name);
         Print("handle found on chart:",handleOnChart);
         
         if(handleOnChart == handle){
            if(!ChartIndicatorDelete(0,window,name)){
                   PrintFormat("Error indicator delete. Error: %d", GetLastError());
                   return;
            }
            else{
               Print("Delete indicator with handle:",handle);
               IndicatorRelease(handle);
               return;
            }
         }
      }
   }
}
 

I am not sure whether OnDeinit() is called if you change the time frame.

BTW In the ref. for ChartIndicatorDelete() (place your cursor in the editor on the function and press F1) is an example with it in DeInit() and the print of the message of the reason for deinit.

 

Thanks for your answer. The problem is that you don't get the correct handle in OnDeinit. When going from H1 timeframe to M30, ChartIndicatorGet returns the handle which is for M30.

Is this a bug or am I just stupid?
 
trustfultrading #: I'm trying to use this method to delete a indicator from the chart. But its not working for me.

You did not add the indicator to the chart with the handle. You can't remove it with the handle.

Code has no eyes; they don't need to see the indicator on the chart, just the values. If you want to see it, you add it to the appropriate chart.

There is no (and will not ever be) ChartIndicatorAdd in MT4.
          MQL4 Add indicator to chart from EA - MQL4 programming forum #18 (2016)

use ChartApplyTemplate() or go down the rabbit hole.
          MQL4 Add indicator to chart from EA - MQL4 programming forum #1 (2016) and #27 (2018) #55 (2022), and Issues with PostMessageW in MQL4 | Forex Factory #27 (2020)

 
Carl Schreiber #: I am not sure whether OnDeinit() is called if you change the time frame.

Of course, it is. reason for deinitialization

Reason: