Application programs written in MQL4 - Expert Advisors, scripts and indicators - are created using MetaEditor.
The executable file of MetaEditor (MetaEditor.exe) is provided as part of the client terminal and is located in the root directory of the terminal. The Userguide of MetaEditor is opened by pressing F1. It contains general information necessary for the creation of new programs. The editor can be opened by clicking on the file name MetaEditor.exe or on a shortcut located preliminarily on the desktop.
Structure of the Client Terminal
For the convenience of operation, MetaEditor has built-in toolbars: "Navigator" (Ctrl+D) and
"Toolbox" (Ctrl+T).
Fig. 25. Location of windows in MetaEditor.
The text of the program is located in the editor window, the toolbox windows are auxiliary.
Windows of the navigator and toolbox have moving boundaries and can be shown/hidden in the editor using the buttons and .
Creating a New Program
Usually during the creation of a new program, toolbox and navigator windows are hidden.
Thus the attention of a user is concentrated on a created program. To create a new program, use the editor menu File >> Create or a button for the creation of new files .
After all these actions "Expert Advisor Wizard" will offer you a list of program types for choosing one to be created:
Fig. 26. Choosing a program type to be created.
If you need to create an Expert Advisor, check Expert Advisor and click Next. In the next window it is necessary to indicate the name of a created Expert Advisor. Suppose it is called create.mq4.
The name of a created file is written without its extension (type indication).
The Expert Advisor Wizard will show a window with several fields to be filled in:
Fig. 27. A window for indicating general parameters of an Expert Advisor.
After clicking Ok a text will appear in the main window and the full name of the created Expert Advisor create.mq4 will appear in the file system and in the navigator window.
Fig. 28. Displaying a created file of an Expert Advisor in the file system and navigator window.
Let us see the program text generated by MetaEditor:
//+---+
#property link "www.company.com"
//+---+
You see, the code contains mainly comments. We already know that comments constitute a non-obligatory part of a program and the text of comments is not processed by the program.
There are three special functions in the program: init(), start() and deinit(). Each function contains only one operator - return(0) - an operator for exiting a function. Thus a program code generated by the Expert Advisor Wizard is only a pattern, using which a programmer can create a new program. The final program code can does not obligatorily contain all the indicated special functions. They are present in the pattern only because as a rule a usual medium-level program contains all these functions. If any of the functions will not be used, it's description can be deleted.
The following lines of the program code can also be omitted:
#property copyright "John Smith"
#property link "www.company.com"
Although the program is of no practical use, it is written correctly from the point of view of syntax. And this program could be compiled and started. It would be executed like any other program (though, there would be no calculations because there are none in the source code).
Program Appearance
Using comments in programs is strongly recommended and in some cases it is strongly essential. And it must be emphasized that a programmer not only creates programs, but also reads them. Sometimes considerable difficulties may occur when reading a program. The experience of many programmers shows that the reasoning logics, on the basis of which a program was developed, can be forgotten (or unknown in a product by another programmer) and without string comments it is difficult, sometimes even impossible to understand code fragments.
A correctly coded program definitely contains comments.
The main advantages of comments are:
Firstly, comments allow to separate one logically detached program part from another. It is much easier to read a wisely formatted text than a straight text.
Secondly, string comments allow to explain in plain words what a programmer meant in each separate code line.
Thirdly, in the upper part of a program, general information about a program may be specified: an author's name and contacts (including the author's web-site, e-mail, etc.), program allocation (whether it is a
complete trading program or a library function), its main characteristics and limitations and other useful information.
Each programmer can choose a convenient style of comments. Style offered by MQL4 developers is presented in the Expert Advisor create.mql4. Let's view the main characteristics of any acceptable appearance styles.
1. A comment line length must not exceed the main window size. This limitation is not the language syntax formal requirement, but reading a program containing long lines is not convenient. Any long line can be separated into several lines so that all they would be fully visible on the screen. For a monitor with 1024 x 768 pixel resolution the maximal line length is 118 symbols.
2. Variable declaration is done at the program beginning. It is recommended to write a descriptive comment for each variable: shortly explain their meaning and, if required, peculiarities of usage.
3. Each operator is better placed on a separate line.
4. If there is a comment in a line, it should be started from the 76th position (recommended for 17" monitors with 1024 x 768 pixel resolution). This requirement is not obligatory. For example, if a code line takes 80 positions, it is not necessarily divided into two lines, a comment can be started from the 81st position. Usually the program code part contains 50 symbol long lines and the string comment looks like a text column in the right part of a screen.
5. For dividing logically separate fragments, continuous line comments of the full width are used (118 symbols).
6. When braces are used, a tabulation size indent must be used (usually 3 symbols).
Let us see, how an Expert Advisor may look like after a program code is written in it. In this case the program algorithm logics is not discussed. We are interested in the program
appearance. A commented program (Expert Advisor create.mq4) may have the following
Alert ("Funct. init() triggered at start"); // Alert
return; // Exit init() Alert("New tick ",Count," Price = ",Price);// Alert
return; // Exit start() }
//---int deinit() // Spec. funct. deinit() {
Alert ("Funct. deinit() triggered at exit"); // Alert
return; // Exit deinit() }
//---It is easy to see that complete meaningful blocks of the program are separated by comments - continuous lines. This is a way to detach special, user-defined functions and the head part of a program:
//---Variables are declared in a separate block where each variable is described. Sometimes programs contain variables, for describing which comments in several lines should be used.
This is a rare case, but if it occurs, such a comment must be necessarily placed; otherwise not only another programmer, but the author himself will not be able to puzzle out the part after some period of time.
The right part of each code line contains an explanatory comment. The value of comments can be fully appreciated if a program does not contain any and some problems with algorithm understanding occur when reading the program. For example, if the same code is presented without comments and block separation, it will be more difficult to read it, even though the program is quite simple and short:
Alert("New tick ",Count," Price = ",Price);
return; } int deinit(){
Alert (""Funct. deinit() triggered at exit");
return;}
Program Compilation
To make a program usable in practice, it must be compiled. For this purpose the button (F5) in MetaEditor should be used. If a program does not contain any errors, it will be compiled and a message will occur in the toolbox:
Fig. 29. Editor message about a successful program compilation.
Besides, a new file create.ex4 will appear in the corresponding directory (in this case in Terminal_directory\experts) . This is a program ready for operation in the client terminal MetaTrader4. During compilation the last version of the source text of the program under the same name (in our case it is the file create.mq4) will be saved in the same directory.
Alongside with that a line with the name of the created Expert Advisor will appear in the section Expert Advisors of the client terminal navigator window:
Fig. 30. Displaying the name of an Expert Advisor in the client terminal navigator window.
If during compilation errors are detected in a program, MetaEditor will show the
corresponding error message. In such a case one should get back to editing the source text, fix errors and try to compile the program once again. A successful program compilation is possible only if there are no errors in the program.
Using a Program in Practice
If an application program (Expert Advisor, script or indicator) has been successfully compiled and its name has appeared in the client terminal navigator window, it can be used in practice.
It is done by dragging the corresponding icon form the navigator window into a security
window using a mouse ('drag & drop' method). It means the program will be attached to a security chart and started for execution.
An Expert Advisor and an indicator will operate until a user terminates the program execution manually. A usual script will stop operating itself after executing its algorithm.
It is important to note here once again that:
Any application programs (Expert Advisor, indicator, script) can be used for trading only as part of MetaTrader 4 Client Terminal when it is connected to s server (dealing center) via Internet. None of the programs can be installed on a server or used in terminals of other developers.
In other words if a trader wants to use any application program, he should switch on a computer, open MetaTrader 4 Client Terminal and start an executable file *.ex4 in a security window. During a program execution (depending on its algorithm) trading orders may be formed and sent to a server, thus conducting trade management.