Yes sir, this is a very common question for anyone developing strategies in MT5.
There is no built-in function in MQL5 that directly tells you a new candle has started.
The standard and recommended approach is to compare the opening time of the latest candle with the previously stored candle time.
Whenever the latest candle time changes, it means a new candle has been formed.
You can do it like this:
datetime lastBarTime = 0;
bool IsNewCandle()
{
datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);
if(currentBarTime != lastBarTime)
{
lastBarTime = currentBarTime;
return true;
}
return false;
}
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.