DEV Community

Omais Ali
Omais Ali

Posted on

A daily log file for your MQL5 EA that you can actually read after a bad day

When an Expert Advisor does something strange at 3 am, the Experts tab is rarely enough. It mixes every EA on the terminal, it is cleared on restart, and the journal files are hard to filter. A small log of your own, one file per day per EA instance, makes post mortems much faster.

What to log

  1. Every input value at OnInit, so you know exactly which settings were running.
  2. Every trade request you send and the full result: retcode, deal ticket, price, volume and the comment from the server.
  3. Every signal you skipped, with the reason, for example spread too wide or daily loss limit reached.
  4. Every error from functions such as CopyBuffer or OrderCalcMargin.

The skipped signals are the most useful part. Most questions after a quiet day are "why did it not trade", and the answer is sitting in that file.

The code

input long InpMagic = 260514;

int    g_log     = INVALID_HANDLE;
string g_logDate = "";

void LogLine(const string text)
{
   string day = TimeToString(TimeCurrent(), TIME_DATE);
   if(day != g_logDate)
   {
      if(g_log != INVALID_HANDLE)
         FileClose(g_log);
      StringReplace(day, ".", "");
      string name = "ea_logs\\" + _Symbol + "_" + IntegerToString(InpMagic) + "_" + day + ".log";
      g_log = FileOpen(name, FILE_READ|FILE_WRITE|FILE_TXT|FILE_ANSI|FILE_SHARE_READ);
      if(g_log == INVALID_HANDLE)
      {
         Print("log open failed, error ", GetLastError());
         return;
      }
      FileSeek(g_log, 0, SEEK_END);
      g_logDate = TimeToString(TimeCurrent(), TIME_DATE);
   }
   string stamp = TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS);
   FileWriteString(g_log, stamp + " " + text + "\r\n");
   FileFlush(g_log);
}

void OnDeinit(const int reason)
{
   LogLine("deinit reason " + IntegerToString(reason));
   if(g_log != INVALID_HANDLE)
      FileClose(g_log);
}
Enter fullscreen mode Exit fullscreen mode

A few details worth knowing:

  1. FILE_READ together with FILE_WRITE opens an existing file without truncating it, and FileSeek to SEEK_END appends. Without that, a restart in the middle of the day would wipe the morning.
  2. FILE_SHARE_READ lets you open the file in a text viewer while the EA is still running.
  3. The file name includes the symbol and magic number, so two instances on different charts never write to the same file.
  4. Files land in MQL5\Files\ea_logs of the terminal data folder. In the Strategy Tester they go to the tester agent folder instead, which is handy because test logs never mix with live ones.
  5. FileFlush on every line costs a little disk activity. For an EA that logs a few lines per bar that is nothing, and it means the last line is on disk even if the terminal crashes.

Logging a trade result

MqlTradeRequest req = {};
MqlTradeResult  res = {};
// fill req here
bool sent = OrderSend(req, res);
LogLine(StringFormat("send type=%d vol=%.2f price=%.5f sent=%d retcode=%u deal=%I64u comment=%s",
        req.type, req.volume, req.price, sent, res.retcode, res.deal, res.comment));
Enter fullscreen mode Exit fullscreen mode

Keep the format boring and consistent, one event per line. Then a quick search for retcode or skip tells you the story of the day in seconds.

Top comments (0)