Welcome to MQL4
Welcome to the MQL4 course. In this series of articles I will try to remove the ambiguity and the confusion of the MQL4 by giving you easy to understand tutorial with straightforward examples. My primary goal is showing you how to use the MQL4 programming language to build your own Expert Advisors, Custom indicators and Scripts. My secondary goal (at least) to understand the code of the Experts, Indicators and script you download from the net and to know how they work. |
MQL4? What, Why and Where?
MQL4 stands for MetaQuotes Language 4.
MetaQuotes is the company that built the MetaTrader Trading Platform, and to make it stronger than the other trading platforms the company provided the trading platform with a built-in programming language. The build-in programming language (MQL4) enables the user (you) to write his 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: 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 time or conditions. It can automate your stop loss/take profit automatically; it can cancel, replaces or closes your orders. The Experts has no access to the indicator functions.
• Custom Indicator: is a program that enables you to use the functions of the technical indicators for analyzing the price data. But it cannot automate your trades.
• Script:: is a program designed for single task execution. Unlike the Advisor, scripts could be executed only one time (when you attach them to a chart) and it removes itself after the execution of the last line of its code. It will NOT reside in the chart (Like the experts). The Scripts too has no access to the indicator functions.
These were “What” MQL4 is? “Why” to use MQL4?
Now, “Where” do I write MQL4?
To write your MQL4 code and as anything else in world, you can choose one of two ways, the hard way and the easy way.
1- The hard way:
The hard way is using your favorite text editor and the command prompt to compile your program.
Notepad is a choice, but do not forget two things:
1- To save the file you have created in plain text format.
2- To save the file as .mq4 (that’s to be easy to reopen it with MetaEditor), but you can save it as any extension you prefer.
After saving your program there is an extra step to make your code comes out to the light. It’s the Compiling step.
Compiling means converting the human readable code that you have just written to the machine language the computer understands.
MetaTrader has been shipped with its own compiler (the program which will convert your code to the machine language) called MetaLang.exe
Metalang.exe is a console program that takes two parameters and output an .ex4 file (the file that MetaTrader understands).
The first parameter is “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 of 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 person like me you will get a screen like figure 1.

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 these features:
1- Text editor with the feature of highlighting different constructions of MQL4 language while you are writing/reading code.
2- Easy to compile your program, just click F5 and the MetaEditor will make all the hard work for you and produces the “ex4” file.
3- Showing what the wrong in your code in the Error Tab. (see figure 2).
4- Built-in a reference book that you can access by highlighting the keyword you want to know more about it then press F1.

Figure 2 MetaEditor 4
In the coming lessons we are going to know more about MetaEditor.
Today I just came to say hello, tomorrow we will start the real works and will study the Syntax of MQL4.
I welcome very much the questions and the suggestions.
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?Tweet
