Master MetaTrader 4/5 Debugging: Fix Common Platform Errors

A practical guide to diagnosing and resolving the most frequent MT4 and 5 errors—from compilation failures and runtime EA glitches to database corruption and.

master-metatrader-4-5-debugging-fix-common

Introduction

Every MetaTrader user—whether you are a retail trader running a single Expert Advisor or a developer maintaining a suite of custom indicators—will eventually face platform errors. A red "Error 138" in the Experts tab, a compile failure with an unhelpful line number, or a chart that freezes for no apparent reason can derail your trading day. Worse, silent errors like "OrderSend failed: 130" can cause missed trades without any visible alert.

This guide walks you through the most common MetaTrader 4 and 5 errors, their root causes, and the exact steps to fix them. You will learn how to read error logs, diagnose compilation problems in MetaEditor, resolve runtime EA failures, and prevent platform-level issues like database corruption and chart freezes. By the end, you will be able to debug with confidence and keep your trading environment stable.

Prerequisites

  • MetaTrader 4 or 5 platform installed (any build from 2020 or later). MT4 and MT5 share most debugging workflows, but I will note differences where they matter.
  • MetaEditor (comes with the platform). You will use it to compile and test MQL code.
  • A demo account for safe testing. Never debug runtime errors on a live account.
  • Access to the Experts tab and Journal tab in the terminal window (View → Terminal, then click the respective tab).
  • Basic familiarity with the MetaTrader interface: how to attach an EA to a chart, open MetaEditor, and navigate the file system.

Understanding MetaTrader Error Types

Before diving into fixes, it helps to classify errors into three categories:

  1. Compilation errors – occur in MetaEditor when you try to compile .mq4 or .mq5 files. They prevent the EA/indicator from loading.
  2. Runtime errors – occur while the EA or indicator is running on a chart. These appear in the Experts tab with a numeric code (e.g., 130, 138, 145).
  3. Platform errors – affect the terminal itself: crashes, freezes, "database corrupted" messages, or connection issues.

Each category requires a different debugging approach.

Step 1: Reading Error Logs Like a Pro

The Experts tab is your primary diagnostic tool. Every EA prints its errors there. To open it: View → Terminal (or Ctrl+T), then click the Experts tab. You will see lines like:

2025.03.15 14:22:33.123 MyEA EURUSD,H1: OrderSend failed: 130

The number after "failed:" is the error code. MetaTrader 4 and 5 use the same numeric codes for most trade-related errors (130 = invalid stops, 138 = requote, 145 = modification denied). To decode any code, open MetaEditor, go to Help → Search, and type "error codes" – or simply keep a browser tab open to MetaQuotes' official error list.

Pro tip: Right-click inside the Experts tab and select "Open" to view the full log file (experts.log) in Notepad. This file contains timestamps and can help you spot patterns—like errors happening only during high volatility.

Step 2: Fixing Compilation Errors in MetaEditor

When you press F7 in MetaEditor and see red text, do not panic. The compiler tells you exactly what is wrong. Here is how to interpret the most common messages:

2.1 "undeclared identifier"

You used a variable or function name that the compiler does not recognize. Check for typos (e.g., Close vs Close[1]). In MQL4, many built-in variables like Bid and Ask are case-sensitive. In MQL5, you must use SymbolInfoDouble(_Symbol, SYMBOL_BID) instead.

2.2 "expression has no effect"

You wrote a statement that does nothing—like a comparison without assignment: if (a == b); (note the stray semicolon). Remove the semicolon after the condition.

2.3 "cannot convert from 'string' to 'int'"

You are passing a string where a number is expected. For example, OrderSend(_Symbol, OP_BUY, 0.1, Ask, 3, "stop", "take") is wrong because the stop loss and take profit parameters must be prices (doubles), not strings.

2.4 Functions missing in MQL5 that exist in MQL4

If you are migrating code from MT4 to MT5, remember that MQL5 does not have OrderSend(), OrderSelect(), or OrderClose(). Instead, it uses PositionOpen(), PositionSelect(), and PositionClose(). The error "function not defined" often means you are using MT4-style trade functions in an MT5 script.

2.5 Fix procedure

  1. Double-click the error line in the bottom pane of MetaEditor. It jumps to the offending code.
  2. Read the error description (hover over the red squiggly line or check the "Errors" tab).
  3. Make the fix, then press F7 again.
  4. Repeat until you get "0 error(s), 0 warning(s)".

Step 3: Diagnosing Runtime EA Errors

Once your EA compiles, it can still fail at runtime. The Experts tab will show numeric codes. Here are the top five runtime errors and their fixes:

Error Code Meaning Common Cause Fix
130 Invalid stops Stop loss or take profit is too close to current price or on the wrong side. Check broker's minimum stop distance (use MarketInfo(Symbol(), MODE_STOPLEVEL) in MT4 or SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) in MT5). Add the stop level to your stop loss calculation.
138 Requote Market price changed between sending the order and execution. Add a retry loop with a small delay (Sleep(100)) and re-request the current price before resending. In MT5, use ORDER_FILLING_IOC to avoid requotes.
145 Modification denied Trying to modify a pending order that is too close to market or already executed. Check OrderSelect() returns true before modifying. Ensure the new stop level respects the minimum distance. Do not modify filled orders.
4108 Unknown symbol (MT5 only) Symbol not available in Market Watch or not selected. Use SymbolSelect(_Symbol, true) in OnInit() to ensure the symbol is visible. Check the symbol name spelling (e.g., "EURUSD" vs "EURUSDm").
4756 Disabled algorithm (MT5 only) Algo trading is disabled in platform settings. Go to Tools → Options → Expert Advisors and check "Allow Automated Trading". Also click the "Algo Trading" button on the toolbar (it should be green).

Step 4: Debugging Platform Crashes and Freezes

4.1 "Database corrupted" or "Cannot open database"

This happens when the terminal's history database (tick and bar data) becomes inconsistent, often due to a forced shutdown or a full hard drive. Fix: Close MetaTrader, navigate to your data folder (File → Open Data Folder), go to the history or bases subfolder, and delete the files for the affected symbol (e.g., EURUSD.hst for MT4 or EURUSD.sqlite for MT5). Restart the platform – it will redownload the data from the broker.

4.2 Chart freezes after attaching an EA

Your EA likely has an infinite loop or a blocking operation. In MQL4, while(true) without a Sleep() hangs the chart. In MQL5, a long-running OnTick() that does not finish before the next tick arrives can cause the chart to become unresponsive. Fix: Remove the EA from the chart (right-click chart → Expert Advisors → Remove). Review your code for loops without breaks. Add Sleep(10) inside while loops in MQL4 (note: Sleep is forbidden in MT5 indicators – use ChartRedraw() instead).

4.3 "No connection" with correct login

If you can log in but the terminal shows "No connection" in the bottom-right corner, the issue is usually a firewall or DNS. Fix: Temporarily disable Windows Firewall to test. If that works, add an exception for MetaTrader. Alternatively, flush DNS: open Command Prompt as administrator and type ipconfig /flushdns. Restart the terminal.

Tips and Best Practices from Experience

  • Enable "Allow DLL imports" with caution. Many runtime errors (especially "cannot call function") stem from missing DLL dependencies. Only enable this for EAs that genuinely need external libraries. Never enable it for untrusted code.
  • Use Print() strategically. When debugging a runtime error, add Print("Variable X = ", X) before the line that fails. This lets you see the exact value that causes the error. Remove these prints before going live.
  • Keep a "debug" version of your EA. Maintain a separate .mq4 file with verbose logging (write to a file using FileOpen()). On live accounts, use the release version with minimal logging to avoid slowing down execution.
  • Test on multiple brokers. Some errors (like 130) are broker-specific because stop levels vary. Run your EA on at least two demo accounts from different brokers before going live.
  • Update your platform regularly. MetaQuotes releases bug fixes every few months. Go to Help → Check for Updates in the terminal. Outdated builds sometimes cause obscure errors (e.g., "Array out of range" in built-in indicators).

Common Mistakes and Troubleshooting

Mistake

Community

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

0 claps0 comments

Related articles