MetaTrader MQL Course. Module 2: The Expert Advisor

The Expert Advisor

In the last module, we went over the MetaTrader platform and the important MT tools offered to the developer. If you’d like to review that module, click here: The MetaTrader Platform.

Today we are going to get a little more in depth with Expert Advisor development.

Before we get started, you may be wondering what is the difference between this email course and the other educational products that I offer. Well, the main difference is, in this email course I try to get readers creating Expert Advisors right away – without a full explanation of every detail.

On the contrary, in more formal education, I try to not use undefined forward references. In other words, I do not use words or phrases (related to the subject matter) unless I have defined them.
In this course I am not going to obey this rule. I may occasionally use terms with which you are not familiar. I do this in the interest of moving forward quickly. Trust me, this will not prevent you from completing each module. (If you are interested in deeper learning, it’s probably only a Google search away…)

In the last segment we used the MetaEditor to create an Expert Advisor. We found that when you select File->New from the MetaEditor menu, it starts a simple wizard to help you get started with your EA. So what does this wizard do? The answer – nothing particularly fancy:

  • It forces you to set the name of the Expert Advisor.
  • It allows you to define a copyright and a link.
  • It allows you to define user parameters.
  • It creates 3 functions: init, start and deinit

Any of these things can be done using the MetaEditor (I’ll show how later). Usually I just define the name of the EA and leave the others fields alone.

So, what are these 3 functions (init, start and deinit) and what are they for?

The init function is called when an Expert Advisor is first attached to a price chart. (Init short for initialization).

The start function is called whenever a new price tick arrives from the broker’s server.

The deinit function is called when the EA is removed from the price chart or is stopped for some other reason. (Deinit short for de-initialization).

The init function comes in handy when you need to do something just once each time the EA is attached to a chart.

The deinit can be useful if you need to do something special before an EA exits.

But, frankly, you can do quite a bit without using these functions. In fact, many of my EAs only have a start function. So, in the interest of progressing, I’m going to focus on the start function. (We’ll get back to the others in a later module).

start is the entry point of the EA. Each time a new price is sent from your broker’s server to your MT platform, your EA is called and it executes the start function. The logic of the start function is executed in order – the way you would read it.

In this exercise, we are going to add some simple functionality to the “init” and “start” functions. Also, we are going to use the “extern” keyword to see how input parameters are used.

All you need to do to cause a variable to appear on the EA Inputs window is to declare the variable as an “extern”. For example:

This code will not cause the variable to appear as an input parameter:

int number_of_periods=12;

This will:

extern number_of_periods=12;

This is what the Inputs window will look like:


This MQL code defines some input variables and display their values.

// this will define an integer, make it available as an input, and set it to 12
extern int number_of_periods=12;
// this will define an integer, make it available as an input, and set it to 12
extern int number_of_bars=0;

// this function will only be called once when the EA is first attached to the chart
int init()
// ++ will increment the number_of_bars variable each time it is called

// this function will be called each time new price data arrives from the broker
int start()
// ++ will increment the number_of_periods variable each time it is called
Comment(“number_of_periods=”, number_of_periods , ” number_of_bars=”, number_of_bars);

Copy this code and then paste it into an Expert Advisor in your MetaEditor. (You can open the EA we used last time, or create a new EA, give it a fancy name, delete everything in the file and then copy this code into it.)

Press the Compile button (or F7) to build the EA. Now open the MT platform and attach the EA to a chart. Watch the chart to see how the numbers increment.