Your setup, automated · Demo firstcontact@stratcorealpha.com · DE
All trading software errors

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.

Native MT5 screenshot recording six passed local array-read cases and zero orders.

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. 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. 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. 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.

Check my EA locally

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 behaviour

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

These public questions explain the error; use your own EA log to check whether the same cause applies.

Trading risk disclosure

Futures and forex trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing one's financial security or lifestyle. Only risk capital should be used for trading, and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results.

Hypothetical performance disclosure

Hypothetical performance results have many inherent limitations. No representation is being made that any account will or is likely to achieve profits or losses similar to those shown. There are frequently sharp differences between hypothetical performance results and the actual results subsequently achieved by any particular trading program. Hypothetical results are generally prepared with the benefit of hindsight, do not involve financial risk, and cannot completely account for the impact of financial risk in actual trading. Market conditions, the ability to withstand losses, and adherence to a trading program can all materially affect actual results.

NinjaTrader trademark disclosure

NinjaTrader® is a registered trademark of NinjaTrader Group, LLC. No NinjaTrader company has any affiliation with the owner, developer, or provider of the products or services described herein, or any interest, ownership or otherwise, in any such product or service, or endorses, recommends or approves any such product or service.

Testimonials, when shown, may not represent the experience of other clients and are not a guarantee of future performance or success. Virtual-currency trading carries additional risks. See the CFTC customer advisories. Read the full financial risk disclaimer.

MQL array out of range: check 3 values before reading a buffer