Overview

The Print() function is the simplest way to observe what your MQL5 program is doing. It writes messages to the terminal logs so you can confirm variable values, execution order, and error conditions. Outputs appear in the Experts tab (for Scripts/EAs/Indicators) and also in the terminal’s log files. Using Print() early and often is the fastest path to reliable debugging.

Ad

What You Will Be Able to Do

  • Display messages and variable values using Print().
  • Format numbers, times, and strings for clear log output.
  • Differentiate normal runtime messages from warnings or errors.
  • Trace program flow in the Experts and Journal tabs.

Code Example


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

void OnStart()
  {
   // 1) Plain text
   Print("Starting PrintExample");

   // 2) Variables
   string sym = Symbol();
   double price = SymbolInfoDouble(sym, SYMBOL_BID);
   int count = 3;
   Print("Symbol=", sym, "  Bid=", DoubleToString(price, _Digits), "  Count=", count);

   // 3) Concatenation vs multiple args
   Print("Concat: " + sym + " price=" + DoubleToString(price, _Digits));
   Print("Multi-args: ", sym, " price=", DoubleToString(price, _Digits));

   // 4) Time formatting
   datetime now = TimeCurrent();
   Print("Server time: ", TimeToString(now, TIME_DATE|TIME_SECONDS));

   // 5) Simple calculation and formatting
   double lots = 0.10;
   double step = 0.01;
   double next_lots = NormalizeDouble(lots + step, 2);
   Print(StringFormat("Lots %.2f -> %.2f (step=%.2f)", lots, next_lots, step));

   // 6) Guarded access example
   double val[];
   if(CopyClose(sym, PERIOD_M1, 0, 5, val) == 5)
      Print("Latest M1 close[0] = ", DoubleToString(val[0], _Digits));
   else
      Print("Warning: CopyClose failed for ", sym);

   Print("PrintExample finished");
  }
Ad

Execution Steps

  1. Open MetaEditorFile → New → Script and name it PrintExample.
  2. Paste the code above and press F7 to compile.
  3. In MetaTrader 5, drag PrintExample onto any chart.
  4. Open the Experts tab to confirm each line appears in order. Use the Journal tab for terminal-wide messages.
  5. If you do not see expected values, check the symbol and connection status, then re-run.

Key Point

Print() is your primary tracing tool. Log the inputs, intermediate values, and branch decisions that matter. Keep messages short, consistent, and searchable to speed up debugging.

Ad

Next Section

→ Next: Getting the Symbol Name