Switching between time frames duplicates the indicator in an EA

 

I am using a custom indicator within an EA and the code of the EA is as follows:

#resource "ATB.ex5"

int _atb;

int OnInit() {
   _atb = iCustom(_Symbol, _Period, "::ATB.ex5");
   if(_atb == INVALID_HANDLE) {
      return(INIT_FAILED);
   }
   int subwindow= (int)ChartGetInteger(0, CHART_WINDOWS_TOTAL);
   ChartIndicatorAdd(0, subwindow, _atb);
   return(INIT_SUCCEEDED);
}

void Deinit(const int reason) {
   if(_atb != INVALID_HANDLE) {
      IndicatorRelease(_atb);
   }

}

When I run the EA (F5) , the indicator displays on the chart as expected, however, when I switch timeframes, say from H1 to M1 then the indicator duplicates, with both indicators displaying correctly, then when I switch again then another indicator is added (3 indicators).

How can I prevent adding these duplicate indicators when I switch time frames on a running EA?

N.B. Adding the indicator on its own and changing the time frame is working as expected.

 
void OnDeinit(const int reason)
 
Ernst Van Der Merwe:
Silly me, thank you.
 
This does not help, it still creates duplicates.
 

Sergei Tsoganov #:
This does not help, it still creates duplicates.

check if the idicator is already on Chart by its short_name

#resource "ATB.ex5"
#define INDICATOR_SHORT_NAME "ATB"

int _atb;

int OnInit() {
   //check if the idicator is already on Chart by its short_name
   _atb = ChartIndicatorGet(ChartID(),0,INDICATOR_SHORT_NAME);

   if(_atb == INVALID_HANDLE)
	_atb = iCustom(_Symbol, _Period, "::ATB.ex5");

   if(_atb == INVALID_HANDLE) {
      return(INIT_FAILED);
   }
   int subwindow= (int)ChartGetInteger(0, CHART_WINDOWS_TOTAL);
   ChartIndicatorAdd(0, subwindow, _atb);
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
   if(_atb != INVALID_HANDLE) {
      IndicatorRelease(_atb);
   }

}


always remember to use IndicatorRelease in OnDeinit to remove your created  indicator handle(s) inside any Expert Advisor

void OnDeinit(const int reason) {
   if(_atb != INVALID_HANDLE) {
      IndicatorRelease(_atb);
   }


if you do not remove them, they will continue working in secret on your chart... until you remove your Expert


the Expert Advisors Create a new thread to runs the indicator, and they will be removed only with IndicatorRelease() 


However Indicator programs do not create a new thread for that, so the IndicatorRelease in Indicator Programs will work like ChartIndicatorDelete

 
Your code
#resource "ATB.ex5"
#define INDICATOR_SHORT_NAME "ATB"
        _atb = iCustom(_Symbol, _Period, "::ATB.ex5");
Try:
#define INDICATOR_SHORT_NAME "ATB.ex5"
#resource "\\"+INDICATOR_SHORT_NAME

        _atb = iCustom(_Symbol, _Period, "::"+INDICATOR_SHORT_NAME);