adding and deleting indicator

 

lets say i want to add the BB indicator to my chart using my indicator code

everytime i change timeframe , another instance of the indicator BB is added again and again

what am i doing wrong ?


here is part of the code

int bbhandle ;

int OnInit()
  { 
  bbhandle = iCustom(_Symbol,_Period,"BB");
  ChartIndicatorAdd(0,0,bbhandle);
  return(INIT_SUCCEEDED);
  }


void OnDeinit( const int Reason ) {
      ChartIndicatorDelete(0,0,"BB");
      IndicatorRelease(bbhandle);

  }
 

Every time you switch to another timeframe the system executes OnInit again. To avoid reattaching the indicator simply check if bbhandle is holding a valid number.

Another solution is removing the attached indicator before attaching another.

 
Ahmed_Fouda:

lets say i want to add the BB indicator to my chart using my indicator code

everytime i change timeframe , another instance of the indicator BB is added again and again

what am i doing wrong ?


here is part of the code

I had same issue but simply adding a condition to check the number of indicators on the chart and limiting the indicator to only add a new indicator OnInit if there is less than one indicator on the chart.

This way, you don’t have duplicate indicators added and also... If you adjust indicator settings, it will remain so unlike when you use indicatordelete  on DeInit 

 
Yashar Seyyedin #:

Every time you switch to another timeframe the system executes OnInit again. To avoid reattaching the indicator simply check if bbhandle is holding a valid number.

Another solution is removing the attached indicator before attaching another.

i am already removing it as u see in Deinit but it seems its not deleting it

 
Ahmed_Fouda #:

i am already removing it as u see in Deinit but it seems its not deleting it

Remove it in OnInit I mean.
 
seems like the  ChartIndicatorDelete  is not working are you sure the name of the indicator is "BB" ?
Reason: