Trade server return code
10020 TRADE_ RETCODE_ PRICE_ CHANGED
Prices changed while the request was processed
What it means
The price moved between the moment the EA read it and the moment the server processed the request. Like a requote (10004) it is mostly a matter of execution mode, speed and the accepted deviation, and the cure is the same: a fresh price and a limited retry.
Common causes
- A price read once at the start of
OnTick()and used after slow work (indicator reads, file access, loops over history). - A deviation that is too small for the symbol in fast conditions.
- Several requests sent back to back with one price.
A real scenario
A basket EA closes ten positions one after another with the Bid it read before the loop. By the fifth close the price has moved, and the rest fail with 10020, leaving half the basket open. Reading the tick inside the loop for every close fixed it.
How to fix it
- Read the tick just before each request, inside loops too.
- Retry only on 10020 and 10004, a few times, each time with a fresh price.
- Keep heavy work out of the path between reading the price and sending the order.
Fixed MQL5 code
bool SendWithFreshPrice(MqlTradeRequest &req, const int maxAttempts = 3)
{
for(int i = 0; i < maxAttempts; i++)
{
MqlTick t;
if(!SymbolInfoTick(req.symbol, t))
return false;
if(req.action == TRADE_ACTION_DEAL)
req.price = (req.type == ORDER_TYPE_BUY) ? t.ask : t.bid; // re-read the price on every attempt
MqlTradeResult res;
ZeroMemory(res);
bool sent = OrderSend(req, res);
if(sent && res.retcode == TRADE_RETCODE_DONE)
return true;
if(res.retcode != TRADE_RETCODE_PRICE_CHANGED && res.retcode != TRADE_RETCODE_REQUOTE)
{
PrintFormat("OrderSend: retcode %u, %s", res.retcode, res.comment);
return false;
}
}
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
- 10004RequoteTRADE_RETCODE_REQUOTERequote: the price moved before the order was filled
- 10021Off QuotesTRADE_RETCODE_PRICE_OFFOff quotes: no current price for the symbol
EA Doctor rules that catch it
- 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 10020? 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.