Skip to content
← All posts

EA Source Code

How to Compile MQ5 in MetaEditor and Fix Common Errors

EABuild Team · · 9 min read

How to Compile MQ5 in MetaEditor and Fix Common Errors – EABuild

This guide shows you how to compile MQ5 in MetaEditor, step by step, and what to do when the build stops with an error. Compiling turns a readable .mq5 source file into an .ex5 file that MetaTrader 5 can run. You will learn what to prepare, the exact order of the steps, how to read the messages MetaEditor prints, and how to check the result before the program touches a live account.

What you need before you compile

Compiling is quick, but most failed builds come from a missing piece rather than bad code. Check these four things first.

  • MetaTrader 5 with MetaEditor installed. MetaEditor comes with the terminal. If you have not installed the platform yet, the download page is at metatrader5.com.
  • The complete source download. A project can hold a main .mq5 file plus .mqh header files (shared code the main file pulls in) and sometimes extra folders. Unzip everything and keep the folder layout.
  • Source written for MQL5. A .mq4 file is MetaTrader 4 code and will not build as an MT5 program without rewriting. Our MQ4 and MQ5 source code guide explains the difference between the two formats.
  • An untouched backup. Copy the unedited download to another folder before you edit anything, so you can always return to a known starting point.

Where the files go

MetaEditor builds files that sit inside the terminal's data folder. In MetaTrader 5 choose File, then Open Data Folder. Inside it, the MQL5 folder holds Experts for expert advisors (EAs, programs that place trades automatically), Indicators for indicators, Scripts for scripts and Include for shared header files. Put an EA project under Experts, an indicator under Indicators, and keep each project in the same relative layout it had in the download.

How to compile MQ5 code: the steps

These steps assume the files are already in the right folder of the data folder.

  1. In MetaTrader 5 press F4, or click the MetaEditor button on the toolbar. MetaEditor opens as a separate window.
  2. In the Navigator panel on the left, expand MQL5, then the folder that holds your file, for example Experts.
  3. Double-click the .mq5 file. The code opens in the editor.
  4. Press F7, or click the Compile button on the toolbar. MetaEditor starts the build.
  5. Open the Errors tab in the panel at the bottom of the window. It lists every error and warning, and it ends with a line that gives the totals.
  6. If the totals show zero errors, look in the same folder as the source file. A new .ex5 file with the same name is the compiled program.
  7. Go back to the terminal, refresh the Navigator panel or restart MetaTrader 5, and the program appears in the matching list, ready to attach to a chart.

If you only need to run a finished EA and not build it, the guide to installing an EA or indicator in MT5 covers the shorter route.

Compile one file or the whole project

Compiling the main .mq5 file also builds the header files it includes, so you normally compile only the main file. If you open a header file on its own and press F7, you may get errors that do not exist in the full project, because that file depends on definitions made elsewhere. Always compile from the main file.

How to read the Errors tab

Each row in the Errors tab gives a description, the file, the line and the column. Double-click a row and the cursor jumps to that place in the code. Two kinds of message appear.

  • Errors stop the build. No .ex5 file is produced until they are gone.
  • Warnings do not stop the build, but they point at code that may not behave the way the author meant. Read them rather than ignoring them.

Start at the first error in the list, not the last. One early mistake, such as a missing bracket, can cause a long run of errors further down. Fix the first one, press F7 again and see how many remain. The list often shrinks a lot after a single fix.

Common compile errors and how to fix them

The wording below follows what MetaEditor typically prints. The exact text can differ a little between builds, but the cause and the fix stay the same.

Cannot open an include file

A message such as cannot open file followed by a name that ends in .mqh means the compiler cannot find a header. The usual causes are that the file was left out of the copy, or that it sits in the wrong folder. Check the #include line at the top of the source. A name in angle brackets points into the Include folder of the data folder, while a name in quotation marks is looked up next to the source file. Move the header to the place the line expects.

Undeclared identifier

Undeclared identifier means the code uses a name the compiler has never seen. Typical reasons are a typing error in a variable name, a variable declared inside another block of code so that it is out of reach, or a function from MQL4 that does not exist in MQL5. Compare the spelling first. If the name is a function, look it up in the MQL5 documentation to see whether it exists and what it is called there.

Function not defined, or wrong parameter count

MetaTrader 4 and MetaTrader 5 handle orders differently. Old MQL4 code that calls the order functions in the MT4 way does not match the MT5 versions, so the compiler reports a wrong number of parameters or an unknown function. The fix is not a small edit: that part of the program has to be rewritten for the MQL5 trading model, or you need a version of the code that was written for MT5 from the start.

Semicolon, bracket or parenthesis expected

Messages that name a missing semicolon, an unbalanced parenthesis or a missing curly bracket are typing slips. Look at the line the message names and also at the line just above it, since the compiler often notices the problem one line late. Matching every opening bracket with a closing one usually solves the whole group.

Possible loss of data due to type conversion

This one is a warning. It says a value is being squeezed into a smaller type, for example a number with decimals stored in a whole-number variable. The build still finishes, but the stored value may be cut. Check whether the change is intended. If it is, you can make the conversion explicit with a cast, which tells the compiler you meant it.

Build finishes but nothing changes in the terminal

If the Errors tab shows no errors but the terminal still runs old behaviour, you may have compiled a copy in a different folder from the one the terminal reads. Confirm that the .ex5 file sits under the data folder that File, Open Data Folder opens, and that the program on the chart was removed and attached again after the build.

Compiling a source package: an example

A source code product shows how this works on a real project. The Smart Gold Hunter EA Source Code (MQ5) listing is for MetaTrader 5 and trades XAUUSD on M5 and M15. As listed, the ZIP holds one compiled file, one source file and six set files, and no DLL is used. A set file is a saved list of input settings that you load from the EA's inputs window.

For a package like this, put the source file under Experts, open it in MetaEditor and press F7. You then have an .ex5 file that you built yourself from the code you can read. The listing describes the strategy as scalping, news trading and hedging, and it marks the version as 3.5. Reading the source lets you see how those parts are written before you rely on them. Our guide to loading a set file in MT4 and MT5 explains the step that follows the build.

Checks after a clean build

A program that compiles is not a program that works. The compiler checks grammar, not trading logic. Before you use a build, go through this short list.

  1. Confirm the totals line shows zero errors, and read every warning.
  2. Attach the program to a chart on the symbol and timeframe it was written for, with algo trading enabled, and watch the Experts tab of the terminal for error messages.
  3. Run it in the Strategy Tester to see that it opens, manages and closes trades the way the code intends. A tester run uses stored price data and says nothing certain about the future.
  4. Test it on a demo account for a period long enough to see it handle spread changes and fast markets.
  5. Check the settings against your broker's contract size and account type before you move to a live account.

If you change the code, keep the backup, change one thing at a time and compile after each change. An edit that builds cleanly can still alter how the program trades, and nobody is obliged to help you debug your own changes.

Where to go next

Once you can build a file from source, you can read other projects the same way. You can browse the EA source code downloads in the catalogue, and every product in the shop is included in the membership. eabuild.app is a shop that sells digital downloads; it is not the developer or publisher of the products.

Trading foreign exchange and CFDs carries a high level of risk and may not be suitable for all investors. Past performance does not guarantee future results; only trade with money you can afford to lose.

Tags: EA Source Code, MetaEditor, MQL5

← All posts

Get every EA with membership

313 products · from $16.59/month

See plans

More in EA Source Code