Skip to content
← All posts

EA Source Code

MQL5 Include Files Explained: What #include Does in an EA

EABuild Team · · 11 min read

MQL5 Include Files Explained: What #include Does in an EA – EABuild

MQL5 include files are the reason an EA source file can be short and still use ready-made trading code. A single #include line near the top of an .mq5 file tells the compiler to pull in another file, and this post explains what that means, where MetaTrader looks for the file, and what to check when you open source code you did not write.

What an #include line does in an MQL5 expert advisor

An expert advisor (EA, a program that places trades automatically) is written in MQL5, the language of MetaTrader 5. Its main file has the extension .mq5. That file can start with preprocessor lines, which begin with a hash sign and are handled before the real compile step.

The #include line is one of them. When you compile, the compiler reads the named file and treats its text as if you had typed it at that spot. The result is one compiled file, with the extension .ex5, that contains your code and the included code together.

This has a practical meaning. Once the EA is compiled, MetaTrader does not need the include file to run it. The include file matters only when you compile or edit the source. If you ever hand a source package to someone, the include files must travel with the main file, or the build will stop with an error.

What is usually inside an include file

Include files normally carry the extension .mqh. The extension is a habit, not a rule, but it tells a reader that the file is a shared piece of code rather than a program of its own. Typical contents are:

  • classes, which bundle data and functions into one named unit;
  • helper functions, such as a routine that rounds a lot size;
  • constants and enumerations, which give names to fixed values;
  • input declarations or #define lines shared by several programs.

An .mqh file does not have its own OnTick function, because it is not an EA. It is a toolbox that the main file opens.

The MQL5 Include folder and the Standard Library

MetaTrader 5 keeps its files in a data folder. In MetaEditor you can reach it from the File menu with Open Data Folder. Inside it sits an MQL5 folder, and inside that a folder called Include. This is where shared include files live.

The Include folder ships with the terminal and holds the MQL5 Standard Library, a set of ready-made classes written by the platform publisher. You can read each file in MetaEditor, which is a good way to learn how the classes work. The language reference on mql5.com documents the library and the #include command.

Trade.mqh and the CTrade class

The most widely used example is Trade\Trade.mqh. It defines the CTrade class, which wraps the steps of sending an order. Instead of filling in a trade request structure by hand, an EA creates a CTrade object and calls methods such as Buy, Sell or PositionClose. A minimal pattern looks like this:

  • #include <Trade\Trade.mqh> at the top of the file;
  • CTrade trade; as a global variable;
  • trade.Buy(...) or trade.Sell(...) inside the trading logic.

When you see trade. followed by a method name in an EA, look at the top of the file for the matching include line. If the line is missing, the compiler reports an unknown class name.

Angle brackets or quotes: how MetaEditor finds the file

There are two ways to write the file name, and they change where the compiler searches.

  1. Angle brackets, as in #include <Trade\Trade.mqh>. The compiler looks in the MQL5\Include folder. The path after the bracket is relative to that folder.
  2. Double quotes, as in #include "MyHelpers.mqh". The compiler looks in the folder that holds the file with the include line. A path such as "Utils\Lots.mqh" is read relative to that folder.

Remember this rule when a build fails with a message that a file cannot be opened. Check the spelling, the slash direction and which of the two forms the line uses. Then check that the file is really at that place on disk.

Local include files and shared include files

The difference between a shared and a local include file is only where you store it. The syntax is the same.

Shared includes in MQL5\Include

A shared file sits in the Include folder, usually in its own sub-folder, for example MQL5\Include\MyLib. Every EA and indicator in that terminal can use it with angle brackets. This suits code you reuse in many programs, such as a lot size helper. The cost is that the file lives outside the EA folder, so it is easy to forget when you copy the EA to another computer.

Local includes next to the EA

A local file sits beside the main .mq5 file, for example in MQL5\Experts\MyEA. The EA refers to it with double quotes. The whole project can be copied as one folder, which makes it simpler to move and to keep versions apart. The cost is that two EAs that need the same helper hold two copies, and a fix in one is not a fix in the other.

Which to choose

For a single EA that you plan to hand on or back up, a local file keeps everything together. For a helper you improve over time and use in several programs, a shared folder avoids duplicate copies. Either way, write down where each file is.

Reading include lines in EA source you downloaded

Source code in a shop package is easier to work with once you read the top of the main file first. Look for the #property lines, the #include lines, then the input block. The include lines tell you which parts of the work are delegated to other files.

The Smart Gold Hunter source code is an MT5 package for XAUUSD on M5 and M15, listed with scalping, news trading and hedging logic. Its fact sheet states one compiled file, one source file and six set files. The sheet does not say how the source is split, so open the .mq5 file and read its first lines to see whether it names any include file.

The Gold Snap source code is another MT5 package for XAUUSD, listed with breakout, trend following and neural network / AI logic, with one compiled file, one source file and six set files. The same habit applies. The listing does not describe its include lines, so read the file rather than assume.

What to check in each include line

  • Does it use angle brackets? Then the file should already exist in MQL5\Include, as with the Standard Library.
  • Does it use quotes? Then the file must sit next to the main source file, or at the relative path shown.
  • Is the named file in the package you received? If not, the compile will fail on that line.

Open each named file in MetaEditor with a double click on the name, or with the Go to Definition command, and skim what it contains. You do not have to understand every line to see that a file exists and is what its name suggests.

Steps to add your own include file to an EA

Splitting code into a separate file is a common first edit for people who start reading MQL5 source. The steps below make a small helper file and use it from an EA.

  1. Open MetaEditor and choose File, then New, then Include (.mqh). Give the file a name and a folder. Store it next to the EA if you want a local file.
  2. Write one small function in it, for example a function that returns a normalised lot size from a number.
  3. Add a guard at the top and bottom, so the file cannot be read twice by mistake: #ifndef MY_HELPERS_MQH, then #define MY_HELPERS_MQH, and #endif at the end.
  4. In the EA, add #include "MyHelpers.mqh" near the top, below the #property lines.
  5. Call the function from the EA, then press Compile and read the Errors tab.

The guard in step 3 matters when two include files both pull in the same third file. Without it, the compiler sees the same names twice and reports a redefinition. The standard files use the same technique.

If the compile step is new to you, the post on how to compile MQ5 code in MetaEditor covers the button, the Errors tab and the common messages.

Include errors and how to read them

Most include problems show up as a message in the Errors tab, with the file name and a line number. These are the usual ones.

  • Cannot open include file. The path is wrong, the file is missing, or the brackets and quotes do not match where the file is stored.
  • Undeclared identifier. The class or function you call is defined in a file that you did not include, or you misspelled its name.
  • Already defined or redefinition. The same name appears twice, often because a file was included twice without a guard.
  • Errors inside the include file. A shared file was edited and no longer compiles. Every program that uses it fails until you fix it.

The last point is the main risk of shared files. A change to MQL5\Include\MyLib affects every EA that includes it, so make a backup copy before you edit a file that several programs use.

Checks before you trade an EA you rebuilt

Compiling cleanly shows that the syntax is valid. It says nothing about whether the strategy behaves as you expect. After you move code into an include file, or change a shared one, run these checks.

  • Compile the main file again and confirm the Errors tab shows zero errors.
  • Attach the EA to a demo chart and watch the Experts tab for messages when it starts.
  • Compare the input window with the inputs you had before, since a moved declaration can change the order or the default values. The post on EA input parameters in MQL5 explains how inputs are read.
  • Run the Strategy Tester on the same symbol and timeframe before and after the change, and look for a difference you did not intend.

Every trading strategy carries risk, and an edited program is untested until you test it. Use a demo account first, and check the settings before you trade live.

FAQ

Does a compiled MQL5 EA still need its include files to run?

No, a compiled MQL5 EA does not need its include files to run, because the compiler copies the included code into the single .ex5 file. Include files matter only when you edit or recompile the source. If you hand a source package to someone, the .mqh files must travel with the .mq5 file, or the build stops with an error.

Why does MetaEditor report an undeclared identifier for CTrade?

MetaEditor reports an undeclared identifier for CTrade when the file does not include the Trade.mqh header, because that header defines the CTrade class. The same error appears for any class or function whose defining file is not included, or whose name is misspelled. Add the include line near the top of the .mq5 file, compile again and read the Errors tab.

Why should an MQL5 include file have #ifndef and #define guards?

An MQL5 include file needs #ifndef, #define and #endif guards so the compiler reads it only once, even when two files include it. Without the guard, two include files that pull in the same third file can make the compiler see the same names twice and report a redefinition.

Can editing a shared include file break other EAs?

Yes, editing a shared include file in the MQL5 Include folder can break other EAs, because every program that includes it uses the same copy. If the edited file no longer compiles, each of those programs fails to build until you fix it. Make a backup copy before you change a file that several programs rely on.

Reading include lines in two MT5 source packages this week

A practical way to learn this topic is to open one real file. Unzip a source package, open the .mq5 file in MetaEditor, and write down every #include line with the folder it points to. Then open each named file. For background on what a source file is and how it differs from a compiled one, read the MQ4 and MQ5 source code guide.

If you want packages to practise on, the two MT5 downloads named above are both listed in the catalogue, and every product is included in the membership.

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, MQL5, MetaEditor

← All posts

Get every EA with membership

876 products · from $10.75/month

See plans

More in EA Source Code

Offer ends inhm$249 lifetime →