Do work only at the time of the birth of a new candle.
Source code viewer
datetime previousCandleDatetime, currentCandleDatetime; void OnTick() { currentCandleDatetime = iTime(Symbol(), Period(), 0); if (currentCandleDatetime == previousCandleDatetime) return; previousCandleDatetime = currentCandleDatetime; // ... your work }Programming Language: MQL5