Trade server return code
10016 TRADE_ RETCODE_ INVALID_ STOPS
Invalid stops: SL or TP too close to the price or on the wrong side
What it means
The stop loss or take profit in the request is not allowed. A stop must be on the correct side of the price at which the position closes, and at least SYMBOL_TRADE_STOPS_LEVEL points away from it ("Minimal indention in points from the current close price to place Stop orders"). In the journal it usually appears as "OrderSend error 4756" followed by "retcode=10016".
Common causes
- A stop or target closer to the price than the stops level, for example a tight scalping stop on a symbol with a 30-point level.
- A stop on the wrong side: a buy's stop loss above the Bid, or a sell's take profit above the Ask.
- Distances in pips used as points (or the reverse) on 5-digit and 3-digit quotes.
- Stops measured from the open price instead of the price the position closes at (the Bid for a buy, the Ask for a sell).
A real scenario
A scalping EA ported from MQL4 sets a 2-pip stop loss on GBPUSD. On the new broker the stops level is 30 points (3 pips), so every entry comes back with 10016, which the MQL4 version had reported as error 130. The EA now reads the stops level and widens the stop to it, or skips the trade when the strategy cannot accept the wider stop.
How to fix it
- Read
SYMBOL_TRADE_STOPS_LEVELand the current tick before setting stops. - For a buy, compare the stop loss and take profit with the Bid; for a sell, with the Ask.
- A stops level of 0 means the broker sets no fixed minimum, but the stop must still be on the correct side.
- Normalise stops to the symbol's digits, and widen or skip rather than send a request you know will fail.
Fixed MQL5 code
bool StopsValid(const ENUM_ORDER_TYPE type, const double sl, const double tp)
{
MqlTick t;
if(!SymbolInfoTick(_Symbol, t))
return false;
double minDist = (double)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) * _Point;
if(type == ORDER_TYPE_BUY)
{
// a buy closes at the Bid: SL below it, TP above it, both at least the stops level away
if(sl > 0.0 && t.bid - sl < minDist) return false;
if(tp > 0.0 && tp - t.bid < minDist) return false;
}
else if(type == ORDER_TYPE_SELL)
{
// a sell closes at the Ask: SL above it, TP below it
if(sl > 0.0 && sl - t.ask < minDist) return false;
if(tp > 0.0 && t.ask - tp < minDist) return false;
}
return true;
}
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
- 10029Order or Position FrozenTRADE_RETCODE_FROZENOrder or position frozen: too close to its trigger to change
- 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
- MediumTrade request not cleared before useA local MqlTradeRequest is declared without an initializer and never cleared, so the fields the code does not set hold leftover values.
- HighStops set without the stops level checkThe EA sets a stop loss or take profit without reading the broker's minimum stop distance, which is refused with 10016 when it is too close.
- MediumPrices not rounded to the symbol's digitsA price, stop loss or take profit is computed with _Point arithmetic and sent without NormalizeDouble() or rounding to the tick size.
- MediumHard-coded point size or digitsThe code writes 0.0001 for a pip or rounds prices to a fixed number of digits, which is wrong on JPY pairs, gold, indices and crypto.
Sources
Get your EA fixed
Still failing with 10016? 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.