Skip to content

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

  1. Read SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) and turn it into a price distance with _Point.
  2. 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

MQL5 · fires this rule
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

MQL5 · fixed
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.

More rules on prices and stops

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.