MQL4 Book MetaEditor Creating and Using Programs
The EA Wizard shows a window with several fields to be filled in. 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. 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 operatorreturn(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 appearanceUsing 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.
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 commentscontinuous 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;} |
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.