Quick Answer
Trade return code 10013 (TRADE_RETCODE_INVALID) means that the trade request is invalid as a whole: a required field is missing, a field has a value that does not fit the action, or the fields contradict each other. The server does not look at your strategy, only at the request. This is different from the specific codes: a wrong volume gives 10014, a wrong price 10015 and wrong stops 10016, so 10013 is often what you get when something else is wrong, for example a field that you forgot to set.
The fastest way to find the problem is not to guess but to ask the terminal before you send: OrderCheck runs the same checks and returns a result with a return code and a comment. The sections below show which fields each action needs, and an EA that checks a request and prints the answer without sending anything.
From the author: I build and convert Expert Advisors. 10013 appears most often with hand-built
MqlTradeRequeststructures, because a structure that is not cleared holds random values in the fields you did not set.CTradefills most fields for you, so it gives 10013 less often, but it can still happen with a pending order or a wrong expiration.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| 10013 with a hand-built request | A field is missing or holds garbage | ZeroMemory the structure, then set every field the action needs |
| 10013 on a pending order | The price, the order type or the expiration does not fit | Check the type, the price and type_time |
| 10013 when changing stops | The wrong ticket field is used | A position change uses the position ticket, an order change the order ticket |
| 10013 for a symbol that is not in Market Watch | The symbol is not selected | SymbolSelect(symbol, true) first |
| Works in one place, fails in another | The code relies on defaults that differ | Set every field on purpose |
| You do not know which field is wrong | Nothing checks the request | Run OrderCheck and print its result |
What the Error Looks Like
With CTrade, a rejected order makes the call return false, and the code is in the result:
if(!trade.BuyLimit(lots, price, _Symbol, sl, tp, ORDER_TIME_GTC, 0, "bot"))
{
PrintFormat("Order failed. Retcode %u: %s",
trade.ResultRetcode(), trade.ResultRetcodeDescription());
}
With OrderSend, you get the code in MqlTradeResult.retcode. The list of all codes is in MQL5 trade return codes. For the specific codes see invalid stops, 10016 and unsupported filling mode.
What Each Action Needs
The request structure has many fields, but each action uses only some of them. The usual set, to check against the MetaEditor help for MqlTradeRequest:
| Action | Fields you set |
|---|---|
| TRADE_ACTION_DEAL (market order) | action, symbol, volume, type, price, deviation, usually sl, tp, type_filling, magic |
| TRADE_ACTION_PENDING | the same, plus a pending type, the pending price, type_time and, when it expires, expiration |
| TRADE_ACTION_SLTP (change stops of a position) | action, position, symbol, sl, tp |
| TRADE_ACTION_MODIFY (change a pending order) | action, order, price, sl, tp, type_time, expiration |
| TRADE_ACTION_REMOVE (delete a pending order) | action, order |
| TRADE_ACTION_CLOSE_BY | action, position, position_by |
The point is not the exact list but the habit: for each action, set what it needs, and set nothing by accident.
Cause 1: A Structure That Was Not Cleared
A local MqlTradeRequest starts with undefined values. If you set only some fields, the others hold whatever was in memory, and the request can be invalid in a way that changes from run to run:
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
Always clear both structures first. With the CTrade class, this is done for you.
Cause 2: A Missing Required Field
A market order without a symbol, with a volume of 0, or without a type cannot be valid. This is easy to cause when you copy a request and change the action but keep fields of the old one. Set action first, then each field of that action, in the same order every time.
Cause 3: A Pending Order That Does Not Fit
A pending order has more rules: the type must match the price side (a buy limit below the current price, a buy stop above it), the price must be set, and the expiration must match type_time. A buy limit above the price is closer to an invalid price (10015) or invalid stops (10016) than to 10013, but a wrong type_time or expiration combination, or a pending order type with a market action, can give 10013. Check the pending order rules in the help of your build.
Cause 4: The Wrong Ticket Field
Changing the stops of an open position uses the position ticket (request.position). Changing a pending order uses the order ticket (request.order). A request with the ticket in the wrong field is invalid:
// change the stops of an open position
request.action = TRADE_ACTION_SLTP;
request.position = positionTicket; // the position ticket, not an order ticket
request.symbol = positionSymbol;
request.sl = newSl;
request.tp = newTp;
Cause 5: A Symbol That Is Not Selected
A symbol that is not in the Market Watch can give an invalid request, or no prices at all. Select it before you read prices or send an order:
if(!SymbolSelect(symbol, true))
Print("Cannot select ", symbol);
Find the Bad Field With OrderCheck
OrderCheck takes the same request and returns a check result. It does not send anything. The check result has a return code and a comment, and also shows what the account would look like after the trade (margin, free margin), which is also useful when you suspect not enough money (10019):
bool CheckRequest(MqlTradeRequest &request)
{
MqlTradeCheckResult check;
ZeroMemory(check);
bool ok = OrderCheck(request, check);
PrintFormat("OrderCheck ok=%s retcode=%u comment=%s",
ok ? "true" : "false", check.retcode, check.comment);
return ok;
}
A check result with a retcode of 0 usually means that the request would be accepted, and any other code names the problem. Read the comment, fix the field it names, and check again. Use this in an EA before OrderSend, and log the result when a check fails.
A Full EA That Checks Two Requests
This EA does not place trades. It builds one request with missing fields and one with all required fields set, runs OrderCheck on both and prints the answers. Run it on a demo account:
#property strict
int OnInit()
{
// 1. A request with fields missing: no symbol, no volume
MqlTradeRequest bad;
ZeroMemory(bad);
bad.action = TRADE_ACTION_DEAL;
bad.type = ORDER_TYPE_BUY;
Print("--- Request with missing fields");
CheckRequest(bad);
// 2. A request with the fields of a market order set
MqlTradeRequest good;
ZeroMemory(good);
good.action = TRADE_ACTION_DEAL;
good.symbol = _Symbol;
good.volume = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
good.type = ORDER_TYPE_BUY;
good.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
good.deviation = 10;
Print("--- Request with the fields set");
CheckRequest(good);
return INIT_SUCCEEDED;
}
bool CheckRequest(MqlTradeRequest &request)
{
MqlTradeCheckResult check;
ZeroMemory(check);
bool ok = OrderCheck(request, check);
PrintFormat("OrderCheck ok=%s retcode=%u comment=%s",
ok ? "true" : "false", check.retcode, check.comment);
return ok;
}
The second request can still be rejected for another reason, for example a filling mode that the symbol does not support (see unsupported filling mode), a closed market or a disabled terminal. That is the point: the check names the real reason.
Checklist: What to Check When You See 10013
- Clear the structures with
ZeroMemorybefore you set any field. - Check the action and set every field that action needs.
- Check the ticket field:
positionfor positions,orderfor pending orders. - Check pending orders: the type, the price side,
type_timeandexpiration. - Select the symbol with
SymbolSelect. - Run
OrderCheckand print the return code and the comment. - Use
CTradewhen you can, because it sets most fields for you. - Test on a demo account before the EA trades with real money.
Common Mistakes
- Not clearing the request structure.
- Reusing a request for a different action without resetting the fields.
- Putting a position ticket in the order field or the other way round.
- Sending a pending order with a wrong expiration setting.
- Guessing the bad field instead of running
OrderCheck. - Ignoring the return code of
OrderSend.
FAQ
Related Guides
- MQL5 trade return codes: what each return code means and how to react.
- MQL5 invalid stops error 10016: the matching error for stop loss and take profit.
- MQL5 unsupported filling mode error: another order rejection with a clear fix.
- MQL5 lot size calculation: a volume that matches the symbol rules.
- MQL5 wrong parameters count error: a call that does not match its parameters.
- MQL5 filter positions by magic number: find the position you want to change.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
- MQL5 autotrading disabled error 10027: the terminal, EA or account switch that blocks trading.
- MQL5 invalid volume error 10014: a lot size that does not match the symbol rules.
- MQL5 invalid price error 10015: a price that is stale, on the wrong side or not rounded to the tick size.
- MQL5 market closed error 10018: an order sent outside the trading session.
- MQL5 not enough money error 10019: not enough free margin for the order.
- MQL5 request rejected error 10006: the server refused the request without naming a reason.
- MQL5 no changes error 10025: a modify that sends the same stop or take profit again.
- MQL5 too frequent requests error 10024: slow down, queue requests and back off.
- MQL5 OrderSend failed with error 4756: read the retcode behind 4756, then fix the request.
- MQL5 return value of OrderSend should be checked: store the result and read
result.retcode.
Fix Compile Errors Without Guessing
If an Expert Advisor does not compile, or you want a second look at its order and risk logic, paste the code into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. The Code Review is free during the early-bird period and checks an EA for common mistakes such as a fixed lot size or a missing stop loss. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.