Skip to content

EA Doctor rule · Prices and stops

Changes near the price without the freeze level check

The EA modifies stops or deletes orders without reading the freeze level, inside which the broker refuses changes with 10029.

Why it matters

Close to the price that would trigger them, the broker can freeze orders and positions: within SYMBOL_TRADE_FREEZE_LEVEL points, a pending order cannot be modified or deleted and a position's stop loss and take profit cannot be changed. The server answers 10029 (frozen).

Trailing stops and order managers that move levels near the market meet it first, often in fast markets when the level is raised.

When the rule fires

The EA modifies or deletes (PositionModify(), OrderModify(), OrderDelete(), TRADE_ACTION_SLTP, TRADE_ACTION_MODIFY, TRADE_ACTION_REMOVE) and SYMBOL_TRADE_FREEZE_LEVEL (or CSymbolInfo::FreezeLevel()) appears nowhere in the file.

When it stays quiet

The freeze level is read anywhere in the file, or the EA never modifies or deletes.

How to fix it

  1. Read SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL) next to the stops level.
  2. Before a change, check that both the current level and the new one are farther from the price than the larger of the two; otherwise wait for the next tick.

Code that fires it

MQL5 · fires this rule
void Trail(const ulong ticket, const int trailPoints)
  {
   if(!PositionSelectByTicket(ticket))
      return;
   const double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   const double sl  = NormalizeDouble(bid - trailPoints * _Point, _Digits);
   if(sl > PositionGetDouble(POSITION_SL))
      if(!trade.PositionModify(ticket, sl, PositionGetDouble(POSITION_TP)))
         Print("Trail failed: ", trade.ResultRetcodeDescription());
  }

Fixed code

MQL5 · fixed
bool FarEnough(const double level, const double price)
  {
   const long stops  = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
   const long freeze = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL);
   return(MathAbs(price - level) > MathMax(stops, freeze) * _Point);
  }

void Trail(const ulong ticket, const int trailPoints)
  {
   if(!PositionSelectByTicket(ticket))
      return;
   const double bid   = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   const double curSl = PositionGetDouble(POSITION_SL);
   const double sl    = NormalizeDouble(bid - trailPoints * _Point, _Digits);
   if(sl <= curSl)
      return;
   // the current stop and the new one must both be outside the freeze and stops levels
   if(!FarEnough(sl, bid) || (curSl > 0.0 && !FarEnough(curSl, bid)))
      return;
   if(!trade.PositionModify(ticket, sl, PositionGetDouble(POSITION_TP)))
      Print("Trail 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.