Skip to content

EA Doctor rule · Environment and lifecycle

Chart objects created but never removed

The EA draws chart objects but never deletes them, so lines and labels stay on the chart after it is removed.

Why it matters

Objects an EA creates with ObjectCreate() (lines, labels, arrows, buttons) belong to the chart, not to the EA. Unless the EA deletes them in OnDeinit(), they stay after it is removed.

Left behind, they mislead the trader (a stop line for a position that no longer exists) and pile up with every restart.

When the rule fires

ObjectCreate() is called and the file never calls ObjectDelete() or ObjectsDeleteAll().

When it stays quiet

Either delete function is called anywhere in the file. A drawing tool whose objects should stay can ignore this rule.

How to fix it

  1. Give the EA's objects a common name prefix.
  2. In OnDeinit(), remove them with ObjectsDeleteAll(0, prefix).

Code that fires it

MQL5 · fires this rule
void DrawLevel(const double price)
  {
   const string name = "Level";
   if(ObjectFind(0, name) < 0)
      ObjectCreate(0, name, OBJ_HLINE, 0, 0, price);
   else
      ObjectSetDouble(0, name, OBJPROP_PRICE, price);
  }

Fixed code

MQL5 · fixed
const string PREFIX = "AQ_Level_";

void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0, PREFIX);
  }

void DrawLevel(const double price)
  {
   const string name = PREFIX + "entry";
   if(ObjectFind(0, name) < 0)
      ObjectCreate(0, name, OBJ_HLINE, 0, 0, price);
   else
      ObjectSetDouble(0, name, OBJPROP_PRICE, price);
  }

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 environment and lifecycle

  • LowAlgo trading permission not checkedThe EA sends orders without checking that algo trading is allowed in the terminal, for the EA and on the account, so every order fails when one is off.
  • LowTimer started but never stoppedThe EA starts a timer with EventSetTimer() but never calls EventKillTimer(), which the MQL5 reference pairs with it in OnDeinit().

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.