Skip to content
All library documents

Faster MT5 Bar Counting and Time-Based Bar Shifts

Article MQL5 code base

Summary

This document presents alternative MetaTrader 5 functions for finding a bar index by time and counting bars within a time interval. The proposed bar-shift function derives its result from the custom counting function, while an optional exact-match check returns a missing-bar result when the requested time does not align with an available bar. The counting method caches prior values and uses differences between cumulative counts to reduce repeated work.

The author reports that the built-in range-counting function can cause delays exceeding ten seconds when it returns zero, and gives a short example of the lag. The code handles timeframe boundaries, including weekly and monthly bars, and updates cached values when the symbol, timeframe, or relevant time changes. These are implementation details for MT5 data access rather than a trading signal or strategy. The reported speed issue is tied to the publication context; the document supplies no benchmark data or confirmation for current terminal versions, so users should validate behavior in their own environment.

Key ideas

  • The proposed bar-shift function obtains an index through a custom range-counting function.\nThe counting function caches earlier totals to reduce repeated calculations.\nThe implementation accounts for symbol and timeframe changes, as well as weekly and monthly boundaries.\nThe document reports long delays from the built-in function in a specific zero-result case, without providing benchmark data.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.