Duplicate indicator at timeframe change - MQL5

 
On Init()

indicatoreHandle  =  iCustom(_Symbol, TimeFrame, ".\PathUsata\indicatore.ex5", Periods, Multiplier);

// Aggiunge indicatore al grafico

   ChartIndicatorAdd(0, 0, indicatoreHandle   );



void OnDeinit(const int reason)
  {
        IndicatorRelease(indicatoreHandle  );

Hi everyone

In MQL5

I use the sequences above to load a custom indicator from EA to manage its results.

When I change the TF, the same indicator is reloaded with each change of TF, confusing the EA

How can I solve the problem?

 
RedTaurus:

Hi everyone

In MQL5

I use the sequences above to load a custom indicator from EA to manage its results.

When I change the TF, the same indicator is reloaded with each change of TF, confusing the EA

How can I solve the problem?

Don't add it twice (or more).
 
Alain Verleyen #:
Don't add it twice (or more).

I don't add it, it does it automatically when changing TF.


And that's what I'd like to avoid.

I just need to change the parameters.

 
RedTaurus #:

I don't add it, it does it automatically when changing TF.


And that's what I'd like to avoid.

I just need to change the parameters.

Of course you add it, do you understand what your code is doing ?

When you change the timeframe ChartIndicatorAdd() is executed again, but the indicator is never removed from the chart.

 
Alain Verleyen #:

Of course you add it, do you understand what your code is doing ?

When you change the timeframe ChartIndicatorAdd() is executed again, but the indicator is never removed from the chart.

Ok I understand.

I just don't know where to enter the command to clear the indicator before reloading it.

If I put it in

void OnDeinit()

It doesn't delete it.

How can I fix it? Can you give me a tip?

 
RedTaurus #:

Ok I understand.

I just don't know where to enter the command to clear the indicator before reloading it.

If I put it in

void OnDeinit()

It doesn't delete it.

How can I fix it? Can you give me a tip?

IndicatorRelease() is useless for your case.

You need to use ChartIndicatorDelete(). Or you can just add it once at the first start, all depends what you want to do.

 
RedTaurus #:

I don't add it, it does it automatically when changing TF.


And that's what I'd like to avoid.

I just need to change the parameters.

In OnInit, get the handle with the parameters you need and add the indicator to the chart.

In OnDeinit, delete the indicator from the chart, then release the handle.

 
Alain Verleyen #:

IndicatorRelease() is useless for your case.

You need to use ChartIndicatorDelete(). Or you can just add it once at the first start, all depends what you want to do.

I try to explain what I would like to achieve.

I load the indicator into the chart and on first start it takes the parameters from the EA.

When I change TF, the indicator is reloaded by duplicating itself always using the parameters passed by the EA.

I wish changing TF would not reload.

And that if I change the parameters from the EA, they are also taken from the indicator.

At the moment if I change the parameters in the EA the indicator keeps the first ones received.


So I changed the listing.


In the statements I put

int MyHandle = iCustom(_Symbol, TimeFrame, ".\MyIndicator", Periods, Multiplier);

and in OnInit() I put


ChartIndicatorDelete(0,0, MyHandle );

ChartIndicatorAdd(0, 0, MyHandle );


this way if I change TF it is not duplicated.

Now I have to solve how to change the parameters of the indicator when they are changed in the EA.
 
Samuel Manoel De Souza #:

In OnInit, get the handle with the parameters you need and add the indicator to the chart.

In OnDeinit, delete the indicator from the chart, then release the handle.

Your solution is here. Did you try that?

 
MyHandle = iCustom(_Symbol, TimeFrame, ".\MyIndicator", Periods, Multiplier);

This has to be in OnInit, otherwise when parameters change the indicator handle won't change.

 
RedTaurus #:

I try to explain what I would like to achieve.

I load the indicator into the chart and on first start it takes the parameters from the EA.

When I change TF, the indicator is reloaded by duplicating itself always using the parameters passed by the EA.

I wish changing TF would not reload.

And that if I change the parameters from the EA, they are also taken from the indicator.

At the moment if I change the parameters in the EA the indicator keeps the first ones received.


So I changed the listing.


In the statements I put

int MyHandle = iCustom(_Symbol, TimeFrame, ".\MyIndicator", Periods, Multiplier);

and in OnInit() I put


ChartIndicatorDelete(0,0, MyHandle );

ChartIndicatorAdd(0, 0, MyHandle );


this way if I change TF it is not duplicated.

Now I have to solve how to change the parameters of the indicator when they are changed in the EA.

ChartIndicatorDelete() doesn't take a handle as parameter, check the documentation.

If you want the EA to be able to use the indicator which is on the chart (on the associated parameters) you need to use ChartIndicatorGet() instead of iCustom().