Use MQL5 Code Checker in MetaEditor to Fix EAs

Learn how MetaEditor's built-in MQL5 Code Checker catches memory leaks, performance bottlenecks, and style issues before you compile. Step-by-step guide with.

use-mql5-code-checker-in-metaeditor-to-fix-eas

You've spent hours writing an Expert Advisor in MQL5. It compiles without errors, you run a backtest, and it works — mostly. But somewhere in the optimization, it starts throwing strange errors, or the memory usage climbs until the terminal slows to a crawl. Sound familiar?

MetaEditor includes a tool most developers ignore: the MQL5 Code Checker. It's a static analysis engine that scans your code before compilation and flags potential problems — memory leaks, performance issues, uninitialized variables, dangerous type casts, and style violations. It won't fix everything, but it catches the kinds of bugs that slip past the compiler and only show up in live trading.

In this guide, I'll walk you through exactly how to run the Code Checker, interpret its warnings, and integrate it into your daily workflow. You'll learn which warnings matter, which you can safely ignore, and how to fix the most common issues it finds. By the end, you'll have a cleaner, more reliable EA — and fewer late-night debugging sessions.

What the MQL5 Code Checker Actually Does

The Code Checker is a static analysis tool. It doesn't execute your code; it reads the source and looks for patterns known to cause runtime problems. Think of it as a spell-checker for MQL5 — it catches things the compiler won't, because the compiler only checks syntax and type correctness, not logic or resource management.

Specifically, the Code Checker looks for:

  • Memory leaks: Dynamically allocated objects (via new) that never get freed with delete.
  • Uninitialized variables: Local or member variables used before being assigned a value.
  • Performance bottlenecks: Expensive operations inside loops (like file I/O or database queries) that shouldn't be there.
  • Dangerous casts: Implicit or explicit type conversions that could lose data or crash.
  • Code style issues: Inconsistent naming, unused parameters, overly complex functions.
  • Resource leaks: Files, network connections, or chart objects not properly closed or released.

It's not a profiler — it won't tell you which function is slow. But it will tell you if you're doing something stupid with memory or resources before you ever hit F5.

Prerequisites

Before you start, make sure you have:

  • MetaTrader 5 (build 2000 or newer — older builds have a less capable checker). The Code Checker is not available in MetaTrader 4. MT4's MetaEditor has a basic syntax checker, but nothing like this.
  • An MQL5 project — an EA, indicator, or script you're developing. If you don't have one, open any EA from the Experts folder and experiment.
  • MetaEditor installed (it comes with MT5). You'll find it in the MT5 installation directory under MetaEditor64.exe (or MetaEditor.exe for 32-bit).

No special accounts or licenses needed — this is built into the free MetaEditor.

Step-by-Step: Running the Code Checker

Step 1: Open Your MQL5 File in MetaEditor

Launch MetaEditor. You can do this from MT5 by pressing F4, or by double-clicking MetaEditor64.exe directly. Navigate to your EA file in the Navigator panel (left side) under Experts, and double-click to open it in the editor.

If you don't have a project, create a new one: File → New → Expert Advisor, give it a name, and accept the defaults. This gives you a skeleton EA with a OnTick() handler and some basic structure.

Step 2: Run the Code Checker

With your file open, go to the menu: MQL5 → Code Checker. Alternatively, press Ctrl+Shift+C (not Ctrl+C — that's copy). The keyboard shortcut is the same on both Windows and macOS under Wine/Parallels.

A dialog titled Code Checker appears. It shows a list of rules grouped by category: Performance, Memory, Style, Portability, Security, and General. By default, all rules are enabled. You can uncheck categories you don't care about, but I recommend leaving everything on for the first pass.

Click the Check button. The checker scans the current file and any included files (like Trade.mqh or Chart.mqh). Results appear in the bottom pane of MetaEditor, under the Code Checker tab. Each result shows:

  • Severity: Error, Warning, or Information.
  • Rule name: e.g., MQL5_MEMORY_LEAK.
  • File and line number.
  • Description: A human-readable message.

Double-click any result to jump to that line in the editor.

Step 3: Interpret the Results

Let's look at a real-world example. Suppose you have this code in your EA:

void OnTick()
{
   double *prices = new double[100];
   // ... use prices ...
   // forgot to delete[] prices
}

The Code Checker will produce:

SeverityRuleMessage
ErrorMQL5_MEMORY_LEAKMemory leak: 'prices' is never deleted. Allocated at line 3.

That's a hard error — every allocation must have a matching deallocation. Fix it by adding delete[] prices; at the end of the function (or better, use ArrayResize with a static array).

Here's another common one:

int i;
for(i = 0; i < 100; i++)
{
   Print(i);
}

The Code Checker flags this with a WarningMQL5_UNINIT_VAR — because i is not initialized before the loop. It works because the loop assigns i = 0, but the checker doesn't track that deeply. The fix: declare int i = 0; or use for(int i = 0; ...).

Performance warnings look like this:

for(int i = 0; i < ArraySize(prices); i++)
{
   FileWrite(handle, prices[i]); // file I/O inside loop
}

Rule MQL5_PERF_IO_IN_LOOP tells you to move the file write outside the loop — batch the data and write once. That's a real performance win if you're processing thousands of bars.

Step 4: Fix the Issues

Work through the list from top to bottom. Start with Errors — these are almost always bugs that will crash your EA or leak memory. Then handle Warnings — many are style or potential issues, but some (like uninitialized variables) can cause intermittent failures. Information messages are often style suggestions; I ignore most of them, but they can help enforce team coding standards.

After each fix, re-run the Code Checker (Ctrl+Shift+C) to confirm the warning disappears. It's faster than recompiling, since the checker runs on the source directly.

Step 5: Compile and Test

Once you have zero errors and only a few warnings (or none), compile your EA (F7). The compiler will catch syntax errors the checker missed. Then run a backtest to confirm the EA behaves as expected. The Code Checker doesn't replace backtesting — it just catches problems early.

Tips and Best Practices

Run the Checker Before Every Compile

I've made it a habit: write a few lines, hit Ctrl+Shift+C, fix warnings, then compile. It adds maybe 10 seconds per iteration and saves hours of debugging later. The checker is fast — even on a 10,000-line EA, it finishes in under a second.

Don't Ignore Memory Warnings

MQL5's garbage collector handles some objects (like CObject descendants from the standard library), but raw new/delete calls are your responsibility. Every MQL5_MEMORY_LEAK warning is a real leak. In a 24/7 running EA, even a small leak (100 bytes per tick) will crash the terminal after a few hours. Fix them.

Use the Rule Configuration Wisely

You can disable specific rules if they don't apply to your coding style. For example, if you prefer Hungarian notation (like iCount), the Style category may complain about naming conventions. Uncheck those rules — they're noise for your project. But leave Memory and Performance on.

To configure: MQL5 → Code Checker → Settings. Here you can set severity levels per rule (Error, Warning, Info, or Off). I set all memory rules to Error, performance rules to Warning, and style rules to Info.

Check Included Files Too

The Code Checker scans #included files by default. If you use the standard library (like Trade.mqh), you'll see warnings from MetaQuotes' own code — things like unused parameters in library functions. Don't fix those; they're not your code. Just note them and move on. You can filter them out by selecting only your project's files in the results pane (right-click → Show only current file).

Combine with the Profiler for Deep Analysis

The Code Checker finds static problems. The Profiler (available in MT5's Strategy Tester) finds runtime bottlenecks — which functions consume the most CPU time. Run both: first the checker to clean up obvious issues, then a profiled backtest to find slow functions. They complement each other.

Common Mistakes and Troubleshooting

"The Code Checker doesn't find anything, but my EA still crashes."

The checker is static — it can't detect logic errors, race conditions, or data-dependent crashes. If your EA crashes only on certain market conditions, the checker won't help. You need to add logging and run stress tests. The checker is a first line of defense, not a silver bullet.

"I get hundreds of warnings from library files."

As mentioned, MetaQuotes' own code isn't perfect. Right-click in the results pane and choose Show only current file to see only your code. Alternatively, create a separate project folder for your EA and only check that folder.

"The checker says 'memory leak' but I'm using CObject."

If you use new CMyObject() and add it to a CArrayObj list, the list's destructor will delete it — no leak. But the Code Checker can't always see that relationship. If you're sure the object is managed, you can suppress the warning with a comment:

//--- MQL5 code checker: suppress memory leak warning
CMyObject *obj = new CMyObject();
list.Add(obj); // list takes ownership

Or simply disable the rule for that file. I prefer to add the comment so future readers know it's intentional.

"The checker reports a performance warning, but my code is fine."

Sometimes the checker flags legitimate patterns. For example, calling SymbolInfoDouble() inside a loop is flagged as a performance issue — but if you're reading tick data every tick, you can't avoid it. In those cases, acknowledge the warning and move on. The key is to know why it's flagged and decide if it matters.

"I can't find the Code Checker in MetaEditor."

Make sure you're using MetaTrader 5, not MetaTrader 4. MT4's MetaEditor has a Syntax Check (F7) but no Code Checker. Also, very old MT5 builds (before 2019) don't have it. Update to the latest build: in MT5, go to Help → Check for Updates.

Summary and Next Steps

The MQL5 Code Checker is a free, built-in tool that catches memory leaks, performance bottlenecks, and style issues before they reach your trading account. It's not a replacement for thorough testing, but it's the fastest way to eliminate a whole class of bugs.

Start using it today:

  1. Open any EA in MetaEditor.
  2. Press Ctrl+Shift+C.
  3. Fix every error and warning.
  4. Compile and backtest.

Once you're comfortable with the basics, explore the rule settings to tailor the checker to your coding style. And if you haven't used the Profiler yet, that's your next step — run a profiled backtest to find the slowest functions in your EA.

Cleaner code means fewer surprises. And in live trading, that's everything.

Frequently Asked Questions

Can the MQL5 Code Checker detect all memory leaks in my EA?

No — it catches only obvious cases where new has no matching delete. It can miss leaks from circular references, objects stored in containers that aren't properly destroyed, or leaks in DLL calls. Use it as a first pass, then test with the terminal's memory monitor (MT5's View → Memory during a long backtest) to confirm no growth.

Does the Code Checker work on MQL4 code?

No. The Code Checker is exclusive to MetaEditor for MetaTrader 5. MetaTrader 4's MetaEditor has a basic syntax checker (F7) that reports errors but no static analysis for memory or performance. If you're developing for MT4, you'll need to rely on manual code review and testing.

How do I suppress a specific warning I know is safe?

Right-click the warning in the results pane and choose Suppress. This adds a comment to your source code that tells the checker to ignore that specific line. You can also disable entire rule categories in MQL5 → Code Check

Community

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

0 claps0 comments

Related articles