Overview

When you develop programs in MQL5, the log is your primary source of truth about what is actually happening inside the terminal. MetaTrader 5 records messages from your Expert Advisors, indicators, and scripts, along with system events and errors. These messages are shown in different tabs such as Experts, Journal, and, when testing, the Strategy Tester output. Learning how to read these logs will dramatically improve your ability to debug and refine your trading algorithms.

Ad

What You Will Be Able to Do

  • Locate and open the Experts and Journal tabs in MetaTrader 5.
  • Understand how Print() and other functions send messages to the log.
  • Interpret error messages and warnings that appear during execution.
  • Use log information to track program flow and diagnose problems.

Code Example


//+------------------------------------------------------------------+
//|                                                   LogDemo.mq5   |
//+------------------------------------------------------------------+
#property version "1.00"
#property strict

input double Lots = 0.10;

int OnInit()
  {
   Print("LogDemo initialized. Lots = ", DoubleToString(Lots, 2));
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   // Simple example: intentionally use an invalid symbol to trigger an error
   string badSymbol = "INVALID_SYMBOL";
   double price = SymbolInfoDouble(badSymbol, SYMBOL_BID);

   if(price == 0.0)
      Print("Failed to get price for symbol: ", badSymbol);

   // Normal message showing that OnTick is running
   Print("OnTick executed on ", _Symbol, " at ", TimeToString(TimeCurrent(), TIME_SECONDS));
  }

void OnDeinit(const int reason)
  {
   Print("LogDemo deinitialized. Reason = ", reason);
  }
Ad

Execution Steps

  1. Open MetaEditor and create a new Expert Advisor named LogDemo.
  2. Paste the sample code above and press F7 to compile.
  3. In MetaTrader 5, attach LogDemo to any chart and enable AutoTrading.
  4. At the bottom of the terminal, select the Experts tab to see messages from Print() and any runtime errors.
  5. Select the Journal tab to see terminal-level events such as connections, loading programs, and trade operations.
  6. If you run the EA in the Strategy Tester, review the output in the Strategy Tester (or Tester) log as well.

Key Point

Effective debugging in MQL5 starts with the log. By combining meaningful Print() messages with careful reading of the Experts and Journal tabs, you can understand the exact sequence of events, detect errors early, and verify that your trading logic behaves as expected.

Ad

Next Section

→ Next: Summary and Next Steps