Simple Indicator, Simple BUG

 

Hello, I coded what was supposed to be a simple indicator to add specific indicators to the chart, unfortunately when I change the period or timeframe of any of the indicators, it duplicates,

I checked on the forum for a solution and was told to add the indicators in the OnInit, I have done so but issue persists.

Please this is the complete indicator, can you let me know what is wrong?

Thanks.

//+------------------------------------------------------------------+
//|                                               Codedindicator.mq5 |
//|                                  Copyright 2023,Obunadike Chioma |
//|                                           https://t.me/devbidden |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023,Obunadike Chioma"
#property link      "https://t.me/devbidden"
#property version   "1.00"
#property indicator_chart_window
#property  indicator_plots 0
#resource "\\Indicators\\bearded_fx_ma_1.ex5"
#resource "\\Indicators\\bearded_fx_ma_2.ex5"
#resource "\\Indicators\\bearded_fx_rsi.ex5"
#resource "\\Indicators\\bearded_fx_bb.ex5"
#resource "\\Indicators\\bearded_fx_trix.ex5"

int  ma1,ma2,rsi,bb,trix;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
int ma_exists =  ChartIndicatorGet(0,0,"bearded_fx_ma");
   int ma_2_exists = ChartIndicatorGet(0,0,"bearded_fx_ma2");
   int bb_exists = ChartIndicatorGet(0,0,"bearded_fx_bb");
   int rsi_exists = ChartIndicatorGet(0,1,"bearded_fx_rsi");
   int trix_exists =  ChartIndicatorGet(0,1,"bearded_fx_trix");
   
   ma1 = iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\bearded_fx_ma_1");
   ma2 = iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\bearded_fx_ma_2");
   rsi = iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\bearded_fx_rsi");
   bb = iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\bearded_fx_bb");
   trix = iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\\bearded_fx_trix");

   if(ma_exists<0)
     {
      ChartIndicatorAdd(0,0,ma1);
      ChartRedraw();
     }
   if(ma_2_exists<0)
     {
      ChartIndicatorAdd(0,0,ma2);
      ChartRedraw();
     }
   if(rsi_exists<0)
     {
      ChartIndicatorAdd(0,1,rsi);
      ChartRedraw();
     }
   if(bb_exists<0)
     {
      ChartIndicatorAdd(0,0,bb);
      ChartRedraw();
     }
   if(trix_exists<0)
     {
      ChartIndicatorAdd(0,2,trix);
      ChartRedraw();
     }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Chioma Obunadike:

Hello, I coded what was supposed to be a simple indicator to add specific indicators to the chart, unfortunately when I change the period or timeframe of any of the indicators, it duplicates,

I checked on the forum for a solution and was told to add the indicators in the OnInit, I have done so but issue persists.

Please this is the complete indicator, can you let me know what is wrong?

Thanks.

This "bug" is not as simple as it seems.

You need to close the indicators in OnDeinit.

For that you need to have the handle to the indicators.

If you do that, then your duplicates will disappear.


 

You just can't suppose you will get the handle of an indicator remaining on the chart this way. 

Already discussed on the forum. You need to search if you want more details.

 
Dominik Egert #:
This "bug" is not as simple as it seems.

You need to close the indicators in OnDeinit.

For that you need to have the handle to the indicators.

If you do that, then your duplicates will disappear.


If I remove indicators on deinit, when I change indicator to different timeframe, the period will change to default 
 
Alain Verleyen #:

You just can't suppose you will get the handle of an indicator remaining on the chart this way. 

Already discussed on the forum. You need to search if you want more details.

Thanks Alain. But please do you think the solution is centered on checking the current indicators on the chart ?
 
Chioma Obunadike #:
Thanks Alain. But please do you think the solution is centered on checking the current indicators on the chart ?

Dominik said how to proceed in general.

I don't know what you are expecting to do, so I can't say anything about a solution.

 
Alain Verleyen #:

Dominik said how to proceed in general.

I don't know what you are expecting to do, so I can't say anything about a solution.

If I use deinit, when I change indicator periods and go to a different timeframe, the period changes to default. 

Is there no way to avoid this? 

That’s my question 

 
Chioma Obunadike #:

If I use deinit, when I change indicator periods and go to a different timeframe, the period changes to default. 

Is there no way to avoid this? 

That’s my question 

What?

If I understand right, that's exactly what you told the code to do.

Are you maybe trying to display different Time frames for the loaded indicators on the chart?

IE you load the indicators in TF 1 Minute and then switch to TF 5 Minute and you want the indicators you loaded, to stay on TF 1 Minute?


If that's the case, then your approach is wrong.

 
Dominik Egert #:
What?

If I understand right, that's exactly what you told the code to do.

Are you maybe trying to display different Time frames for the loaded indicators on the chart?

IE you load the indicators in TF 1 Minute and then switch to TF 5 Minute and you want the indicators you loaded, to stay on TF 1 Minute?


If that's the case, then your approach is wrong.

hmm. I did this instead and it solved the problem... or so it seems.

But thank you Dominik, you always help out. 

 int  number = ChartIndicatorsTotal(0,0);
   Print(number);
   if(number<2)
Reason: