MT5 · code and request checks
MQL array out of range: check 3 values before reading a buffer
Your indicator cannot read a value that is absent from its array. My native MT5 run passed six array-read checks without executing an invalid read. I check the returned count and array size before reading an indicator value.

Arnold Holm · 2026-10-19
What the error means
An array holds a sequence of values. Its size tells you how many elements exist. With three elements, the valid indices are zero, one and two. Three is the size, but it is already outside the array. A loop that includes the size as its final index can therefore reach a value that does not exist. A forum author converting an MT4 indicator to MT5 reported an array error and posted code that copied indicator values and then read element zero. The posted code also attempted more buffer bindings than its declared buffer count. These are separate places to inspect. The discussion does not establish that a single guard repairs that whole indicator. For a copied indicator value, keep the return count from CopyBuffer. Requesting one element is an intention. The return value tells you how many elements were actually copied, or reports failure. An empty or failed result must stay out of the array access. An allocated array alone does not establish that its old values belong to the latest successful request. The helper below receives the actual array, the count from the current copy and the index you intend to read. It rejects a non-positive count, a negative index, an index at or beyond the copied count and an index at or beyond the physical array size. Only then does it read the element. Keep these inputs tied to the same operation. Passing a count from an earlier copy would change what this check means.
Three code paths to check
1. The copy result was ignored.
Save the return count from the current CopyBuffer call. Keep zero and negative results out of the read. Record the handle, buffer and requested shift separately so the failed request can be investigated.
2. The loop reached the size itself.
For a three-element ordinary array, the final valid index is two. Check the upper bound before entering the read. Also check a lower bound when an offset subtracts from the loop index.
3. The buffer setup was assumed to succeed.
Keep the declared buffer count, binding indices and SetIndexBuffer return values together. Read direction and which candle a position refers to need their own checks after the binding. The guard here checks bounds, not trading meaning.
Six fixed cases in native MT5
I compiled and ran my original read-only script in an isolated MT5 terminal on 19 October 2026. The empty-array input was blocked. A negative index was blocked. The last valid index in a three-value array returned 30. An index equal to the size was blocked. An index outside a shorter copied count was blocked even though the array itself was larger. A negative copy-result input was blocked. All six comparisons passed. The runtime log and screenshot retain those outcomes. No invalid element was deliberately accessed. The value 30 is a fixed input used to check the helper, rather than a market price or trading result. The chart behind the text is terminal background. This is a small boundary you can test before investigating the rest of an indicator. In your own program, preserve the original input and error line first. A bounds check should stop the unavailable read; it should not quietly turn missing data into an entry signal.
bool SafeRead(const double &values[],int copied,int index,double &out)
{
if(copied<=0 || index<0 || index>=copied || index>=ArraySize(values)) return false;
out=values[index];
return true;
}Six fixed local inputs, zero orders and no real CopyBuffer call; no third-party indicator repaired, candle order established or live feed tested.
How you know it is fixed
Absent input stays unread
Use an empty array, zero count and negative count. Each must return false before reading.
The last valid index stays available
For values 10, 20 and 30 with count three, index two returns 30; index three stays blocked.
Your five-minute Journal check
Find the array access named by your Journal error. Log its requested index, actual array size and copy return count before that line.
Your own source
Find the next line to check
Start with the free EA-Check. Your code stays in your browser. It highlights patterns with line numbers; it does not prove the broker accepts an order.
Want one small change shown on your own case?
DEMO0: send the problem and try a free compiled demo first. R49: one authorised, reproducible MT4/MT5 defect in readable source up to 500 lines, with three agreed cases. 49 USD fixed. Delivery within 48 hours after source review and funding. The exact case is agreed before funded work.
Send the expected and actual behaviourRelated checks
Common questions
Does a requested copy count prove the value exists?
Keep the actual return count and array size from that operation. A requested count is not a successful copy.
Did this fix the original forum indicator?
No. It tested six fixed inputs in an original local helper. Buffer setup, indicator readiness and candle direction remain separate checks.
Sources and limits
- Original MT5 indicator array question
- CopyBuffer return count
- ArraySize
- SetIndexBuffer
- Native six-case runtime log
These public questions explain the error; use your own EA log to check whether the same cause applies.