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
- Round every computed price:
NormalizeDouble(price, _Digits). - For symbols with a tick size above the point, round to the tick first:
MathRound(price / tickSize) * tickSize, thenNormalizeDouble().
Code that fires it
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
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.
Related MQL5 errors
More rules on prices and stops
- 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.
- 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.
- 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.