Welcome to MQL4


Welcome to the MQL4 course.

In this series of articles I will try to remove the ambiguity and confusion surrounding MQL4 by offering you an easy-to-understand tutorial with some straightforward examples.

My primary goal is to show you how to use the MQL4 programming language to build your own Expert Advisors, Custom Indicators and Scripts.
My secondary goal (at the very least) is to make sure you can understand/ the code of the Experts, Indicators and Scripts that you download from the Net and to learn how they work.

If you are coming from another programming language, especially C or C++, the good news is that you will find the MQL4 programming language is very much alike. If you have never programmed in any language before, no problem as I'll guide you to understand the concept of programming in general as well.

So, let's start from the beginning.

MQL4? What, Why and Where?

MQL4 stands for MetaQuotes Language 4.

MetaQuotes is the company that built the MetaTrader Trading Platform. To make this trading platform stronger than the other trading platforms, the company provided it with a built-in programming language.
The built-in programming language (MQL4) enables the user (you) to write his own trading strategies and technical indicators.

The MQL4 language enables you to create one of the following:

1- Expert Advisors.

2- Custom Indicators.

3- Scripts.

• Expert Advisor: this is a program that can automate trading for you. For example, it can send a buying order to the broker server for execution at specified times or conditions. It can automate your stop loss/take profit automatically; it can cancel, replace or close your orders. The Expert Advisor has no access to the indicator functions.

• Custom Indicator: this is a program that enables you to use the functions of the technical indicators for analyzing price data and drawing on the charts. But it cannot automate your trades.

• Script: this is a program that is designed for single task execution. Unlike the Expert Advisor, scripts can only be executed once (when you attach them to a chart), and each script removes itself after the last line of its code is executed. It will NOT reside in the chart (like the experts). Script too has no access to the indicator functions.

This explained what MQL4 is, and why to use it.

Now, "Where" do I write MQL4?

To write your MQL4 code, as with anything else in the world, you can choose one of two ways – the hard way or the easy way.

1- The hard way:

The hard way is to compile your program using your favorite text editor and the command prompt.

Notepad is one choice of text editor, but do not forget to do two things:
1- To save the file you have created in plain text format.
2- To save the file as .mq4 (that's to make it easier to reopen with MetaEditor), but you can save it as any extension you prefer.

After saving your program, there is an extra step needed to bring your code into the light of day. This is the Compiling step.

Compiling means taking the humanly readable code that you have just written and converting it to the machine language that the computer can understand.
MetaTrader has been shipped with its own compiler (the program which will convert your code to machine language), called MetaLang.exe.

Metalang.exe is a console program that takes two parameters and outputs an .ex4 file (the file that MetaTrader understands).

The first parameter is the options parameter and the only option available is –q = quit.
The second parameter is the full path to your .mq4 file.

The syntax will be in this format:

metalang [options…] filename

Example:

1- Find your metalang.exe path; it should be the same path as MetaTrader (here my path is D:\Program Files\MetaTrader 4).

2- Create a batch file (a .txt file but save it as .bat) and name it compile.bat (or any name you prefer).

3- Write these lines into the .bat file, then save it:

cd D:\Program Files\MetaTrader 4
metalang -q "D:\Program Files\MetaTrader 4\my_first_mql4_script.mq4"


(Don’t forget to change the path to you MetaTrader installed path).

4- Run the batch file and, if you are lucky like me, you will get a screen like that shown in Figure 1.

metatrader 4 metalang compiler
Figure 1 Metalang compiler

As you see you will get the output file my_first_mql4_script.ex4

2-The easy way:

MetaTrader has been shipped with a very good IDE (Integrated Development Editor), called MetaEditor, which has the following features:

1- A text editor that highlights different constructions of the MQL4 language while you are writing/reading code.

2- Your program is easy to compile; just click F5 and the MetaEditor will do all the hard work for you. It also produces the ".ex4" file.

3- Errors in your code are shown in the Error Tab (see Figure 2).

4- A built-in reference book can be accessed by highlighting the keyword you want to know more about it and then pressing F1.

Metatrader MetaEditor 4
Figure 2 MetaEditor 4

In the following chapter we are going to learn more about MetaEditor.
Today I just came to say hello. Tomorrow we will start the real work – we will study the syntax of MQL4.

See you
Coders’ Guru

Note: MetaTrader, the MetaTrader logo and MetaEditor are trademarks or registered trademarks of MetaQuotes Software Crop.

Do You Like this Article?