EA Doctor rule · Prices and stops
Stops set without the stops level check
The 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.
Why it matters
Brokers set a minimum distance, in points, between the current price and a stop loss, a take profit or a pending order's price: SYMBOL_TRADE_STOPS_LEVEL. A level closer than that is refused with 10016 (invalid stops).
A tight stop often works in the tester and on one account, then fails on a symbol or broker with a larger level. A value of 0 means the symbol has no fixed minimum.
When the rule fires
The EA sets a stop loss or take profit (request.sl / request.tp, the stop arguments of CTrade Buy(), Sell(), PositionOpen() or the pending-order methods, PositionModify(), OrderModify()) and SYMBOL_TRADE_STOPS_LEVEL (or CSymbolInfo::StopsLevel()) appears nowhere in the file.
When it stays quiet
The stops level is read anywhere in the file, or the EA sends no stops (all stop arguments are 0).
How to fix it
- Read
SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL)and turn it into a price distance with_Point. - For a buy, measure from the Bid (the price that closes it); for a sell, from the Ask. Move a stop that is too close out to the minimum, or skip the trade if the strategy needs that exact level.
Code that fires it
void OpenBuy()
{
const double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
const double sl = NormalizeDouble(ask - 150 * _Point, _Digits);
const double tp = NormalizeDouble(ask + 300 * _Point, _Digits);
trade.SetExpertMagicNumber(InpMagic);
if(!trade.Buy(InpLots, _Symbol, ask, sl, tp))
Print("Buy failed: ", trade.ResultRetcodeDescription());
}Fixed code
void OpenBuy(const int slPoints, const int tpPoints)
{
const double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
const double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
const double dist = (SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) + 1) * _Point;
double sl = ask - slPoints * _Point;
double tp = ask + tpPoints * _Point;
if(bid - sl < dist)
sl = bid - dist; // a buy closes at the Bid: measure from there
if(tp - bid < dist)
tp = bid + dist;
sl = NormalizeDouble(sl, _Digits);
tp = NormalizeDouble(tp, _Digits);
trade.SetExpertMagicNumber(InpMagic);
if(!trade.Buy(InpLots, _Symbol, ask, sl, tp))
Print("Buy failed: ", trade.ResultRetcodeDescription());
}The fixed code shows this rule’s fix and compiles in MetaEditor for MetaTrader 5 (with CTrade trade, InpLots and InpMagic declared, as in a typical EA). Other rules may still apply to it as a fragment: check your whole EA.
Related MQL5 errors
More rules on prices and stops
- MediumChanges near the price without the freeze level checkThe EA modifies stops or deletes orders without reading the freeze level, inside which the broker refuses changes with 10029.
- 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
Check a whole EA for this and 29 other mistakes with EA Doctor, free and in your browser. Or send it to us: we confirm the price in writing, then fix it.