Now you are reading
MQL Practically. Basics and Market Puls [Programming Course]

MQL Practically. Basics and Market Puls [Programming Course]

created Radek Szafron12 March 2019

In the cycle MQL Practically we will provide you with the knowledge necessary to start working with applications written in MQL 4 for the Meta Trader platform. Of course, the subject of programming is very wide, but the purpose of this cycle is not to raise all possible issues and cover you with a complicated theory. The purpose of this cycle is to show you how to start working with MQL and provide knowledge that will allow you to develop yourself in this spectrum.

The purpose of the tasks that we will perform together is a separate installation of the MetaTrader 4 platform, on which only the demo account of any broker is installed. We do not want to mess up on a real bill. This may be the original version of the MT4 platform.


Basic functions

Let's start with the basics, that is from what the MT4 platform window looks like and what functions it contains for applications written in MQL. Let's take a look at the picture below.


MQL Practically - Fig.1

In the place marked with the one there is a window "Navigator" (Ctrl + N). The panel allows us to access all MQL tools installed on a given platform and is divided into "Indicators", "Strategies" i "Scripts". We run the programs by dragging them to the selected chart and configuring the parameters of the given application in the window that appears. In the place marked with the two, we can see an example of the settings window. All the parameters we can configure for the application are available here, but let's focus on the basic elements that are common to all programs. Field "Let trade" is often overlooked by users, and the bird determines whether the algorithm will be able to conclude transactions. This function allows you to disable and enable transaction functionality for each algorithm as opposed to a function "Autotrading", marked with a triangle, which is a global switch of the possibility of concluding transactions by all tools working on a given Meta Trader platform.

I think that there is no need to bother you with additional functions at this stage. The above information is enough for us to successfully launch our first program.

We can start thinking about programming. The MetaTrader platform integrates an application development environment. The environment is available from the menu "Tools / MQL4 language editor".


MQL Practically - Fig.2

After selecting the option in the menu, a new application will open - "MetaEditor". At this point, we will write algorithms for the MT4 platform. Let us stop here for a moment and get to know the most important elements of the editor.


MQL Practically - Fig.3

On the left side of the window, just like in the MT4 itself, there is a panel "Navigator". In the place marked on the above drawing, we developed the content of the Experts catalog, and then, by double clicking on the name of the program, we opened its source code, which appeared in the part of the window marked with the number two. It is in this part that we create and edit algorithms. When we finish writing the application, we must transform it into a computer readable form, that is, to compile the program by selecting the option "Compile" (F7), marked with a triangle. The compiled algorithm can be found in the panel "Navigator" MT4 platform and run dragging on the selected chart. Exactly as in the previous part of this article.

Sapling "Navigator" it reflects the structure of files in the MQL4 directory, i.e. the directory in which the platform stores all applications written in MQL. If you want to install the application, eg an application made available as part of our cycle, it is to the MQL4 directory that you should copy the file, remembering that "Strategies" we place in the catalog "MQL4 \ Experts", "Indicators" In the catalogue "MQL4 \ Indicators" and that every copy of the Meta Trader 4 platform installed on your computer has its own MQL4 directory. How to find the MQL4 catalog of a given platform is presented in the picture below (in the MQL4 icon, click the right mouse button).


MQL Practically - Fig.4

We already know how to run, install and write applications for Meta Trader 4. Let's move to the merits and start learning programming.

expert advisors

We create the first application

To enable you to take the first steps as a programmer, we will write a small application together, the task of which will inform us about the current market activity. We'll call it "Puls Rynku"and we will use busy browsing the press in anticipation of the appearance of the desired variability, which we will hear thanks to the change in the frequency of sounds generated by our algorithm.

As an attachment to this article, under THIS LINK you will find a sound file "Pulse_beep.wav". Please, put it in the catalog "Sounds"which is located in the main catalog of the MT4 platform. Catalog "Sounds" you can locate by opening the shortcut properties running the terminal, as in the picture below.

MQL Practically - Fig.5

The following pictures show how to create a new, almost empty, automatic policy file. In the MetaEditor window, select the function "New", then select "Expert Advisor", choose the name of the program and click "Next" as long as the open template of the new program appears in the editor.









We do not go on the easy way and delete the entire contents of the file in the window marked with one in the last picture. We will write the entire code of our application by ourselves. Let's get started.

              // Puls Rynku.mq4
              // Copyright 2019, Mistrz Programowania
              // https://www.mistrz.ty

By typing "//" at the beginning of the line, we create comments, that is, fragments that serve us as a programmer to create all kinds of information and notes inside the code. Comments do not affect the operation of the program. With the help of comments, you can create the header of your application, which will provide, for example, data about the author, information about the application itself or copyright. Let's create a header (or not), and then put the following code on the new line.

              #property strict

Parameters of type "#property" allow to provide the platform with additional information about the program being created. The "#property strict" parameter, placed at the beginning of the line, informs the platform that the algorithm is created using the latest syntax of the MQL4 language.

In the next step, enter the following operation in the new line.

              input string Plik_dzwiekowy =  "pulse_beep.wav";

Word "Input" informs the program that we are creating a parameter that will allow us to enter the settings needed to run the program. After "Input" We always specify the type of data entered, here it is "String" that is text. Next, we create the name of the variable, meaning, in simplified form, the name of the computer memory fragment to which we will be able to refer in the future in order to read the entered data. We call our variable "Plik_dzwiekowy", it is of the string type, that is, it will allow us to enter into the program the text that we will use as the name of the sound file to be used by our algorithm.

We see the equal sign after the variable name. It means that you want to assign text to our variable "Pulse_beep.wav". This value will be used in this case as the default value and it can be changed after dragging the program onto the graph in the settings window. We have just defined the data assignment operation. It is a simple operation, and these end with a semicolon.

At this point you should stop for a moment and read about the data types. In the above line, we used the data type "String"but there are many more types and they are a fundamental part of the language that can not be bypassed. I place an address to a fragment of the documentation describing the MQL4 language data types.
As you can see in the above fragments, a single line is very important in programming and one should be very careful about what we write in the source code, because even the smallest error will prevent the compilation of the algorithm.

We write further.

                void OnTick()   

The above record defines the function "OnTick ()". Functions are elements of the algorithm responsible for performing the operations entrusted to them. In this case, we see the integral function of the MQL language, whose task is to perform the operation when the course of the instrument on which our algorithm operates changes. The platform will call this function every time a new tick appears. The operations to be performed by each function we write are defined by ourselves. We have full freedom and virtually zero restrictions here. This time, however, we will be restrained and recreate only the sound file previously defined with the parameter "Input". We will use another, also built-in function of the MQL 4 language - "PlaySound (string filename)"the task of which is to reproduce the sound file with the name it will receive using a variable with the data type "string" that it expects between the brackets after the function name. Operation of the function call "PlaySound" we end with a semicolon, because just like the data mapping operation is a simple operation.

As you probably know, here is the topic of operations available in the language syntax.

We press compile!



I keep my fingers crossed so that everything looks like the one above. If so, the compilation went well. If the result is more like a drawing of the 10 number, then we need to locate the error using the information displayed in the bottom panel, which appeared after the compilation attempt. On the left side of the panel, we can read what the errors concern, and on the right in which row they are. Double-clicking a line with an error will take us to the appropriate part of the source code. Insert the missing semicolons and brackets and press again "Compile".



Ready! We can go to MT4 and drag our first algorithm to any chart.

Beep, beep ... 😉

See you next week. We will write a new program and explore more advanced language functions.


What do you think?
I like it
Heh ...
I do not like
About the Author
Radek Szafron
The author of the publication is Radek Szafron, owner of the Expert Advisors company, which for many years has been supporting investors by providing technologies dedicated to the FOREX market. The author is a graduate of the Warsaw School of Economics with the specialization "Financial markets" and a programmer with almost 20 summer experience. The company implements designs of algorithms and applications written in all languages ​​from the "C" family, including popular platforms Meta Trader 4 and 5. Expert Advisors can be found at