FX NOOKAI CODE LAB
Loading

MT5 Error Codes: Meanings, Causes, and Practical Fixes

By FXNook Lab team

To diagnose an MT5 error, first identify where it came from: a trade-server retcode, an MQL5 runtime error returned by GetLastError(), or a MetaEditor compile error. Each points to a different problem. Log the trade result separately from runtime errors, inspect the request and symbol constraints, then reproduce the issue in the Strategy Tester before changing the EA.

Identify which kind of MT5 error you have

MT5 does not use one shared error-code list for every failure. A trade server can reject a validly compiled EA's request, MQL5 can encounter a runtime fault while executing the program, and MetaEditor can refuse to compile the source at all. The code and the tab where it appears tell you which path to investigate.

Kind of messageWhere to find itWhat it tells you
Trade-server retcodeToolbox Experts or Journal; also the result returned by a trade functionThe server or terminal's outcome for a trade request, such as invalid volume or market closed.
MQL5 runtime errorToolbox Experts and the Strategy Tester log; it may also be returned by GetLastError()The program encountered a runtime problem, such as invalid array access or a failed operation.
Compile errorMetaEditor, in the Errors tab after compilingThe source could not be compiled. The compiler reports a file, line, and often a column; this is not a trade-server result.

The Toolbox can be opened in the terminal from the View menu or with Ctrl+T. In the Strategy Tester, read the tester's Journal and Experts output for the run you are investigating. MetaEditor compiler messages are separate: fix those first, because an EA with compile errors cannot run.

Do not diagnose from a number alone. Record the complete log line, including its timestamp, symbol, request details, and any text after the code. A retcode describes the trade operation; a runtime error can identify a fault in the EA; a compile message points to source code that MetaEditor cannot build.

Log trade results and runtime errors separately

With the MQL5 Standard Library's CTrade class, the Boolean returned by a method such as Buy() tells you whether the method's basic request handling succeeded. It does not, by itself, prove that the trade was executed. Read ResultRetcode() and ResultRetcodeDescription() to find the reported trade outcome.

GetLastError() serves a different purpose: it reads the last MQL5 runtime or terminal error. A rejected request can have a meaningful trade retcode even when GetLastError() is zero. Reset the stored runtime error before the operation if you plan to log it, so an older error is not mistaken for the cause of this request.

// MQL5 script: sends one market buy request and logs both result types.
#include <Trade/Trade.mqh>

CTrade trade;

void OnStart()
{
   trade.SetDeviationInPoints(20);

   ResetLastError();
   bool method_ok = trade.Buy(0.01, _Symbol);
   int runtime_error = GetLastError();

   Print("CTrade Buy returned: ", method_ok);
   Print("Trade retcode: ", trade.ResultRetcode(),
         " - ", trade.ResultRetcodeDescription());
   Print("GetLastError: ", runtime_error);
}

This script makes one request when run as a script; it is not an EA template for sending orders on every tick. Use a test account or the Strategy Tester when checking order logic. Before running any request, make sure the symbol, account permissions, volume, and trading conditions are appropriate for the test.

For code that calls the low-level OrderSend() function directly, inspect its Boolean return and the fields in the MqlTradeResult structure, especially retcode and comment. For either approach, keep the server result distinct from GetLastError(); they answer different questions.

Common MT5 trade retcodes and what to check

Trade retcodes are defined by the trade operation's result. The codes below are common examples, not a complete catalogue. Some successful or partially successful outcomes are also retcodes, so do not treat every nonzero value as a failure.

RetcodeMeaningLikely causeFirst relevant check
10014 TRADE_RETCODE_INVALID_VOLUMEVolume is invalid.The requested lot size is below the minimum, above the maximum, or not aligned to the allowed step.Read SYMBOL_VOLUME_MIN, SYMBOL_VOLUME_MAX, and SYMBOL_VOLUME_STEP with SymbolInfoDouble(); normalize the requested volume to the broker's constraints.
10015 TRADE_RETCODE_INVALID_PRICEPrice is invalid.The request uses a stale, zero, or incorrectly rounded price, or a price unsuitable for the order type.Get a fresh quote with SymbolInfoTick() and check that the price matches the symbol's tick size and the intended operation.
10016 TRADE_RETCODE_INVALID_STOPSStop levels are invalid.A stop loss or take profit is on the wrong side of the market or too close under the symbol's rules.Compare the requested levels with the current bid or ask and SYMBOL_TRADE_STOPS_LEVEL. Check freeze restrictions where an operation modifies an existing position.
10018 TRADE_RETCODE_MARKET_CLOSEDThe market is closed.The symbol is outside its trading session, or a session or holiday restriction applies.Read the symbol's trading sessions and the server-time context in the log. Do not assume the computer's local clock matches server time.
10019 TRADE_RETCODE_NO_MONEYInsufficient funds for the operation.The account does not have enough free margin for the requested volume and position.Review account free margin, the symbol's contract settings, and the requested size. Use OrderCalcMargin() where suitable to estimate required margin.
10004 TRADE_RETCODE_REQUOTEThe server returned a requote.The quoted price changed before the request could be completed.Fetch a fresh tick, inspect the allowed deviation, and decide whether the EA's entry conditions still hold at the new price.
10020 TRADE_RETCODE_PRICE_CHANGEDThe price changed.The request was based on a price that is no longer current.Log the requested and current prices. If the strategy still permits entry, form a new request from a fresh quote rather than resending stale values.
10021 TRADE_RETCODE_PRICE_OFFNo usable quote is available.The symbol may not have a current bid or ask, or a quote may not yet be available.Read the tick and ensure both the symbol and quote are available before building the request.
10030 TRADE_RETCODE_INVALID_FILLThe filling mode is invalid.The request's filling policy is not supported for that symbol or account setup.Inspect the symbol's supported filling mode and configure the request or CTrade accordingly.
10009 TRADE_RETCODE_DONEThe request completed.This is a successful outcome, not an error.Check the resulting deal or position and ensure the EA updates its own state only once.

Symbol rules matter as much as the EA's calculations. For example, a volume that looks reasonable in a parameter input may still violate the symbol's minimum or step. A stop distance that worked on one broker's symbol may be rejected on another because the minimum distance differs. For a deeper stop-loss diagnosis, see Invalid Stop Loss in MT5: Causes, Checks, and Fixes.

When a request is rejected, log the actual request values, not just the retcode: symbol, order type, volume, price, stop loss, take profit, and filling policy. Also record the symbol constraints used to build it. That makes it possible to tell whether the EA calculated the wrong values or used correct values that the current trading conditions do not allow.

Common MQL5 runtime errors and their fixes

A runtime error is a program execution problem, not a trade-server decision. Typical causes include accessing an array element outside the available range, using a failed indicator handle, assuming CopyBuffer() filled data when it did not, or passing unsuitable values to a function. A compile can succeed while these faults remain in the running code.

Invalid array access

Before reading an array element, make sure the array contains that index. This is especially important when code relies on the latest bar, copies a variable number of values, or assumes history has already loaded. Log the array size and the index at the point of access, then step through the relevant path in MetaEditor's debugger to see which assumption fails.

Bad indicator handles or buffer copying

In MQL5, functions such as iMA() return an indicator handle rather than an indicator value. Verify that the handle is not INVALID_HANDLE, and release a valid handle when it is no longer needed. Before using copied values, check the return value from CopyBuffer(); it reports how many elements were copied, and a result less than the requested count means the buffer is not ready for the code's assumption.

Indicator data may not be available immediately, particularly while history is loading. Do not read the output array simply because the copy call was made. Handle a short or failed copy deliberately, log enough context to identify the symbol, timeframe, buffer, and requested range, and try again only when the program's logic allows it.

Incorrect function parameters or failed operations

A function can compile with correctly typed arguments and still receive a value that is invalid for the current operation. Validate the symbol, timeframe, index, price, and other inputs before using them. When a function reports failure, capture its documented return value and inspect GetLastError() immediately if that function uses the terminal error state.

In MetaEditor, place a breakpoint near the failing operation and run the debugger to inspect values one step at a time. The Toolbox's Experts output and the Strategy Tester logs provide the runtime context, while the debugger helps locate the source line and follow the condition that led there. If the EA has no runtime fault but simply never reaches its trading code, the problem may be an entry condition or early return, not an error code.

A repeatable workflow for an unknown MT5 error

  1. Record the full message. Copy the exact text, code, timestamp, symbol, and nearby log lines. Include the result description or server comment where available; a bare number often leaves out the useful detail.
  2. Identify its source. Decide whether it is a trade retcode, a value from GetLastError(), or a MetaEditor compile message. Use the tab and the API call that produced it, rather than trying to find one universal MT5 error list.
  3. Inspect the request and symbol rules. Log the trade parameters and compare volume, price, stops, filling mode, and permissions with the current symbol properties and account conditions. For a request built with MqlTradeRequest, inspect the full structure before sending it.
  4. Reproduce the same conditions. Use the Strategy Tester with the same EA inputs, symbol, timeframe, and a relevant date range. Select a tick model appropriate to the behavior being tested, and inspect the tester's logs. A failure that depends on a live quote or server session may not reproduce exactly in historical testing, so retain the live log details too.
  5. Make one targeted change. Fix the cause indicated by the evidence, such as aligning volume to the symbol step or correcting a buffer count check. Avoid changing several inputs at once; otherwise you will not know which change addressed the fault.
  6. Retest both outcomes. Confirm that the expected request succeeds in the test case and that a rejection is handled without creating duplicate or unintended orders. Read the new logs rather than assuming a successful compile proves the trading behavior is correct.

For an EA that compiles but does not open a trade, follow the entry path as well as the order result. A filter may never pass, trading permission may be off, or the EA may return before reaching the send call. The guide to MT5 EA Not Taking Trades: Checks, Causes, and Fixes covers those cases.

Do not hide the cause with blind retries

Clearing the stored error, retrying every failed request, or changing parameters until the message disappears can make the original fault harder to find. ResetLastError() is useful before an operation when you want a clean runtime-error reading; it does not repair the request or explain a trade-server rejection. Log the original result before taking any recovery action.

Price changes and requotes can be transient, but even then, an automatic retry needs a clear rule. Fetch a fresh quote, check whether the strategy's conditions still permit the trade, and limit retries to the cases the EA is designed to handle. Repeatedly resending an invalid volume, unsupported filling mode, or impossible stop distance will not fix a persistent request or logic fault and may cause repeated order attempts.

Do not increase stop distances, alter volume, or widen price deviation simply to force a request through. First establish which constraint is being violated and why the EA produced those values. If the intended trading rule is unclear, preserve that rule while diagnosing the technical problem rather than silently changing the strategy.

Verify the fix before running the EA live

In the Strategy Tester, confirm that the EA reaches the intended trade path and logs the values used to form its request. The values should satisfy the symbol's volume, price, stop, and filling requirements for the test conditions. The retcode should match the outcome the EA is designed to handle, and a successful trade should update the EA's state without sending another order unintentionally.

Also test a rejection path where practical. The EA should record the failure, avoid treating a rejected request as an open position, and avoid looping into repeated requests on every tick. A clean compile is only the first check: tested or historical results do not guarantee future results, and live server conditions can differ from the tester.

If the logs show a compile problem, use FXNook Lab's MQL4/MQL5 error-fixing page to upload code with MetaEditor errors and see the cause and a checked fix. If the EA compiles but the trade path is unclear, the EA debugger is designed to trace why it does not trade. The practical next step is to capture the complete message and identify its source before changing the EA.

Questions

Where can I find MT5 error codes?

For trade outcomes, look in the terminal's Toolbox under Experts or Journal, and inspect the trade function's result in the EA. Runtime messages also appear in Experts and in Strategy Tester logs, while compile errors are listed separately in MetaEditor's Errors tab.

What is the difference between an MT5 retcode and GetLastError()?

A retcode describes the outcome of a trade request, including a server rejection or successful execution. GetLastError() reads the MQL5 runtime or terminal error state, so it may be zero even when the trade request was rejected.

How do I see why an MT5 trade request was rejected?

Log the trade retcode and its description, then record the request values and the symbol's relevant constraints. With CTrade, read ResultRetcode() and ResultRetcodeDescription(); with OrderSend(), inspect the MqlTradeResult fields, including retcode and comment.

Why does my MT5 EA show an error but not open a trade?

The message may describe a rejected request, a runtime fault, or a compile problem, and each has a different fix. An EA can also reach no trade call at all because a filter blocks the entry, permissions are disabled, or code returns early, so follow the Experts log and the entry path.