History Preloader
MT4 downloads history in the background and does not block a calling function until it arrives. An EA that calls iMA, iClose or a custom indicator on a symbol other than the chart's own - or even on the chart's own symbol right after the terminal has just started - can receive far fewer bars than it asked for, and nothing raises an error. iBars() simply returns a smaller number than expected, and code that assumes "the bars I asked for are already there" reads stale or unfilled data, or divides by a bar count that has not caught up yet. This is one of the most common silent bugs in published EAs, and it is silent precisely because there is no exception to catch. HistoryEnsureLoaded turns that race into an explicit, bounded wait: SymbolSelect(symbol, true) first, because a symbol that was never added to Market Watch does not download history at all no matter how long you wait; then a polling loop that keeps nudging the terminal via RefreshRates and checks iBars against the requirement, until either the bars arrive or a caller-supplied timeout expires. The function returns a plain bool, so a caller can decide what "history never arrived" should mean for their program rather than having the decision made for them by whatever partial data happened to exist. HistoryEnsureLoadedMulti extends this to several symbols and timeframes at once, taking three parallel arrays (symbols, timeframes, bar counts) rather than a single 2D array of requests - MQL4 only allows the leftmost dimension of a 2D array to be dynamic, so an arbitrary list of independent (symbol, timeframe, count) triples is expressed as three same-length 1D arrays instead of forcing a fixed or ragged 2D shape. Notes and limitations - This does not force a download from nothing - it relies on the terminal actually being able to reach the trade server and having that symbol available. If the symbol name is wrong for your broker, or the terminal is offline, the wait will simply run out and return false; that is the intended behaviour, not a bug in the wait loop. - The wait loop calls Sleep(), so it is meant for OnInit/OnStart, not for OnTick or OnCalculate - using it inside a tick-critical path will stall tick processing for up to maxWaitMs. - barsNeeded is a lower bound, not an exact request. Once the terminal has more bars than requested it is still a pass; this library does not trim history down to a specific count. - On the Strategy Tester, history is normally already fully loaded before a test starts, so these functions will typically return immediately - their value is mainly for live/demo charts and freshly attached EAs.
Script · MT4 · v1.0 · Free
About History Preloader
History Preloader is a script for MetaTrader 4. It has 4 settings, such as Bars needed, Max wait ms and Other symbol1. MQL source code included.
Files in the download: HistoryPreloader.mqh, HistoryPreloader.txt, HistoryPreloaderDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bars needed | 2000 | Bars required on the current chart's symbol/timeframe |
| Max wait ms | 4000 | Maximum time to wait per symbol, in milliseconds |
| Other symbol1 | EURUSD | Second symbol to check (must exist at your broker) |
| Other symbol2 | GBPUSD | Third symbol to check (must exist at your broker) |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code