Stopping the indicator refresh with time frame change

 

Hi. Could someone please suggest a way to stop an indicator from refreshing when changing the time frame of the chart in MT5. Whenever I change the timeframe of the chart, it refresh the indicator and generate the same alert again and again. Please suggest a way to avoid this.

Thank you.   

 
rhodium1trading: Hi. Could someone please suggest a way to stop an indicator from refreshing when changing the time frame of the chart in MT5. Whenever I change the timeframe of the chart, it refresh the indicator and generate the same alert again and again. Please suggest a way to avoid this.

If the time-frame changes, then data will change, and your indicator is obligated to recalculate its buffers/plots.

However, to prevent the alert from reactivating, just make sure that it reacts only to when a new bar is truely detected, and not when it's in the middle of a bar due to a time-frame change.

Below is an example how to do it for an EA but the same principal applies to indicators. You just need to adapt it to the OnCalculate() parameter data instead.

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:38

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.

 
Fernando Carreiro #:

If the time-frame changes, then data will change, and your indicator is obligated to recalculate its buffers/plots.

However, to prevent the alert from reactivating, just make sure that it reacts only to when a new bar is truely detected, and not when it's in the middle of a bar due to a time-frame change.

Below is an example how to do it for an EA but the same principal applies to indicators. You just need to adapt it to the OnCalculate() parameter data instead.


Hi Fernando. Thank you very much for your suggestion.

Does it mean that if I use 1 minute period for “dtBarCurrent  = iTime( _Symbol, _Period, 0);”, the indicator is going to refresh only after 1 minute? So, I can keep on changing timeframes within 1 minute, but after 1 minute when I change the time frame, the indicator will refresh again.

If that’s the case, this method is not going to help me because I’m using 1 minute time frame and I need to change time frames on the chart even after 1 minute.

Anyway, thanks again for your attention.

 
rhodium1trading #: Hi Fernando. Thank you very much for your suggestion. Does it mean that if I use 1 minute period for “dtBarCurrent  = iTime( _Symbol, _Period, 0);”, the indicator is going to refresh only after 1 minute? So, I can keep on changing timeframes within 1 minute, but after 1 minute when I change the time frame, the indicator will refresh again. If that’s the case, this method is not going to help me because I’m using 1 minute time frame and I need to change time frames on the chart even after 1 minute. Anyway, thanks again for your attention.

No, I am suggesting that you use something like this ...

dtBarCurrent  = time[ rates_total - 1 ]; // For non-series array

However, I am only speculating here because you have not provided your Indicator's source code, so I have no ideia how it is currently working.

 
Fernando Carreiro #:

No, I am suggesting that you use something like this ...

However, I am only speculating here because you have not provided your Indicator's source code, so I have no ideia how it is currently working.

Hi, Fernando. Thanks again.

All I want is to calculate the gap between the current price and the price when the indicator is first loaded in to the chart. So,

Price gap = Current Price – Price when the indicator is first loaded to the chart  

In order to do this, getting the "Price when the indicator is first loaded to the chart (initialprice)" is the tricky part for me. I used the following code before “int OnInit()” which seems to be ok if I do not change the time frames of the chart.

static double initialprice=(iClose(Symbol(),PERIOD_M1,0));

When I change the timeframes, the indicator loads again and the “Price when the indicator is first loaded to the chart (initialprice)” keeps on changing. So, kindly requesting help to stop this and keep the initial price unchanged even when changing time frames of the chart.

 
You want others to help you change your program so that the alert is not triggered when you change the timeframe. Kindly provide the code that shows what triggers the alert.
Reason: