If as you say, you don't need to do the calculations all the time
Limit to new bars only
if ( prev_calculated < rates_total)
Free download of the 'MovingAverages.mqh Part II by Wiliam210' indicator by 'William210' for MetaTrader 5 in the MQL5 Code Base, 2024.06.22
Another, more complex solution is to set a timer of the duration you want.

- www.mql5.com
if ( prev_calculated < rates_total)
You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)

Champagne !!!!
Thank you guys. I will try the suggested approaches. I already made a solution for "cold analysis", which is, to plot the data I calculated in the chart when the markets are closed ( I did a function within a EA to plot objects OBJ_TREND for each candle) It is not a definitive solution but at least I can visually teste if the the calculations are useful. I start to hate the mql5 indicator way of event handlings... If my analysis turns out to be useful I will still need a way to plot it on a live chart during market hours...

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear all, I created the code for the indicator below, whose objective is to show a theoretical value for the minidollar (WINFUT, which is the USDBRL symbol) depending on the variation of the minidollar (WDOFUT, brazilian main index symbol). The rationale is: if WDOFUT is falling 0.5% I reverse the sign of this fall and consider that WINFUT should be rising 0.5%.
It works correctly, but it makes the graph slow. Could anyone take a look and see if they can find the problem that is making it difficult (slowness in the graph)?
Additional details: the OnCalculate is called more than 4 thousand times per minute. I think this might be the problem, it is overwhelming for metatrader. But I dont need that high frequency. Is it possible to plot the indicator with less frequency? Lets say, 10 times per minute... it would be nice to plot the indicator using OnTimer instead of OnCalculate but I am not sure if mt5 allows it, anyone knows?