Common MQL5 & EA Errors — From Compile to OrderSend
When your Expert Advisor won’t compile, or OrderSend fails with codes like 10016 (invalid stops) or 10019 (not enough money), you need a clear map of causes and fixes. The MQL5 Trade Server Return Codes describe every retcode returned in MqlTradeResult after OrderSend(). This guide covers compile errors, the most frequent trade return codes, invalid stops (10016), not enough money (10019), trade disabled (10017, 10027), and why you should use OrderCheck() before sending — with official references and quick solutions.
Compile Errors: Fix Before Running
Before your EA can run or send orders, it must compile in MetaEditor (F7). Common issues:
- Undefined variable or function — Check spelling and that you included the right headers; MQL5 compilation errors list all codes.
- Wrong types — e.g. passing
intwherelongordoubleis required; fix types to match function signatures. - Missing semicolon or bracket — The compiler points to the line; fix the syntax and recompile.
Fix all errors (0 errors in the Build tab) before attaching the EA to a chart. Build your first EA and MQL5 programming reference help with structure and APIs.
Trade Server Return Codes (OrderSend)
OrderSend() sends a request to the trade server; the result is in MqlTradeResult. The retcode field is the server’s answer. Per MQL5: "All requests to execute trade operations are sent... using function OrderSend(). The function execution result is placed to structure MqlTradeResult, whose retcode field contains the trade server return code."
Important codes:
| Code | Constant | Meaning |
|---|---|---|
| 10009 | TRADE_RETCODE_DONE | Request completed |
| 10008 | TRADE_RETCODE_PLACED | Order placed |
| 10016 | TRADE_RETCODE_INVALID_STOPS | Invalid stops in the request |
| 10017 | TRADE_RETCODE_TRADE_DISABLED | Trade is disabled |
| 10019 | TRADE_RETCODE_NO_MONEY | Not enough money |
| 10027 | TRADE_RETCODE_CLIENT_DISABLES_AT | Autotrading disabled by client |
| 10031 | TRADE_RETCODE_CONNECTION | No connection with the trade server |
Always check result.retcode after OrderSend; do not assume true means the deal was executed.
Invalid Stops (10016)
TRADE_RETCODE_INVALID_STOPS (10016) means the broker rejected your Stop Loss or Take Profit — usually because they are too close to the current price. Each symbol has a minimum distance (in points) defined by the server. In MQL5 you can read it with SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL).
Fix:
- Compute SL/TP so that
|order_price - sl|and|order_price - tp|are at leastSYMBOL_TRADE_STOPS_LEVELpoints (useSymbolInfoDouble(symbol, SYMBOL_POINT)to convert). - Or set SL/TP to 0 and manage exits in code (e.g. close by market order later).
Not Enough Money (10019)
TRADE_RETCODE_NO_MONEY (10019) means margin or balance is insufficient for the requested volume. Check AccountInfoDouble(ACCOUNT_MARGIN_FREE) and required margin for the symbol/volume before sending.
Fix: Reduce lot size, or deposit more. Use EA risk management to size positions safely.
Trade Disabled (10017, 10027)
- 10017 (TRADE_RETCODE_TRADE_DISABLED) — Trade disabled by the server (broker). Possible causes: symbol not tradeable, account restricted, or market closed.
- 10027 (TRADE_RETCODE_CLIENT_DISABLES_AT) — AutoTrading is off in the terminal. Press Ctrl+E or enable in Tools → Options → Expert Advisors.
Fix for 10027: Enable AutoTrading and ensure "Allow automated trading" is checked. For 10017, contact broker or try another symbol/account.
Use OrderCheck() Before OrderSend()
The OrderSend() docs recommend checking the request first: "It is recommended to check the request before sending it to a trade server. To check requests, use the OrderCheck() function." OrderCheck() returns whether the trade would be accepted and fills a structure with margin, free margin, and return code — so you can avoid sending orders that will fail with 10016 or 10019.
Quick Reference Table
| Symptom / Code | Cause | Fix |
|---|---|---|
| Won’t compile | Syntax/type errors | Fix errors in MetaEditor; see errors compile. |
| 10016 Invalid stops | SL/TP too close to price | Respect SYMBOL_TRADE_STOPS_LEVEL; or SL/TP = 0. |
| 10019 No money | Insufficient margin/balance | Reduce lot; check margin before send. |
| 10017 Trade disabled | Server-side restriction | Broker/symbol/account; contact broker. |
| 10027 Autotrading disabled | Client terminal | Enable AutoTrading (Ctrl+E), Options → EAs. |
| 10031 No connection | Network / server | Check connection; retry later. |
Next Steps
- Build Your First EA — Correct structure and compile.
- EA Risk Management — Position sizing and risk.
- Deploy and Maintain Your EA — Logging and troubleshooting on live/VPS.
References: MQL5 Trade Return Codes, OrderSend, OrderCheck, Compilation Errors, Runtime Errors.