how to use ChartIndicatorDelete when changing parameters of EA

 

I have an EA that uses an indicator. I would like to see the indicator when running the EA. However, each time I change a parameter in the EA, the indicator is doubled in the chart.


In the example below the Ibands has 3 lines, when I load the EA for the first time...but if I change any parameter in the user interface of the EA,  a new set of lines a created for the indicator. Is there a way to delete the prior handle of an indicator?

   input group             "-----BB------";
   input int               Entry_BB_Len  = 20;          // Length Bollinger             
   input double            Entry_BB_SDs  = 2.0;         // Number of standard deviations
   
 
   int              Entry_BB_Handle;
   double           Entry_BB_Buffer[]; 
   
  
 
int OnInit()
{  
Entry_BB_Handle = iBands(Symbol(),PERIOD_CURRENT, Entry_BB_Len,0,Entry_BB_SDs,PRICE_CLOSE);
ChartIndicatorAdd(0,0,Entry_BB_Handle);
   
   return(INIT_SUCCEEDED);
}


void OnDeinit(const int reason)
{
ChartIndicatorDelete(0,0,"Bollinger Bands");
IndicatorRelease(Entry_BB_Handle);
}

void OnTick()
{

}

 
   input group             "-----BB------";
   input int               Entry_BB_Len  = 20;          // Length Bollinger             
   input double            Entry_BB_SDs  = 2.0;         // Number of standard deviations
   
 
   int              Entry_BB_Handle;
   
  
 
int OnInit()
{  
Entry_BB_Handle = iBands(Symbol(),PERIOD_CURRENT, Entry_BB_Len,0,Entry_BB_SDs,PRICE_CLOSE);
ShortName = ChartIndicatorAdd2(0,0,Entry_BB_Handle);
   
   return(ShortName == NULL);
}


void OnDeinit(const int reason)
{
ChartIndicatorDelete(0,0,ShortName);
IndicatorRelease(Entry_BB_Handle);
}

string ChartIndicatorAdd2( const long chart_id, const int sub_window, const int indicator_handle )
{
  return(ChartIndicatorAdd(chart_id, sub_window, indicator_handle)
           ? ChartIndicatorName(chart_id, sub_window, ChartIndicatorsTotal(chart_id, sub_window) - 1)
           : NULL);
}

string ShortName;
 
   input group             "-----BB------";
   input int               Entry_BB_Len  = 20;          // Length Bollinger             
   input double            Entry_BB_SDs  = 2.0;         // Number of standard deviations
   
 
   int              Entry_BB_Handle;
   
  
 
int OnInit()
{  
Entry_BB_Handle = iBands(Symbol(),PERIOD_CURRENT, Entry_BB_Len,0,Entry_BB_SDs,PRICE_CLOSE);
ChartIndicatorAdd(0,0,Entry_BB_Handle);
// ChartIndicatorAdd(0,1,Entry_BB_Handle);
// ChartIndicatorAdd(0,2,Entry_BB_Handle);
   
   return(INIT_SUCCEEDED);
}


void OnDeinit(const int reason)
{
ChartIndicatorDelete(0,Entry_BB_Handle);
IndicatorRelease(Entry_BB_Handle);
}

bool ChartIndicatorDelete( const long chart_id, const int sub_window, const int indicator_handle )
{
  bool Res = false;
  
  for (uint i = ChartIndicatorsTotal(chart_id, sub_window); !Res && (bool)i--;)
  {
    const string ShortName = ChartIndicatorName(chart_id, sub_window, i);
    
    Res = (ChartIndicatorGet(chart_id, sub_window, ShortName) == indicator_handle) &&
           ChartIndicatorDelete(chart_id, sub_window, ShortName);
  }
  
  return(Res);
}

bool ChartIndicatorDelete( const long chart_id, const int indicator_handle )
{
  bool Res = false;
  
  for (uint i = (uint)ChartGetInteger(chart_id, CHART_WINDOWS_TOTAL); (bool)i--;)
    Res |= ChartIndicatorDelete(chart_id, i, indicator_handle);
    
  return(Res);
}
 
fxsaber #:

Thank you...

This function works... are the other options you provided better?


bool ChartIndicatorDelete( const long chart_id, const int indicator_handle )

{
  bool Res = false;
  
  for (uint i = (uint)ChartGetInteger(chart_id, CHART_WINDOWS_TOTAL); (bool)i--;)
    ChartIndicatorDelete(chart_id, i, IntegerToString(indicator_handle));
    
}


 
Camilo Mora #:

This function works... are the other options you provided better?

The second option is more reliable - by handle.

 
Sorry, it actually does not work...your code dobles the indicaotr plots each time ones changes the parameters in the EA...
 
Camilo Mora #:
Sorry, it actually does not work...your code dobles the indicaotr plots each time ones changes the parameters in the EA...

There are two options: first and second. Copy the entire source code of one of the given options and compile. There should be no compiler errors or warnings.


 
That is very nice of you, thank you...indeed second options works...
Reason: