Now, wherever you want your strategy to execute only once per candle, simply use the function like this:
void OnTick()
{
if(IsNewCandle())
{
// Your strategy logic here
Print("New candle detected.");
}
}
This is the most commonly used approach in MQL5 because:
-It prevents your strategy from executing on every incoming tick.
-It ensures your trading logic runs only once for each newly formed candle.
-It works on any timeframe since it uses the current chart timeframe (PERIOD_CURRENT).
So, instead of looking for a dedicated “new candle” function, the recommended practice in MT5 is to compare the latest candle’s opening time with the previously stored time. This is the approach used in most professional MQL5 Expert Advisors.
Verified Solution
This is what I am looking for, thanks a lot for the detailed answer with the codes also, it’s really help me to detect new candle. Thanks again!– Radhesh JoshiJul 28