Expert Advisors on Metatrader 4

The well known Metatrader 4 (MT4) is a well adopted tool in the world of forex trading. It had such a growing success that it is still preferred to the new Metatrader 5. Besides allowing setting up orders it also contains most of the common indicators used in trading. But what caught my eye was the ability to actually code the so-called expert advisors (EA) to intervene or to analyse on quote data.

Today I decided to show you a few things I’ve been learning about MT4’s ecosystem and how to get started on setting up your own expert advisors.

First lets talk a bit about MT4’s integrated language MQL4:

  • Good for coders familiar with C/C++. Not hard to learn if you’re used to other typed languages.
  • The official documentation is quite good.
  • There are few communities online you can find snippets and trading strategies.
  • The debug tools on MT4 aren’t that great.

Honestly I believe anyone with the will can learn a bit of MQL4. After a while you get you used to it, it’s not of course comparable to more up to date editors but still ok.

The setup can be a bit unusual if you’re on a Mac. Normally I use my VPS with Windows to do all my work on MT4 but if you are already subscribed to a forex broker I suggest you grab the off-the-shelf VM they usually offer for Mac users. It’s possible to navigate the VM by accessing it through your applications and click Show Package Contents. Most of the time you won’t need to navigate through the explorer to manage files but in case you’re curious the files are usually stored in something like:

Contents/Resources/drive_c/Program Files/MT4/MQL4

The easiest way to create an EA is to simply click Create in MetaEditor and follow the wizard – Expert Advisor (template). Don’t be too worried for now on the options presented, you can name it hello_world and pick onTester as the template. You should reach something like this:

Screen Shot 2017-02-02 at 20.23.23

Fig. 1 – Setting up a new Expert Advisor

Don’t be alarmed by the amount of code present on the template, we’ll look into a bit of that soon.

After you’ve created your new EA you can then easily open the EA editor by right clicking on it on the left pane of MT4 and click Modify.


Fig. 2 – Accessing EA through the main screen

Now that we are set lets have a look on how we can start coding a nice EA.

For starters we will need an entry point function. Going back to our EA lets briefly scroll through the presented options and get to know them:

  • OnInit –  Constructor. 
  • OnDeinit – Destructor.
  • OnTick – a Tick event is generated when a new tick for a symbol is received. Seems like we can access tick data here.
  • OnTimer – Function used for initialisation.

Basically these are all event handling functions which allow interface between the MT4 platform and our program. We could easily for example create our “Hello World!” using the OnTick function like so:

Screen Shot 2017-02-02 at 21.42.11

Fig. 3 – Printing Hello World

If you compile the code (Screen Shot 2017-01-31 at 23.17.04 on top) you now have your first “Hello World!” program! Too bad it’s not working yet. To make it work we now must return to MT4 main screen and press Attach to a chart. And voilá! You should see your “Hello World” displaying on the Experts tab below.


Fig. 4 – Attaching the hello_world to a chart

The experts tab will be very useful later for when you wish to log events while the EA is chewing data, presenting results and even placing orders. To stop the EA you can right click on the chart – Expert Advisors > Removenotice the EA name on the top right of the chart along with a smiley, you are sure to know your EA is working if this happens.

If you go through the documentation you’ll see there are plenty of things one can do with an EA. The MT4 api for accessing data is pretty interesting and allows the developers to try out trading strategies or collecting data for example. Before we finish lets lets take a step forward and write EURUSD  tick values to a file. I invite you to study the code below in case it’s not clear what it is supposed to do. You can add this snippet to the existing hello_world OnTick function:


if(file_handle != INVALID_HANDLE) {
    if(FileSeek(file_handle, 0, SEEK_END)) {

        FileWriteString(file_handle, Symbol() + " - " + TimeCurrent() + " - Ask: " + Ask + "; Bid: " + Bid + "\n");



So we open a file called “EURUSD”, make use of the integrated INVALID_HANDLE constant to check for irregularities, if everything ok, get the end of the file and write Tick data. The string format is merely illustrative.

If you ran the code above, on an “EURUSD” chart, soon enough you’ll have these tick lines being written on a file. If you go back to your MetaEditor, you’ll find the file “EURUSD” on your files directory. If you have been paying attention, the fact we named the file “EURUSD” doesn’t really matter, you can attach this EA to any other chart and it will still work.

This simple example can be very useful if you wish to continue learning about MT4 and MQL4. Even if you are not fond of coding with MQL4 you can now use this data to test strategies on any other language before actually committing to having an EA executing on real-time data.

That’s it for today, I hope you find this information useful, feel free to let me know if you have any questions.

Further reading on MQL4:

By |February 2nd, 2017|Categories: ALL, How to|

About the Author:'
Software engineer passionate about the web and mobile technologies. On my free time I usually read, code and travel.