Quick Answer
Two return codes say that the terminal could not do the job because the data or the link was missing, not because the order was wrong:
- 10021 (
TRADE_RETCODE_PRICE_OFF): "There are no quotes to process the request". The server has no price for the symbol at this moment. - 10031 (
TRADE_RETCODE_CONNECTION): "No connection with the trade server". The terminal is not connected, or the link dropped while the request was sent.
Both can pass on their own, so a short wait and a limited retry is the right reaction, not a change of the request. What helps is to check the state before you send: is the terminal connected, and is there a fresh tick for the symbol? The sections below show the two checks, a retry rule, and an EA that prints the decision for a set of example states, without sending anything.
From the author: I build and convert Expert Advisors. In a converted EA, the order logic is often copied from a strategy that never had a missing price or a dropped link, so nothing handles them. Then the EA retries in a tight loop, or it trusts a price that is minutes old. The fix is a few lines of state checks.
Key Facts
| Code | Name | What it means | First thing to check |
|---|---|---|---|
| 10021 | TRADE_RETCODE_PRICE_OFF | No quotes to process the request | Is the symbol quoted now? Session, Market Watch, a fresh tick |
| 10031 | TRADE_RETCODE_CONNECTION | No connection with the trade server | The connection indicator, the network, the server |
The names and descriptions are from the MQL5 documentation. The advice below is practical, not part of the documentation.
What the Error Looks Like
With CTrade, a refused request makes the call return false, and the code is in the result:
if(!trade.Buy(lots, _Symbol, 0.0, sl, tp, "bot"))
{
PrintFormat("Order failed. Retcode %u: %s",
trade.ResultRetcode(), trade.ResultRetcodeDescription());
}
With OrderSend, read MqlTradeResult.retcode. The full list of codes is in MQL5 trade return codes.
Cause 1 (10021): The Symbol Has No Price Right Now
Typical situations:
- The market is closed or the symbol is in a break. The matching code for a closed session is 10018, and the same wait applies.
- The symbol is not in Market Watch, or it has never received a tick in this session, so the terminal has no quote.
- The EA starts and sends at once, before the first tick arrived.
- A thin symbol that has no quote for a while.
- A request with a price that is not a quote of the symbol (invalid price, 10015 is the code for a bad price level, a different problem).
Check the tick before you send. SymbolInfoTick fills a structure and returns false when it has no data:
bool HasFreshQuote(const string symbol, int maxAgeSeconds)
{
MqlTick tick;
if(!SymbolInfoTick(symbol, tick))
return false; // no data at all
if(tick.bid <= 0.0 || tick.ask <= 0.0)
return false; // empty quote
return (TimeCurrent() - tick.time) <= maxAgeSeconds;
}
TimeCurrent() is the time of the last server tick, not a clock, so on a quiet symbol a small maxAgeSeconds can reject a valid but old quote. Choose a value that fits the symbol, and print it when you test. In the Strategy Tester the tick time follows the test, so test the logic with the same function.
Cause 2 (10031): The Terminal Is Not Connected
The terminal shows the connection state in the lower right corner of the window. In code, ask the terminal:
bool IsConnectedNow()
{
return (bool)TerminalInfoInteger(TERMINAL_CONNECTED);
}
Also check that trading is allowed at all (see autotrading disabled, 10027): a terminal that is connected can still have trading switched off, and the code for that is different.
Common causes of a lost link: a network change, a VPN, a server restart or maintenance, a laptop that went to sleep, or a VPS with a poor route. The EA cannot repair the link, so it should wait and re-check, not send in a loop.
A Wait and a Limited Retry
The same idea as for other temporary errors: wait for a time stamp, make the wait longer each time, and stop after a few attempts:
datetime g_nextTry = 0;
int g_failCount = 0;
const int MAX_TRIES = 4;
bool MayTryNow()
{
return g_failCount < MAX_TRIES && TimeCurrent() >= g_nextTry;
}
void OnTemporaryFailure()
{
g_failCount++;
int wait = (g_failCount == 1) ? 5 : (g_failCount == 2 ? 30 : 120);
g_nextTry = TimeCurrent() + wait;
}
void OnSuccess()
{
g_failCount = 0;
g_nextTry = 0;
}
One thing to decide on purpose: should the EA still send the order after a long wait? A signal that was valid a few seconds ago may not be valid after two minutes. After a wait, recompute the signal and the price, and send only if the signal still holds. A stale order sent late is worse than a missed one.
Also keep the pace of retries under control, because a tight loop of requests brings its own refusal: too frequent requests (10024). Never use Sleep() in a loop to wait for a link, because it blocks the EA and its event handling.
A Decision Helper
One function that turns the state into a decision keeps the rules in one place:
enum SendDecision
{
SEND_NOW,
WAIT_NO_CONNECTION,
WAIT_NO_QUOTE,
GIVE_UP
};
SendDecision Decide(bool connected, bool freshQuote, int failCount, int maxTries)
{
if(failCount >= maxTries) return GIVE_UP;
if(!connected) return WAIT_NO_CONNECTION;
if(!freshQuote) return WAIT_NO_QUOTE;
return SEND_NOW;
}
A Full EA That Prints the Decisions
This EA does not place trades and does not read the terminal. It feeds example states into the decision helper and prints the result. Run it on a demo account to check the logic:
#property strict
enum SendDecision
{
SEND_NOW,
WAIT_NO_CONNECTION,
WAIT_NO_QUOTE,
GIVE_UP
};
SendDecision Decide(bool connected, bool freshQuote, int failCount, int maxTries)
{
if(failCount >= maxTries) return GIVE_UP;
if(!connected) return WAIT_NO_CONNECTION;
if(!freshQuote) return WAIT_NO_QUOTE;
return SEND_NOW;
}
string Name(SendDecision d)
{
switch(d)
{
case SEND_NOW: return "send now";
case WAIT_NO_CONNECTION: return "wait: no connection";
case WAIT_NO_QUOTE: return "wait: no fresh quote";
default: return "give up";
}
}
int OnInit()
{
// connected, freshQuote, failCount
PrintFormat("1: %s", Name(Decide(true, true, 0, 4)));
PrintFormat("2: %s", Name(Decide(false, true, 1, 4)));
PrintFormat("3: %s", Name(Decide(true, false, 1, 4)));
PrintFormat("4: %s", Name(Decide(false, false, 2, 4)));
PrintFormat("5: %s", Name(Decide(true, true, 4, 4)));
return INIT_SUCCEEDED;
}
The output should be: send now, wait: no connection, wait: no fresh quote, wait: no connection, give up. Note line 4: with both problems, the connection is reported first, because without a link a quote cannot be trusted.
Where to Put the Checks
- Before a new order: check the connection and the quote. If either fails, skip this bar and look again on the next one.
- Before a modify or a close: check the same, but do not skip silently. A stop loss that cannot be moved, or a position that cannot be closed, deserves a log line and a retry soon, because it protects the account.
- After a failed send: if the code is 10021 or 10031, count the failure and wait. For other codes, follow the return codes guide.
Similar Return Codes
| Code | Name | What it means |
|---|---|---|
| 10021 | TRADE_RETCODE_PRICE_OFF | No quotes to process the request |
| 10031 | TRADE_RETCODE_CONNECTION | No connection with the trade server |
| 10018 | TRADE_RETCODE_MARKET_CLOSED | The market is closed |
| 10027 | TRADE_RETCODE_CLIENT_DISABLES_AT | Autotrading is disabled by the client terminal |
| 10024 | TRADE_RETCODE_TOO_MANY_REQUESTS | Requests are sent too often |
| 10015 | TRADE_RETCODE_INVALID_PRICE | The price in the request is not valid |
Checklist: What to Check When You See 10021 or 10031
- Read the code and the time of the failure in the log.
- Check the connection in the terminal and with
TERMINAL_CONNECTED. - Check the symbol: in Market Watch, in session, with a fresh tick.
- Wait a few seconds, then try again, with a longer wait each time.
- Stop after a few attempts and write a clear log line.
- Recompute the signal after a long wait, and do not send a stale order.
- Protect open positions first when the link comes back.
- Test on a demo account before the EA trades with real money.
Common Mistakes
- Retrying in a tight loop while the link is down.
- Using
Sleep()to wait for the connection. - Sending a stale signal after a long wait.
- Trusting a price without checking that the tick is recent.
- Starting to trade in
OnInitbefore the first tick. - Treating 10021 like a bad price and changing the request.
FAQ
Related Guides
- MQL5 trade return codes: what each return code means and how to react.
- MQL5 market closed error 10018: an order sent outside the trading session.
- MQL5 invalid price error 10015: a price that is stale, on the wrong side or not rounded to the tick size.
- MQL5 autotrading disabled error 10027: the terminal, EA or account switch that blocks trading.
- MQL5 invalid request error 10013: a request with a missing or wrong field.
- MQL5 invalid stops error 10016: the matching error for stop loss and take profit.
- MQL5 not enough money error 10019: the margin side of a refused order.
- MT5 every tick vs real ticks: how the tester feeds prices to an EA.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
- MQL5 request rejected error 10006: the server refused the request without naming a reason.
- MQL5 too frequent requests error 10024: slow down, queue requests and back off.
- MQL5 no changes error 10025: a modify that sends the same stop or take profit again.
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.