Master MetaTrader Debugging: Step Into MQL4/MQL5 Code Like

Learn to use the MetaEditor debugger to step through MQL4/MQL5 code, set breakpoints, inspect variables, and fix logic errors in EAs and indicators.

master-metatrader-debugging-step-into-mql4-mql5

Why Debugging MQL Code Matters More Than You Think

Every MQL developer eventually hits a wall: the EA opens trades at the wrong price, the indicator line doesn’t appear, or the exit condition never triggers. You stare at the code, re-read it five times, and still can’t spot the bug. The MetaEditor debugger is the tool that turns guesswork into certainty. Instead of adding Print() statements everywhere and recompiling, you can pause execution mid-tick, inspect every variable, and watch exactly how your logic executes line by line. This guide walks you through the exact steps to set up and use the debugger in MetaTrader 4 and MetaTrader 5, covering both the built-in Strategy Tester debug mode and the newer visual debugger in MT5. By the end, you’ll fix bugs in minutes instead of hours.

Prerequisites

Before you start debugging, confirm you have:

  • MetaEditor installed (comes with MT4 and MT5 terminals — open it via F4 or Tools → MetaQuotes Language Editor).
  • An MQL4 or MQL5 project (EA, indicator, or script) that compiles without syntax errors. The debugger catches run-time logic issues, not compile errors.
  • A demo account or free demo connection. The debugger uses live market data or history data from the terminal.
  • MT5 build 2000+ for the visual debugger (optional but recommended). MT4 uses the Strategy Tester debug mode only.

Step-by-Step Debugging in MetaEditor

1. Open Your Project and Set Breakpoints

In MetaEditor, open the MQL file you want to debug. Find the line where you suspect the logic goes wrong — for example, inside OnTick() or OnCalculate(). Click in the left margin (the grey bar next to line numbers) to place a breakpoint. A red dot appears. You can set multiple breakpoints across different functions.

Tip: Place your first breakpoint on the first line of OnTick() (MT4) or OnTick()/OnCalculate() (MT5). This stops execution at the start of each new tick or bar.

2. Start Debugging in MT5 (Visual Debugger)

In MetaEditor, click Debug → Start Debugging or press F5. A dialog appears asking you to select a symbol, timeframe, and whether to use the Strategy Tester or live market data. For most debugging, choose Strategy Tester with a short date range and Every tick mode. Click OK. The terminal opens the Strategy Tester and starts running your EA. Execution pauses at your first breakpoint. The current line is highlighted in yellow.

3. Start Debugging in MT4 (Strategy Tester Mode)

MT4 does not have a visual debugger. Instead, use the Strategy Tester debug mode. In MetaEditor, click Debug → Start Debugging or press F5. The Strategy Tester opens with your EA selected. Set the symbol, timeframe, and date range. Enable Visual mode (check the box). Click Start. The tester runs in visual mode, and MetaEditor opens a Debug window showing variable values. Execution pauses at breakpoints, but you see the tester chart updating. Use F10 to step over lines, F11 to step into functions, and Shift+F11 to step out.

4. Control Execution Flow

Once paused, use these keyboard shortcuts to navigate:

Action Shortcut What It Does
Step Over F10 Executes the current line and moves to the next line in the same function. Does not enter called functions.
Step Into F11 Jumps into the function called on the current line, letting you debug its internals.
Step Out Shift+F11 Runs the rest of the current function and returns to the calling line.
Continue F5 Resumes execution until the next breakpoint or the end of the test.
Stop Debugging Shift+F5 Terminates the debug session immediately.

5. Inspect Variables and Expressions

While paused, hover your mouse over any variable name to see its current value in a tooltip. For a full view, open the Watch window: Debug → Watch or Ctrl+Alt+W. Right-click in the Watch window and select Add Watch to type an expression, like Bid - Ask or OrderSelect(0, SELECT_BY_POS). You can also view local variables in the Locals window (Ctrl+Alt+L). Use these to verify your conditions — for example, check if Close[1] is actually above your moving average value.

6. Debug Custom Indicators

For indicators, set a breakpoint inside OnCalculate() (MT5) or start() (MT4). In MT5, the debugger works the same as for EAs. In MT4, the Start Debugging command attaches to the Strategy Tester, but you must first attach the indicator to a chart in the tester’s visual mode. Alternatively, attach the indicator to a live chart in the terminal, then in MetaEditor click Debug → Attach to Process (MT5 only) — this lets you debug an indicator running on a live chart.

Tips and Best Practices from Experience

  • Use minimal test data. Debug with a short date range (e.g., 100 bars) and a single symbol. Long backtests slow debugging to a crawl.
  • Disable optimization. The debugger only works in single-pass mode. Uncheck “Optimization” in the Strategy Tester settings.
  • Set conditional breakpoints. In MetaEditor, right-click a breakpoint and choose Condition. Enter an expression like OrdersTotal() > 0 — the breakpoint only triggers when true. This saves time in loops.
  • Check the Experts tab. If the debugger doesn’t stop at your breakpoint, look at the Experts tab in the terminal. Errors like “array out of range” appear there, often before the breakpoint is hit.
  • Use Print() as a fallback. For MT4 users who find the Strategy Tester debug mode clunky, strategic Print() calls with timestamps still work. But the debugger is faster once you get comfortable.
  • Keep your MQL code modular. Break large functions into smaller ones. This makes stepping into specific logic cleaner and reduces the chance of side-effect bugs.

Common Mistakes and Troubleshooting

Mistake 1: Debugger Doesn’t Stop at Breakpoints

Cause: The code you’re debugging is not the compiled version. MetaEditor compiles on start, but if you changed code after starting, you need to stop and restart. Also, ensure the breakpoint is on an executable line (not a comment, declaration, or empty line).

Fix: Stop debugging (Shift+F5), recompile (F7), then start again. Verify the red dot is solid, not hollow — a hollow dot means the breakpoint is not valid.

Mistake 2: “Unable to Start Debugging” Error in MT4

Cause: MT4 requires the Strategy Tester to be closed before starting a debug session. If the tester is already running, MetaEditor cannot attach.

Fix: Close the Strategy Tester window in MT4 completely. Then press F5 in MetaEditor again.

Mistake 3: Variable Values Show as “???” or “0”

Cause: You’re inspecting a variable that hasn’t been initialized yet, or you’re looking at a local variable from a different scope (e.g., inside a different function call).

Fix: Step forward a few lines (F10) until the variable is assigned. For global variables, check the Watch window after the first tick executes.

Mistake 4: Debugging an EA That Uses Pending Orders

Cause: The debugger pauses on each tick, but pending order placement and modification happen asynchronously in MT4. You may see OrderSend() return without the order appearing immediately.

Fix: Use Step Over (F10) after OrderSend() and then check OrdersTotal() on the next tick. In MT5, order operations are synchronous, so this is less of an issue.

Summary

Debugging is the single most effective skill for writing reliable MQL4 and MQL5 code. You now know how to set breakpoints, step through execution, inspect variables, and handle common pitfalls. Start with a small test script — for example, an EA that only prints Bid — and practice stepping through it. Once you’re comfortable, apply these techniques to your real projects. You’ll catch logic errors that Print() statements never reveal, and your coding speed will improve dramatically.

Next steps: Try debugging an indicator that calculates a moving average. Set a breakpoint inside the loop and watch how iMA() values change bar by bar. Then move on to debugging an EA with multiple entry conditions — the debugger will show you exactly which condition fails and why.

Frequently Asked Questions

Can I debug an EA running on a live chart in MT4?

No, MT4 only supports debugging through the Strategy Tester. For live debugging, use MT5’s “Attach to Process” feature (Debug → Attach to Process) while the EA runs on a chart in the terminal.

Why does my breakpoint turn hollow after I start debugging?

A hollow breakpoint means the code at that line is not compiled into the current executable. Recompile your project (F7) and restart the debug session. Also ensure the file you are debugging is the active tab in MetaEditor.

How do I debug an indicator that uses multiple buffers?

Set breakpoints inside OnCalculate() (MT5) or start() (MT4). In the Watch window, add expressions like CopyBuffer(handle, 0, 0, 1, buffer) to inspect buffer values at each bar. Step through the loop to see how each buffer index is populated.

Can I debug MQL5 code without the Strategy Tester?

Yes. In MetaEditor, go to Debug → Start Debugging and choose “Real ticks” or “Live market” as the data source. This attaches the debugger to a virtual chart that uses real-time price data from your terminal connection.

Community

Clap for the article and open comments only when you want to read them.

0 claps0 comments

Related articles