What is mql programming




















The principle of initiation and construction of custom functions will be shown in further episodes, however, it certainly will do no harm to have a look at what they can look like now. As the name of this special function suggests, it is responsible for the execution of the code upon deinitialization, i. Thus, it is a set of actions that are performed once the user or the terminal give the command to end the program. It may involve for example deleting graphics features of the program, parting with the user, etc.

Once the execution of the function is finished, the program ends. If the function is missing, the program will end directly upon the signal to exit, like in the case of OnInit.

In further episodes, we will finally have a look at the specific characteristics of the MQL language. We will start with the division of data into individual types. Why is it important to distinguish data according to their type?

What data types are there and what are they good for? How does a computer work with data? We will be using numbers, texts, often times also colors or enumerations, and performing various operations with such data — arithmetic, such as addition, multiplication, etc. However, we may only perform these operations with data of the same type.

A text cannot be added to a number and a color can in no way be compared to a date. Therefore, it is necessary to strictly differentiate what type of data we are working with. To communicate to the computer what type of data for a specific variable or function we are working with, we use so-called datatype identifiers in the code. We insert these identifiers in front of a variable or function when declaring it. The introduction or declaration of variables and functions can be found in further episodes.

The data in the code can be divided into the types listed in the following table. Listed here are only the basic types sufficient for the creation of common programs. It is also interesting that of all of them, the program only uses three types, i. The difference from other types is only in the size of memory the computer allocates to each value, and in the form, the data are presented to the user.

You can get to the bottom of this reading the following lines. This type of data is useful for variables and functions which by their nature can only be integers. It may be the number of positions, the number of chart candles, the difference between the prices in pips, and the like.

The value of the variables of this type is limited to the number 2 in both the positive and negative spectrum. If we need a higher number, we need to use a derived data type, such as int or long. An example of the introduction of an integer variable and assignment of a specific value is shown in the following line. In the code, it can be written as true and false or as 1 and 0. This means that the computer understands this type as an integer.

We use the string data type for data in which we want to insert text or a string of any characters. The beginning and end of the string have to be always enclosed in quotation marks. We use a decimal number where the value is not inherently an integer but can also have decimal places up to If we determine a variable or function as the data type color, the value we input in it will be understood by the computer as a specific color code according to its internal color palette.

We have three options for informing it which color we want. The easiest option is to write it in words. However, the color name must be one of the web colors the MQL is able to distinguish.

Another option is an integer number notation. On the Internet, it is usually used in the form of the so-called hexadecimal code. The color hexadecimal code can be retrieved from any simple text or graphics editor. There are more options to enter the color code, however, they may be considered as less practical. This data type is, of course, used to write dates and, if need be, time as well. The formats rrrr. Seconds, minutes and hours are facultative.

However, if the day, month or year are missing, the compiler will notify us of error. Even in this case, the date is converted to an integer number, for the purposes of the processing unit computations as the number of seconds that have passed since The date and time You will most likely never enter the number in this format, but it may happen that where you expect the date, a fantastical number like this one pops up due to some kind of error.

The cause will then be known to you. In practice, a date is seldom explicitly entered into variables. Typically, variables which bear time in them are in some way derived for example from the current time, for which we have the functions TimeCurrent and TimeLocal functions. Now you already know why this is this way, which will make your work with data considerably easier. This data type is used to create groups of data of the same type.

We may assign values to the data, but we do not have to, as they will be assigned automatically. Enumeration must be declared inside curly brackets. In this basic series, we more than likely will not need enumerations. We will be able to read about them in articles beyond the scope of this series. It is precisely variables that will be discussed in the next episode.

What is a variable and what does it do in the program? How do I create a variable? How do I assign it a value? Into what types of variables can they be divided? Work with variables is absolutely unavoidable in any program. A variable acts as the holder of a certain value we assign to it.

The value of the variable is stored in computer memory until changed again by another operation. Using variables, the program can store and present information in various forms. However, you will also be using auxiliary variables which will only operate within the code so their use will not be apparent to the user from outside. You might not feel much informed by these definitions but you will find them clear soon enough from practical examples.

The introduction of variables into the program is called declaration. It is very easy. To introduce a variable one has to only know the name and data type we wish to input. We explained data types in the previous episode. The introduction will be performed by writing the data type identifier and then the name of the variable. The name can only contain English characters.

Special characters, including the dot, for example, may not be used. A declaration is an operation, it is necessary to write a semicolon at the end of it. We will learn why in the next episode dealing with operations.

There can even be more variables of the same type at the same time, their names only have to be separated by a comma:. This way, we have declared six variables to which we are planning to input decimal numbers. If any of the variables were of a different type, we need to declare them separately. The first value assigned to a variable is called initialization. Initialization, i. The options are numerous. Initialization can be performed by direct assignation:. However, the variable Today must be declared as a string type.

Once again, do not forget the semicolon at the end. Declaration and initialization can be combined into a single operation on a single line, like this:. On the previous two lines, we have introduced an integer variable of the value 3 and the color Yell with the value Yellow and Lemon with the value Lime. Above, we have seen the creation and work with custom variables. In the MQL framework, however, there is also a number of predefined variables we do not need to declare or initialize.

If we use them in the code, they turn pink and automatically contain the appropriate value. These variables contain specific data about the market, the chart on which the program is running, and other characteristics of the Terminal. Below, you will find a table with these variables and the values they contain. You will probably never use some of them while others are used in almost every program.

By clicking on the variable name, you will open its detailed description at mql. Maybe you paused at the array type variables and looked for them in this episode. However, they deserve their own chapter so we will go through them in the next episode. Now, just briefly on the division of variables to global and local.

Whether a variable is global or local depends on the part of the program in which it is declared. If the variable is declared in the code header outside the body of all functions we talked about program structure in the fourth episode , it is a global variable. Such a variable is available to all of the functions of the program — both main and custom ones. In other words, it can be used with the same name and current value in all parts of the program.

However, this type of the global variable must not be confused with global variables which are available even for the entire terminal and all its other programs. Those are operated using a set of functions. This special type of variables will be discussed in a separate article.

Conversely, any local variable is a variable defined inside one of the functions. This is then available only for that function alone. If it is used in another function, it will not be detected, or the compiler will mark it as an error and force you to repair it. This other function can also have a variable declared with the same name but its value will depend only on the operations within this function.

If you need to convert the value of a variable from one function to another, it can be done using the parameters of the function call. However, we will only talk about those in the episode of functions. As mentioned above, the next episode will expand on the issue of variables by the arrays which are used with the very important predefined variables, such as Open, High, Low, Close, Time and Volume.

What is an array variable? How to index its value? How are its declaration and initialization different from the normal variables? How do you find a specific value from a set of values? Why such variables exist and how will they make my work easier? As we said last time, we need to allocate a part of the series to a piece on array variables, especially on the predefined ones which fortunately are their simplest instances. This is because these variables are used to initiate the characteristic values of candlesticks in the graph which are necessary for the formation of most trading systems and indicators.

A normal variable can only bear a single value at a time. In contrast, an array variable carries the whole indexed set of values which can be up to four-dimensional. Thanks to the indexes, we can then initiate a specific value from the set we currently need. At a given time, it is only possible to have access to one of the values in the set. The more dimensions the set has, the more coordinates the index has. It is the same as when we are looking for a point on a two-dimensional plane, e.

We, too, need to enter the longitude and latitude. But when we are looking for a point in a three-dimensional space, we already need to have three coordinates. Unlike the geographical coordinates, it could also be needed for example to know the floor of the building. This is nicely illustrated in an image from the website mql. Figure a illustrates a one-dimensional set of values of an array variable, figure b a two-dimensional one and figure c a three-dimensional one. Fortunately, it will suffice for the purposes of this series to work with one-dimensional sets.

The declaration of an array variable compared to the normal declaration only differs in one thing. Apart from the data type and name of the variable, the number of values in the set have to be stated as well in square brackets. Initialization also only runs slightly differently.

If we want to insert the set of values manually, we can do so by listing them in the brackets. 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. Today, anybody can join the world of professional traders and start independent trading at any time.

Hundreds of thousands of worldwide traders have already judged MetaTrader 4 Client Terminal on its merits. Choose a different symbol here if your strategy involves trading the instrument depending on the behavior of some other one. I also set the timeframe at M We see that a signal has been added for the current symbol and timeframe, which will generate events for the EA. Move on to the Trailing Stop mode. Here I have chosen a fixed size limit. I also set stop loss at points and take profit at points.

Next, we can choose one out of five modes of money management for a trading account. Trading is available with:. Click Done and gain profit! We have created an Expert Advisor without writing a single line of code. Let's take a closer look and conduct a small analysis of the sources.

Head of the table and attributes - everything is familiar. Then comes the inclusion of library modules. Expert is our robot. Then the settings that include input variables that define the interface for interacting with the application. It looks familiar, doesn't it? Of course! We set them up in the Expert Advisor Generation Wizard. In the future, the default values may be changed here. Next, an automatic global variable of the CExpert type is declared, which is initialized with a default value.

For an object, this means that the default constructor has been called. In the body OnInit, the EA is initialized, signals and filters are created and initialized with the values specified in the settings.

Below is the code from which I removed all the checks of the initialization results for success and the descriptors for validity in order to visually reduce the amount of code.

Each of them simply calls the corresponding method of the CExpert class. Accordingly, you can add some specialized solutions to the generator and create some for any need in a few clicks. Let's try to compile the code. To launch the strategy testing mode press Ctrl and F5 simultaneously or click the button in the graphical menu at the top. Control is transferred to the MetaTrader Client Terminal in the settings of the strategy tester.

Then we select the file to test and configure the rest of the parameters: symbol, timeframe, history depth, balance and leverage of the trading account, etc. There are many settings, and for each of them, there is a description in the reference file.

For detailed information on settings and testing mode focus on the tester and press F1. To begin, press Start in the lower right corner of the tester and, if testing occurs without optimization, then you can select the visualization mode for your strategy.

Do not hesitate to refer to the documentation to expand your knowledge in the question. The main drawback lies in the very purpose of these languages. Only MetaTrader Client Terminal can launch the program for execution.

There is no manual memory management. You still choose to create the variable on the stack or in dynamic scope, but in either case, you don't have access to its virtual address. He has much more merits. Low threshold of entry, especially if you already have experience in developing in statically typed languages. Support for object-oriented programming style: encapsulation and extensibility, inheritance, polymorphism and virtual methods of classes.

It is possible to create templates for classes and functions, that is, parametric polymorphism. All this allows the code to be reused many times.

Free infrastructure includes a trading terminal, development environment, strategy tester, included libraries. Let us consider the transformation of updating a program from one version to another using the example of a robot, which was previously presented in this article in the section about the capabilities of the language. You won't be able to compile this code without changes, a lot of errors will occur when trying. Note that it is now easier to select a deal for any symbol in MQL5 using the built-in PositionSelect function, without having to search through all open deals.

We use PositionSelect to check for an open position for a symbol. If there is no such one, then go inside the if body and prepare to send a request to open a deal. I will not now consider each of its fields separately. Let me just note that just like in MQL4, we have 4 out of 7 required parameters: trade symbol, volume, direction and deviation from the requested price.

We calculate the remaining three the opening price, stop loss and take profit depending on the direction of the transaction. We fill in the structure in order. Now there is no need to normalize floating-point numbers, so there is no need for the digit variable. Sending a request in the latest version of the language looks much more concise.

All information about the result of executing the OrderSend function is now returned to us in MqlTradeResult. I ended up with the following code after debugging and a series of tests. The size has not changed, although we made a change in the logic that required additional actions at the initialization stage in OnInit. Let's summarize. If you have basic knowledge of MQL, it will not be difficult to do it yourself. Further, I specially selected for you a number of the most common MQL questions and tried to answer them briefly and thoroughly.

MetaQuotes Language implements the multi-paradigm concept. During the code writing, you can use object-oriented, functional, procedural or mixed approach.

OOP supports encapsulation, inheritance and polymorphism. Function and class templates are available.

Unfortunately, it is not possible to use metaprogramming with compile-time computation. Now we are ready to write our first MQL program. The result of running such a program in the trading terminal will be the following output:. Mq4 is a simple text file. This extension allows the operating system to automatically associate the file itself with the MetaEditor development environment.

An ex4 file is an executable file that cannot be read as text, and which contains data in binary code. During compilation, the mq4 program code is converted into an executable ex4 code. This transformation is simple and occurs very often when creating programs.

This is exactly what the compiler does. The reverse process is a decompilation disassembly of the executable ex4 code into mq4 text code occurs rarely and often has no legal basis. Open source projects generally provide source code to anyone who agrees with the rules for their distribution, transformation and use. The rest of the projects protect all their developments by copyright law. There are no standard tools for performing decompilation. What if you really need to convert the executable code of the ex4 file into the program code?

Here are several answers to this question, placed in order of increasing complexity of implementation: 1. Contact the author of the program and find out the conditions for obtaining the code. Find a programmer who can implement the logic in your ex4 program and negotiate with him. Learn MQL and write a program by yourself equivalent to the one you have in the form of ex4. This path is not easy, but only it will expand your possibilities like no other.

Carry out decompilation by yourself or with someone's help. Please be aware of the liability provided by copyright law. Mq4 and mq5 files are simple text files that contain the MQL code in text form.

If you do not want to share your own source code the easiest thing to do in this situation is not to distribute files in this format. A simple way to protect your code is to distribute it in ex4 ex5 format. Files in this format are generated by MetaEditor when compiling the mq4 mq5 code.

In accordance with the language rules, instructions in the text are converted into an executable binary code, which is executed by the terminal when you add an advisor, script or indicator to the chart. Disassembling the executable code back to the original is not a trivial task, which helps to protect your source code. In any case, when decompiling into source code, meaningful variable names of your code will be lost, and it will be much more difficult to understand the program logic.

Later, it had a significant impact on the development of MQL4 and contributed to its updating. In MQL5 there is a tool for generating Expert Advisors, which makes it possible to completely abandon the need to write code by hand. In a few clicks, you will receive a trading robot along with the source code.

Setting it up for trading is done using signals. Control of the state of the trading account is based on the risk and money management strategies supplied with the trading terminal. Downloading an indicator is no different than downloading any other file to your computer.

After you restart the terminal the indicator will be available in the Navigator. All you have to do is move it to the chart and adjust the input parameters. There is backward compatibility between the major and minor versions of the language. The only limitation in this case is the MetaQuotes Server itself. Some functions and structures are missing in the fourth version of the platform. The logic of processing trade operations in the fifth version of the server has been partially changed.

There are also differences in the internal structure of the indicators. In the fourth version, no more than eight indicator buffers can be used. In MetaTrader 5, the number of buffers has been increased to The built-in iCustom function allows you to use signals from indicators to implement trading logic inside the EA. If you have your own indicator and you want to generate trading signals in the advisor to carry out trading operations, you need to use iCustom for this.

Let's look at the documentation. Here you can see the following definition of MQL4: MQL5 definition: Basically, the function does the same thing, even though its signature is different in the fourth and fifth versions of the language. Let's take a closer look at each of the parameters: 1. String symbol line.

It is the symbol that will be calculated. String parameter name. You need to pass the path to the indicator into it if, for example, it is located in another directory and also the indicator name together with the extension for which we want to calculate. Next, the required parameters are passed for the indicator that we want to use. A list of parameters for the indicator, which will be launched by the iCustom function.

Their number depends on how many arguments are needed to get the required value from your indicator. It makes sense that the function takes a variable number of parameters. For example, for Alligator it is included in the MetaTrader Client Terminal installation package : Or, for example, for Ichimoku it will be some other set of variables: Thus, in the first case, you will need to pass 6 parameters to the function to get the correct value, and 3 in the second.

There are two more parameters in MQL4 of the fourth version. The first is an integer mode that denotes the indicator line index and is used by the runtime for internal indexing of data buffers. The next integer shift indicates the forward or backward shift of the indicator along the timeline. Unfortunately, if we compare MQL with its ancestor, it is clear that the language is developing with a lag. At first glance, the code of programs in versions 4 and 5 differs slightly, but there are still distinctions.



0コメント

  • 1000 / 1000