DEV Community

Omais Ali
Omais Ali

Posted on

Drawing an MQL5 EA's entry, stop and target on the chart for visual debugging

The fastest way I know to find logic bugs in an Expert Advisor is to make it draw what it intends to do. A journal line saying "stop at 1.08412" is easy to skim past. A red line sitting on the wrong side of price is impossible to miss, especially in the visual tester.

Here is a small helper that draws the planned entry, stop and target as horizontal segments starting at the signal bar, plus a short label with the reason for the trade.

The helper

void DrawLevel(const string name, datetime t1, double price, color clr)
{
   datetime t2 = t1 + PeriodSeconds() * 20;   // segment 20 bars long
   if(ObjectFind(0, name) < 0)
      ObjectCreate(0, name, OBJ_TREND, 0, t1, price, t2, price);
   else
   {
      ObjectMove(0, name, 0, t1, price);
      ObjectMove(0, name, 1, t2, price);
   }
   ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
}

void DrawTradePlan(ulong id, double entry, double sl, double tp, string reason)
{
   datetime t = iTime(_Symbol, _Period, 0);
   string base = "plan_" + (string)id + "_";
   DrawLevel(base + "entry", t, entry, clrDodgerBlue);
   DrawLevel(base + "sl",    t, sl,    clrRed);
   DrawLevel(base + "tp",    t, tp,    clrLimeGreen);

   string lbl = base + "why";
   if(ObjectFind(0, lbl) < 0)
      ObjectCreate(0, lbl, OBJ_TEXT, 0, t, entry);
   ObjectSetString(0, lbl, OBJPROP_TEXT, reason);
   ObjectSetInteger(0, lbl, OBJPROP_COLOR, clrSilver);
   ObjectSetInteger(0, lbl, OBJPROP_FONTSIZE, 8);
}
Enter fullscreen mode Exit fullscreen mode

Call DrawTradePlan right before sending the order, with the same prices you put in the request and a short reason such as "MA cross, ATR 14 stop". Using a unique id, for example a counter or the signal bar time, keeps old plans on the chart so you can scroll back through them.

What it catches

In the visual tester these drawings expose bugs within minutes:

  1. Stops placed on the wrong side of entry after a buy and sell branch was copied and only half edited.
  2. Stop distance measured from the wrong price, which shows up as stops that sit consistently one spread too close.
  3. Targets that are unreachable because of a points versus pips mix up, which appear far off the screen.
  4. Signals firing on the wrong bar, visible because the segments start one bar late or early.

Keeping it out of production

Drawing hundreds of objects slows the terminal and clutters a live chart. I wrap the calls in an input switch and turn it on only for testing:

input bool InpDrawPlans = false;

if(InpDrawPlans)
   DrawTradePlan(signalId, entry, sl, tp, reason);
Enter fullscreen mode Exit fullscreen mode

And in OnDeinit, remove everything with the prefix:

ObjectsDeleteAll(0, "plan_");
Enter fullscreen mode Exit fullscreen mode

Why bother

Unit tests catch math errors, and logs catch runtime errors, but logic errors about where things are relative to price are visual by nature. Fifteen lines of drawing code turn the strategy tester into a debugger you can actually see.

Do you draw anything on the chart while testing your EAs?

Top comments (1)

Collapse
 
goodtogreatmind profile image
goodtogreatmind •

The wrong-side stop after a copied buy/sell branch is the one that bites most, and drawing it is exactly right: a red segment above entry on a buy is obvious in a way a journal line never is. Two things worth adding: derive the level prices from the same variables you pass to OrderSend, not recomputed values, or you'll draw a plan that differs from what was actually sent. And call ObjectSetInteger with OBJPROP_ZORDER so plans sit above candles. The points-versus-pips bug also shows up faster if you label the distance in points next to the reason string. For anyone who'd rather catch these before writing the object code, QuantForge Studio builds MT4/MT5 EAs visually and exports compilable MQL4/MQL5.