MQL4 Book  MetaEditor  Creating and Using Programs

Creating and using programs


Application programs written in MQL4—EAs, 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 on your 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.

Figure 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 and hidden in the editor using the buttons me_1 and me_2.


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, go to the the editor File menu, and click New. Or, use the button for the creation of new files:
me_3.

After all these actions "EA Wizard" offers you a list of program types to create.

Fig. 26. Choosing a program type to be created.

Figure 26 Choosing a program type to be created.

If you need to create an EA, check EA, and click Next. In the next window it is necessary to indicate the name of a created EA. Suppose it is called create.mq4.

icon_information

The name of a created file is written without its extension (type indication).

The EA Wizard shows a window with several fields to be filled in.

Fig. 27. A window for indicating general parameters of an Expert Advisor.

Figure 27 A window for indicating general parameters of an EA.

After clicking Ok, text appears in the main window, and the full name of the created EA, create.mq4, appears 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.

Figure 28 Displaying a created file of an EA in the file system and navigator window.

Let us see the program text generated by MetaEditor.

//+------------------------------------------------------------------+
//|                                                       create.mq4 |
//|                                                       John Smith |
//|                                                  www.company.com |
//+------------------------------------------------------------------+
#property copyright "John Smith"
#property link      "www.company.com"
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+

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 EA Wizard is only a pattern, using which you can create a new program. The final program code does not obligatorily contain all the indicated special functions. They are present in the pattern only, because, as a rule, an average program contains all these functions. If any of the functions will not be used, its 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. This program could be compiled and started. It would be executed like any other program. (However, 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. 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 logic, on which a program was developed, can be forgotten (or unknown in a product by another programmer). Without string comments, it is difficult, sometimes even impossible, to understand code fragments.

icon_attention

A correctly coded program definitely contains comments.

The main advantages of comments are:

Firstly, comments separate one program part from another. It is much easier to read a wisely formatted text than a straight text.

Secondly, string comments explain in plain words what a programmer intended to do in each separate code line.

Thirdly, in the upper part of a program, general information about a program may be specified such as an author's name and contacts (including the author's web-site, e-mail and so on), 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. The style options offered by MQL4 developers is presented in the EA create.mql4. Let us view the main characteristics of 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 each line is fully visible on the screen. The maximum line length is 118 symbols for a monitor with 1024 x 768 pixel resolution.

2. Variable declaration is done at the beginning of the program. It is recommended to write a descriptive comment for each variable; briefly explain their meaning and, if required, their 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 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 three symbols).

Let us see how the code of an EA may look. In this case, the program algorithm logic is not discussed. We are interested in the program appearance. A commented program (EA create.mq4) may have the following form.

//--------------------------------------------------------------------
// create.mq4
// To be used as an example in MQL4 book.
//--------------------------------------------------------------------
int Count=0;                                    // Global variable
//--------------------------------------------------------------------
int init()                                      // Spec. funct. init()
   {
   Alert ("Funct. init() triggered at start");  // Alert
   return;                                      // Exit init()
   }
//--------------------------------------------------------------------
int start()                                     // Spec. funct. start()
   {
   double Price = Bid;                          // Local variable
   Count++;                                     // Ticks counter
   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 a while.

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 understanding the algorithm 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.

int Count=0;
int init() {
Alert (Funct. init() triggered at start");
return; }
int start() {
double Price = Bid;
Count++;
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 compile (F5) in MetaEditor should be used. If a program does not contain any errors, it is compiled, and the following message occurs in the toolbox.

Fig. 29. Editor message about a successful program compilation.

Figure 29 Editor message about a successful program compilation.

Also, when a program is compiled, a new file (create.ex4) appears in the corresponding directory (in this case, in Terminal_directory\experts). The program is 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.

Another thing that happens when a program is compiles is that a line with the name of the created EA will appear in the EAs folder of the client terminal navigator window.

Fig. 30. Displaying the name of an Expert Advisor in the client terminal navigator window.

Figure 30 Displaying the name of an EA in the client terminal navigator window.

If errors are detected in a program during compilation, MetaEditor will show the corresponding error message. In such a case, you should get back to editing the source text, fix the 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 (EA, 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 EA and an indicator will operate until a user terminates the program execution manually. A normal script will stop operating itself after executing its algorithm.

icon_attention

Any application programs (EA, indicator, or script) can only be used for trading as part of MetaTrader 4 Client Terminal when it is connected to a server (dealing center) via the Internet. None of the programs can be installed on a server or used in the terminals of other developers.

In other words, if a trader wants to use an 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.