First of all it should be noted that programs discussed in this book can be used only as applications for working in MetaTrader 4 Client Terminal.
For a better understanding of the importance of these programs in trade management, let's look at Fig. If you are interested in MQL4 programming, you must have got acquainted with the client terminal. Client terminal is a part of the online trading system. This system also includes a server installed in a dealing center. The dealing center in its turn is conne. Post on Jan 4. Category: Documents download. Tags: independent trading automated trading applications niceties of trading new level of trading embedded programming knowledge of mql4 generation language kovalyov programming.
Kovalyov Programming in Algorithmic Language Introductory Course Nowadays, a personal computer became indispensable for everybody. The rapid development of Internet and performance of modern computers opened up new vistas in many fields of human activities. As early as ten years ago, the financial market trade was available only for banks and for a limited community of specialists. Now, a trader can implement his or her ideas as an application program - write a custom indicator, a script to perform single operations, or create an Expert Advisor - an automated trading system trading robot.
This fact has been proven by the annual Automated Trading Championship, in which hundreds of automated trading systems compete for 3 months.
MQL4 is the first programming language to consider all niceties of trading on financial markets. This present textbook will help you create your own Expert Advisors, scripts and indicators and incarnate in them your ideas your algorithms of profitable trading. The textbook is intended for a large number of readers without experience in programming that want to learn how to develop automated trading applications for MetaTrader 4 Client Terminal.
The textbook is designed in such a method that to make learning MQL4 as convenient and consequent as possible. Preface It is a sort of difficulty to start writing a textbook on programming for beginners, because the area of knowledge under consideration involves some new concepts that are not based on anything previously known or usual. Generally speaking, a problem of this kind may occur in any other field of knowledge.
For example, point is known in mathematics as infinitesimal circle, whereas the circle itself is defined as a set of points ordered in a certain manner. Another way is to learn from experts in this field — from books. Unfortunately, there aren't many good books on MQL4 and not all of them are worth buying if you only plan to learn certain tricks to modify and customize the existing programs for MetaTrader.
That is why it is always good to have some free books on MQL4 coding available. Currently, only two such books are worth your attention:. It is uploaded to EarnForex. Someone here needs to respect other people, and, in the least, not try to jump the gun. I do expect you to keep your promise. I am sure there are other, helpful folks here one example is above.
Hope you understand, since you do not know the track record above I talked about. No offense here. Frankly, I don't care what WHRoeder ended up doing for someone else.
I got to look at my situation. I did not edit my post after his reply- before it, or perhaps our typing coincided. I have no use for WHRoeder's. I am doing my own damn research. If you have nothing to help me with, keep your peace and your big mouth shut. Let those other folks who have helped me in practice do so.
Whatever I have said so far makes a lot of sense to a lot. If MetaQuotes is so wonderful, why are they now backtracking, after years of hype and vaporware, on this wonderful MQL5 people all over refused to use, and promising fixes to MQL4? The binary representation of x is shifted by y places to the right. This right shift is logical, it means that all places emptied to the left will be filled with zeros. The binary representation of x is shifted by y places to the left; the emptied places to the left will be filled with zeros.
The bitwise operation AND of the binary representations of x and y. The value of the expression contains 1 TRUE in all places, in which both x and y contain non-zero; and the value of the expression contains 0 FALSE in all other places. The bitwise operation OR of the binary representations of x and y. The value of the expression contains 1 in all places, in which x or y does not contain 0. It contains 0 in all other places. The value of the expression contains 1 in the places, in which x and y have different binary values.
Comma Operation Expressions separated by commas are calculated left to right. All side effects of calculations in the left expression can only occur before the right expression is calculated.
The type and value of the result coincide with the type and value of the right expression. The transferred parameter list see below can be considered as an example. Operators and functions are considered in the sections of Operators, Functions and in the chapter named Operators. If an elementary school pupil were told that, when solving the problem about the number of pencils, he or she would have to base his or her presentation on such terms as operands, operators and expressions, the poor schoolchild would surely find it impossible.
Looking at the symbols of operations, one may think that coding is a mysterious and very complicated process, accessible only for for a kind of elite. However, coding isn't really difficult at all, you just need to make head or tail of some intensions. To be sure that this is really so, let's consider some examples.
Let us denote the number of John's pencils as variable A and the number of Pete's pencils as variable B, whereas the result will be denoted as C. Pencils are things, i. Thus, we will consider pencils as real variables, i. In our case, this will be the double type.
We will get the similar answer for the difference between the values How many more pencils does Pete have than John? Problem 2. Pupils go to the blackboard and answer in class.
John went 2 times, Pete went 3 times. How many times did boys go to the blackboard in total? In this example, we have to use the variables of int type, since we consider events that are integer by their nature you cannot go to the blackboard 0. The situation is a bit different with the variables of string type: Problem 3. At one corner of the house, there is a grocery store named "Arctic".
At another corner of the same house, there is an establishment named "Hairdressing Saloon". What is written on the house? In MQL4, you are allowed to add together the values of string constants and variables. If we add together variables of string type, strings are simply added one by one, in the sequence they are mentioned in the expression.
The value of variable Ans will be the string that appears as follows: ArcticHairdressing Saloon We obtained a not-much-to-look-at, but absolutely correctly formed value of string type. Of course, we should consider gaps and other punctuation in our practical coding of such problems. Typecasting Typecasting is modifying mapping of types of the values of an operand or an expression. Before execution of operations all but assignment operations , they are modified to a type of the highest priority, whereas before execution of assignment operations they are modified to the target type.
Let's consider some problems that deal with typecasting. As far as the formal logic is concerned, the ill-posedness of the problem is obvious. It stands to reason that events cannot be added together with things, it is wrong. Problem 5. At one corner of the house, there is a grocery store named "Arctic", whereas John has 2 pencils. With the same degree of hopelessness as far as normal reasoning is concerned we can ask either: 1.
How many in total? If you want to solve both problems above correctly in MQL4, you should refer to typecasting rules. First, we have to talk about how variables of different types are represented in the computer memory.
Data types, such as int, bool, color, datetime and double, belong to the numeric data type. The internal representation of constants and variables of int, double, bool, color and datetime type is a number.
The variables of int, bool, color and datetime types are represented in the computer memory as integers, whereas the variables of double type are represented as double-precision numbers with a floating point, i.
The value of constants and variables of string type is a set of characters Fig. The values of int, bool, color and datetime types are represented in the computer memory as integers. The values of double type are represented in the computer memory as real numbers. The values of string type are represented in the computer memory as a sequence of characters. The values of int, bool, color, datetime and double types are the values of numeric type. The values of string type are the values of character type.
We mentioned above that the values of the variables of int, bool, color and datetime types are represented in the computer memory as integers, whereas those of double type - as real numbers.
So, if we want to find out about the type of an expression that consists of variables of different types, we can only choose between three data types: int, double and string.
The values of bool, color and datetime types will prove themselves in an expression in the same way as the values of int type. So, what type will be the value of an expression composed of operands of different types? In MQL4, the rule of implied typecasting is accepted: if the expression contains operands of different types, the expression type will be transformed into the type having the highest priority; the types int, bool, color and datetime have equal priorities, whereas the double type has a higher priority, and the string type has the highest priority; if the type of the expression to the right of the assignment operation sign does not coincide with the type of the variable to the left of the assignment operation sign, the value of this expression is cast as the type of the variable to the left of the assignment operation sign; this is called 'target-type cast'; it is prohibited to cast the values of string type to any other target type.
Well, let's return to Problem 4. There can be two solutions for it. Solution 4. First of all, we need to know the value of the expression provided its operands are of different types. In compliance with the rule of implied typecasting, the value of this expression will be a number of double type.
The value of this expression is real number 5. The calculations have been performed according to the second part of the implied typecasting rule - 'target-type cast'. The final result of calculations and manipulations is as follows: The value of the integer variable F is integer 5. The result of calculations the value of the double-type variable F is real number 5. Let's now find a solution for Problem 5.
Here, in the right part, we add together the values of two variables: the one of string type, and the other one - of double type. According to the rule of implied typecasting, the value of variable A will first be cast to the string type since this type is of a higher priority , and then the one-type values will be added together concatenation.
The type of the resulting value to the right of the assignment operation sign will be string. At the next stage, this value will be assigned to the string variable Sum. As a result, the value of variable Sum will be the following string: Arctic2. In this case, we break a prohibition of target-type cast for the values of string type.
When the assignment operation is executed, the target-type cast must be performed. However, according to the rule, the string-type target cast to the types of lower priority is prohibited. This is an error that will be detected by MetaEditor at creation of the program at compilation. Generally, the rules given in this section are clear and simple: If you calculate any values, you must cast all differing types to a type with the highest priority.
Typecasting with lowered priority is allowed only for numeric values, whereas strings cannot be transformed into numbers. Features of Integer Calculations Integers are known to be the numbers without a fractional part.
If you add them together or subtract them, you will get an intuitive result. However, it is not an integer. In the above example, the expression to the right of the equality sign contains only integers, i. This value zero will be assigned to variable Z at the end. Correspondingly, other values of variables X and Y will produce other results. The calculating rule consists in the following: The value of an expression is calculated according to the priorities of arithmetical operations and left to right, each intermediate outcome being calculated according to the typecasting rules.
The expression to the right of the equality sign consists of two summands: 2. The summand 2. This expression contains two operations multiplication and division of the same rank, so we will calculate this expression left to right.
As is easy to see, the order of operations in a program is similar to that in mathematics. However, the former one differs in calculating of the types of values in intermediate outcomes, which exercises a significant influence on the final result of calculations. Particularly unlike the rules accepted in mathematics , the order of operands in an expression is very important. To demonstrate this, let's consider a small example.
The result of calculation is intuitively expected to be the same, in both cases. However, this statement is true only for real numbers. If we calculate the values of expressions composed of the operands of int type, we should always consider the intermediate outcome.
The result is integer 0 zero. In the above example, we consider just a small code fragment, in which the values of the variables of int type are calculated. If we replace these variables with constants with the same values, the final result will be the same. When calculating expressions containing integers, you must pay greater attention to the contents of your program lines. Otherwise, an error may occur in your code, which would be very difficult to find and fix later especially in large programs.
Such troubles do not occur in calculations using real numbers. However, if you use operands of different types in one complex expression, the final result may fully depend on a randomly formed fragment containing division of integers. In the section of Operators, the term and general properties of operators are considered, whereas the special properties of each operator are described in the chapter named Operators.
Operators The Term of Operator One of the key notions of any programming language is the term of 'operator'. Coding seems to be impossible for the person that has not completely learned this term. The sooner and more properly a programmer learns what operators are and how they are applied in a program, the sooner this programmer starts writing his or her own programs.
Operator is a part of a program; it is a phrase in an algorithmic language that prescribes a certain method of data conversion. Any program contains operators. The closest analogy to 'operator' is a sentence. Just as normal sentences compose the text of a story or of a novel, so operators compose a program. Properties of Operators There are two kinds of properties of operators - a common property and specific properties of different operators.
Common Property of Operators All operators have one common property - they all are executed. We can say that operator is an instruction containing the guide to operations the description of an order. For a computer, executing a program running on it means consecutively passing from one operator to another complying the orders prescriptions, instructions contained in the operators. Operator as such is just a record, a certain sequence of characters.
Operator does not have any levers, wires or memory cells. This is why, when a computer is executing a program, nothing happens in operators as such, they continue staying in the program as composed by the programmer. However, the computer that has all those memory cells and links between them experiences all the transformations inside.
If your PC has executed some transformations of data according to the instruction contained in an operator, you say: the operator has been executed. Specific Properties of Operators There are several kinds of operators. Operators of each type have their specific properties. For example, the property of an assignment operator is its ability to assign a certain value to the given variable; the property of a loop operator is its multiple executions, etc.
Specific properties of operators are considered in all details in the corresponding sections of chapter Operators in this book. We will only say here that all types of operators have their own properties that are typical only for their type and are not repeated in any other types. Using this separator, the PC can detect where one operator ends and another one starts. Character ";" semicolon is as necessary in a program as character ".
One operator may take several lines. Several operators may be placed in one line. Compound Operators Compounds A compound operator consists of several simple ones separated by character ";" and is enclosed in braces.
In order to be able to use several operators where only one is expected to be located, programmers use a compound operator they also call it "block" or "block of code". The list of operators in a compound is separated by braces. The presence of a closing brace marks the end of a compound operator.
An exemplary use of a compound in a conditional operator. It starts with the conditional operator if expression followed by a compound. The compound operator contains a list of executable operators. The body of a compound operator is enclosed in braces. Every compound operator ends with a closing brace. The body of a compound is always enclosed in braces and can consist of zero, one, or several operators.
This is a rare, but absolutely admissible construction. In this case, the operators enclosed in braces are called "a block of operators".
This use is fully acceptable. This is the programmer who decides whether to enclose operators in braces or not, just for the sake of convenient code representation. Operators must be written in the text of a program according to the format rules how they must be represented in a code. No operator may be composed beyond these rules. If the program contains an operator composed against the format rules, MetaEditor will produce an error message at compilation.
This means that the program containing the wrong operator cannot be used. You should understand the phrase "operator format" as a set of format rules typical for the given type of operator. Each type of operator has its own format. Operator formats are in all details considered in the corresponding sections in chapter Operators of this book.
Order of Operators Execution A very important characteristic of any program is the order of operators execution in it. Operators cannot be executed for no reason or by exception. In MQL4, the following order of operators execution is accepted: Operators are executed in the order, in which they occur in the program. The direction of operators execution is accepted as left to right and downwards. This means that both simple and compound operators are executed just one by one like the lines in poems: first we read the top line, then the next lower, then the next, and so on.
If there are several operators in one line, they should be executed consecutively, one by one, left to right, then operators are executed in the nearest lower line in the same order. Operators composing a compound operator are executed in the same way: any operator in the block of code starts being executed only after the previous one has been executed. Writing and Execution of Operators: Examples The text of a program containing operators is not dissimilar in aspect to a normal text or a mathematical notation.
However, this similarity is only formal. A normal text allows the notes to be placed in any sequence, whereas you must keep a well-defined order in a program. As an example, let's see how an assignment operator works. We'll solve a simple linear equation system and compare the representation of some mathematical calculations in a normal text and in a program code, in operators. Problem 7. Solution 1. A normal text on a sheet of paper: 1.
Both in the first and in the second solutions, the notes lines have a completed content. Nevertheless, the lines in Solution 1 cannot be used in a program as they are, because their appearance does not comply with the assignment operator format. The notes given in Solution 1 represent some dependences in paper form. They can only be used to inform the programmer about the relations between the variables. Operators in a program are assigned for other purposes - they inform the machine what operations and in what order it must execute.
All operators without any exceptions represent precise instructions that allow no ambiguities. Both operators in Solution 2 are the assignment operators. Any assignment operator gives the machine the following order, literally: Calculate the value of the expression to the right of the equality sign and assign the obtained value to the variable to the left of the equality sign. For this reason, nothing else but variable can be located to the left of the equality sign in an assignment operator.
This is why there is no memory cell for placing the numeric value of the expression calculated to the right of the equality sign. Let's follow the computer during execution of operators of the second solution. Passing on the operator line 1. Referencing to the right part of the operator the right part is located between the equality sign and the semicolon. The computer has detected that the right part of the operator contains a numeric value.
Recording the numeric value 5 in the memory cell of variable Y. Passing on the next operator line 2. Referencing to the right part of the operator.
The computer has detected that the right part of the operator contains an expression. Calculating the numeric value of the right part of the operator 5 - 2. Recording the numeric value 3 in the memory cell of variable. Performing steps by the computer is execution of the first operator line 1. Performing steps by the computer is execution of the second operator line 2.
In order to code a workable program, the programmer must well realize what and in what order will be executed in this program. Particularly, not all mathematical calculations will be put in a program, it is sometimes necessary to pre-prepare operators. For example, many intermediate calculations are made when solving mathematical problems.
They can help a mathematician to find a proper solution, but turn out to be useless from the viewpoint of programming. Only meaningful solutions can be included into a program, for example: original values of variables or formulas to calculate the values of other variables. In the preceding example, the first operator bears information about the numeric value of variable Y, and the second operator provides the formula to calculate the value of the variable X we are interested in.
Any workable program contains expressions of a familiar sight, but you can also find such expressions that you will be able to understand only if you rate them as possible operators in your program. However, it is quite acceptable if you consider it as an operator by the way, it is this operator that is widely used in coding.
In this operator, we calculate a new value of variable X: when executing the assignment operator i. Execution of this assignment operator results in that the variable X gets the new value 4. The machine will store this value of variable X until the variable X occurs in the left part of the equality sign in another assignment operator.
In this case, the new value of this variable will be calculated and stored up to the next possible change. Functions The Term of Function The most important technological breakthrough in computer engineering is the possibility of creation and storage of separate code fragments that describe the rules of data processing for solving a problem or a small task. Such a possibility exists in MQL4, too. Function is a named, specific part of a program that describes the method of data conversion.
Speaking about functions, we will consider two aspects: function description and function call. Function description is a specific named part of a program intended for execution. Function call function reference is a record, execution of which results in execution of a function.
In our everyday life, we can find many analogues of the function. Take, for example, the braking system of a car. The actuating mechanism that properly performs braking, as well as the idea implemented by the engineer, is the analogue of a function, whereas brake pedal is the analogue of function call.
The driver presses the pedal down, and the actuating mechanisms perform some actions and - stop the car. Similarly, if a function call occurs in a program, then the function of the same name will be called and executed, i. The general sense of a function is in taking a logically completed part of the code outside the basic text of the program, whereas only the call for this part of the code remains inside the basic text of the program.
Such program construction has some incontestable advantages: first, the program text composed in such a manner is read much easier; second, one can easily see and, if necessary, modify the text of a function without making any changes in the basic code of the program; third, a function can be composed as a single file and use it in other programs, which will release the programmer from the necessity to insert the same fragments of the code in each newly created program.
We can say that the most part of the code in programs composed using MQL4 is written in form of functions. This approach became widespread and is a standard now. Composition of a Function Thus, a function can be described and called. Let's consider an example.
Suppose we have a small program Fig. In this program, all calculations are located together, the operators are executed one by one in the order, in which they occur in the program from the top down. It would be reasonable to make a function using the two lines that find the search value. The same program using a function is shown in Fig.
In this program, a part of calculations is composed as a function. The basic code contains a user-defined function call. The description of the user-defined function is located outside after the basic code. The code of a program that contains the description of and the call for user-defined function gipo. Both versions of the program will give the same result. However, the code is composed as a single module in the first version Fig.
Upon completion of the calculations separated into the function, the calculations in the main code will be continued. To have the function executed, we have to call it refer to it. This is why the function is practically represented in two parts: the code itself composing the function function description and the function call used to launch the function referring to the function. If you don't call the function, it will not be executed.
At the same time, if you call a nonexistent function, this will result in nothing MetaEditior will give an error message if you try to compile such a program in MQL4. Function Description The description of a function consists of two basic parts - function header and function body. The header of a function consists of the type of the return value, function name and the list of formal parameters. The list of formal parameters are enclosed in parentheses and placed after the function name.
The type of the return value can be one of the types we already know: int, double, bool, color, datetime, or string. If the function does not return any value, its type can be denoted as void "without contents, empty" or as any other type.
The body of a function is enclosed in braces. The value returned by the function is given in the parentheses of operator return.
The type of the value returned using operator return , must match with the function type specified in the function header. The description of the function is ended with a closing brace. The function description must be located separately in the program, outside any other functions i. Function Call Function call represents the function name and the list of transferred parameters.
0コメント