Skip to content

EA Doctor rule · Prices and stops

Prices not rounded to the symbol's digits

A price, stop loss or take profit is computed with _Point arithmetic and sent without NormalizeDouble() or rounding to the tick size.

Why it matters

Prices computed with _Point carry floating-point noise (1.0849999999 instead of 1.08500). The MQL5 reference says stop loss, take profit and pending order prices must be normalised to the symbol's digits with NormalizeDouble().

Symbols whose tick size is larger than the point (many indices and some metals) need a multiple of SYMBOL_TRADE_TICK_SIZE as well. Otherwise the server answers 10015 (invalid price) or 10016 (invalid stops).

When the rule fires

The EA trades, and a request's price, sl or tp, or a variable named like sl, tp, stop, take or price, is computed with _Point, Point() or SYMBOL_POINT and a plus or minus, without NormalizeDouble() around it or applied to that variable later, and the file has no rounding to SYMBOL_TRADE_TICK_SIZE.

When it stays quiet

NormalizeDouble() wraps the value or is applied to the variable later, or prices are rounded to SYMBOL_TRADE_TICK_SIZE with MathRound().

How to fix it

  1. Round every computed price: NormalizeDouble(price, _Digits).
  2. For symbols with a tick size above the point, round to the tick first: MathRound(price / tickSize) * tickSize, then NormalizeDouble().

Code that fires it

MQL5 · fires this rule
void OpenSell(const int slPoints)
  {
   const double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   const double sl  = bid + slPoints * _Point;
   trade.SetExpertMagicNumber(InpMagic);
   if(!trade.Sell(InpLots, _Symbol, bid, sl, 0.0))
      Print("Sell failed: ", trade.ResultRetcodeDescription());
  }

Fixed code

MQL5 · fixed
double RoundToTick(const double price)
  {
   const double tick = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   if(tick <= 0.0)
      return(NormalizeDouble(price, _Digits));
   return(NormalizeDouble(MathRound(price / tick) * tick, _Digits));
  }

void OpenSell(const int slPoints)
  {
   const double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   const double sl  = RoundToTick(bid + slPoints * _Point);
   trade.SetExpertMagicNumber(InpMagic);
   if(!trade.Sell(InpLots, _Symbol, bid, sl, 0.0))
      Print("Sell 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.