Trade server return code
10004 TRADE_ RETCODE_ REQUOTE
Requote: the price moved before the order was filled
What it means
The trade server could not fill the order at the price in the request and offered another one. It is mostly seen on symbols with Instant or Request execution (SYMBOL_TRADE_EXEMODE), where the request carries a price the dealer can refuse; Market and Exchange execution fill at the current market price instead.
Common causes
- The price in the request was read earlier in
OnTick()and is stale by the time the request reaches the server. - The accepted deviation (
deviation, in points) is 0 or too small for the symbol's normal movement. - A fast market: a news release or the session open moves the price faster than the round trip to the server.
- High latency between the terminal and the trade server (a distant VPS or a slow connection).
A real scenario
An EA trading XAUUSD on an Instant-execution account reads the Ask once at the start of OnTick(), runs its filters, then sends a buy with a deviation of 0 during a US CPI release. The server answers 10004; the EA retries at once with the same old price, so every retry is requoted until the move is over and the entry is missed.
How to fix it
- Read a fresh tick (
SymbolInfoTick) immediately before each attempt and use its Ask for buys and Bid for sells. - Set a deviation in points that fits the symbol (
CTrade::SetDeviationInPoints). - Retry a small, fixed number of times, and only for 10004 and 10020; any other code needs its own fix.
- Log the retcode and the result comment of every failed attempt.
Fixed MQL5 code
#include <Trade/Trade.mqh>
CTrade trade;
bool BuyWithRetries(const double lots, const int maxAttempts = 3)
{
trade.SetDeviationInPoints(30); // accepted slippage in points: tune it per symbol
for(int attempt = 1; attempt <= maxAttempts; attempt++)
{
MqlTick tick;
if(!SymbolInfoTick(_Symbol, tick)) // a fresh price for every attempt
return false;
bool sent = trade.Buy(lots, _Symbol, tick.ask, 0.0, 0.0);
uint rc = trade.ResultRetcode();
if(sent && (rc == TRADE_RETCODE_DONE || rc == TRADE_RETCODE_PLACED))
return true;
PrintFormat("Buy attempt %d: retcode %u (%s)", attempt, rc, trade.ResultRetcodeDescription());
if(rc != TRADE_RETCODE_REQUOTE && rc != TRADE_RETCODE_PRICE_CHANGED)
return false; // other codes need a different fix, not a retry
}
return false;
}
A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.
Related codes
- 10020Prices ChangedTRADE_RETCODE_PRICE_CHANGEDPrices changed while the request was processed
- 10015Invalid PriceTRADE_RETCODE_INVALID_PRICEInvalid price: the order price is not valid for the symbol
- 4756Trade Request Sending FailedERR_TRADE_SEND_FAILEDTrade request sending failed: read the retcode, not 4756
EA Doctor rules that catch it
- HighOrderSend() result not checkedOrderSend() is called as a plain statement, so a refused or requoted order goes unnoticed and the EA carries on as if it traded.
- HighCTrade call result not checkedA CTrade method such as Buy(), Sell() or PositionClose() is called as a plain statement, so its failure is never seen.
- LowSleep() inside OnTick()OnTick() pauses with Sleep(), which holds up every other event of the EA and leaves it working with old prices.
Sources
Get your EA fixed
Still failing with 10004? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.