Expert Advisor-Layout of an MQ4 File
Creating a New Expert Advisor
The Expert Advisor Wizard in MetaEditor is the quickest way to get started in creating an expert
advisor. You can start the wizard by selecting New from the File menu, by pressing the New button
on the toolbar, or by pressing Ctrl+N on your keyboard.
The dialog presents you with several options. You can create indicators, scripts, libraries and include files using the wizard. You can also choose a template for generating a file. The resulting file will be saved to the appropriate directory, depending on its type. Make sure Expert Advisor is chosen and press Next.
You will be prompted for a Name, Author and Link, as well as some optional parameters. The Name
field will be the file name of your program. The EA will be saved to the \experts folder under that
The contents of the Author field will appear next to the EA name in the Strategy Tester, and as a
tooltip when you mouse over the EA name in the Navigator window. The Link field is a URL to your
website, but it will not appear anywhere outside the source code file.
You can also enter your trade parameters here. For now, add a parameter or two, but don’t bother
adjusting them. It’s best to simply add these manually to the source code later. Press the Finish
button and an expert advisor template will open with your information already added.
The default expert advisor template is rather minimal, but it contains the basic structure of an expert advisor. Let’s identify the layout of an MQL file using the expert advisor template as our guide.
The first thing to appear in any MQL file are the preprocessor directives. These are prefaced by a #.
The default expert advisor template has two: #property copyright, which is the Author name you
entered in the Expert Advisor Wizard, and #property link, which is the Link you entered in the
There are other #property directives, but almost all of them are related to indicators and scripts.
The only #property directive you should include in your expert advisor is #property copyright,
which identifies the EA as your creation.
A second type of preprocessor directive you will likely use is the #include directive. As mentioned
earlier, an include file consists of functions and source code that will be included in your project when it is compiled. The syntax for the include directive is:
The file stdlib.mqh in our example on page 19 is a standard include file that comes with
MetaTrader. It includes several miscellaneous functions that programmers may find useful. Like all
include files, it is located in the \experts\include folder.
The #define directive is used for declaring constants for use in our program. For example, instead of
typing out a long text string every time you need to use it, you can define a constant and type that
In this example, we can use the constant identifier MYCONSTANT in place of the text string in our
code. The convention for constant identifiers is to use all capital letters. Although it is not absolutely necessary, for consistency’s sake you should define all identifiers for constants using caps.
Sometimes, a function you’ll need to use is already compiled in another file, such as another expert
advisor, a library file (.ex4) or a Windows DLL file (.dll). You can import functions directly into a
project using #import directives.
Libraries are similar to include files, but instead of including the source code in our project, we will execute the other file and call the function from it. We’ll talk about using libraries later in the book.
Import directives are usually placed in include files, especially if there are many functions to import.
But if you just need to import one or two functions, and an include file for them doesn’t already
exists, then go ahead and import them directly into your project.
For detailed examples of the #import directive, see the MQL Reference page Importing of Functions,
and look at the include files in the \experts\include folder. Here is the syntax for the #import
In this example, the library file we are importing the function(s) from is library.ex4. We are
importing a single function of type double, called MyImportedFunction(). The function identifier
must match the function name in the source library file. Note the semicolon at the end of the function
Parameters and External Variables
The next section in our expert advisor source code file are the external variables. These are the
adjustable parameters for our trading system. This includes your trade settings (stop loss, take profit, lot size) and indicator settings. When you open the Expert Properties dialog for an expert advisor, you are viewing the external variables for that program.
We specify an external variable by adding extern in front of the variable. This specifies that the
variable will appear in the Expert Properties dialog, and will be viewable and adjustable by the user.
extern double StopLoss = 50;
Be sure that the identifier for your external variable is descriptive of what it actually does. (“StopLoss”is better than “stop” or “SL”, for example). You have 31 characters to describe your variable, so make the most of it. The default value for your variable will also be the default for that parameter, so choose a logical default value.
We declare any global variables at the top of our source code file, generally after the external
variables. The location does not matter, as long as both the global and external variables are placed
outside of and before any functions.
A global variable is one that is available to any function in the program. As long as the program is
running, the global variable and it’s value stays in memory, and can be referenced and changed by
any function in the program.
Technically, external variables are global as well, but the global variables we’re discussing in this
section are internal, which means they are not viewable or changeable by the user.
MQL has 3 built-in functions to control program execution: init(), deinit() and start(). The
init() function is comprised of code that is run once, when the EA is first started. The init()
function is optional, and can be left out if you’re not using it.
The deinit() function consists of code that is run once, when the EA is stopped. This function is
also optional, and it’s unlikely you will need to use it in an expert advisor.
The start() function contains the main program code, and is required in your EA. Every time the
start function is run, your trading conditions are checked, and orders are placed or closed depending
on how those conditions are evaluated.
The start() function is run on every tick. A tick is a price movement, or change in the Bid or Ask
price for a currency pair. During active markets, there may be several ticks per second. During slow
markets, minutes can pass by without a tick.
Any other functions that your EA may use should be declared after the start() function. These
functions will be called from the start(), init() or deinit() functions, or from other functions
that are called from the main program. We’ll cover custom functions later in the book.