Source code
MQL5 array out of range: find the first invalid index
An array index must refer to an element that actually exists. Compiling successfully does not prove that every runtime loop uses a valid index. Start at the first reported line rather than patching all loops at once.
Write down the bounds
For a one-dimensional array with five elements, valid zero-based indexes are zero through four. A loop that includes index five goes too far. When reading i+1, leave room for that extra element.
Check the data you received
If you requested many bars but received fewer, the requested count is not a safe loop bound. Log the copied count, array length and failing index. Decide what the EA should do when history is not ready.
Reproduce a small case
Try the first few bars, an empty result and the minimum lookback. These cases often reveal a boundary error faster than another full-year run. Preserve the failing input so the correction can be checked again.
Keep the fix narrow
Do not suppress the message and continue with invented prices. A useful fix establishes valid bounds or waits for valid data. Editable source makes these small, focused improvements possible without changing the entire strategy.
Start with a free editable EA project · Explore MQLume source code
Back to top