programming
Now you are reading
MQL Practically. Panic Button, part I [Programming Course]
0

MQL Practically. Panic Button, part I [Programming Course]

created Radek Szafron21 March 2019

Welcome to the second episode of MQL Practically! In the second part of our course, we will start again with a blank sheet of paper and write a new application step by step. We will get to know the elements that every programmer uses in his daily work, such as functions, logical operators and loops, which is what each of you must have heard about, but you did not know what it is. Fortunately, in our series, we give the theory tasty and without exaggeration, and we focus on what is most important, i.e. programming in practice.

This week we will write the first part of the application "Panic Button"whose name I think explains a lot already. The task of our program will be, after pressing a button, to immediately close all open positions and orders, when a cold sweat appears on our forehead, the world goes silent and the image narrows down to a small, hazy field with a flickering rate of a rushing dollar after one of the cute tweets of one of the our favorite politicians.

Let's roll up our sleeves and turn on MetaEditor

Let's start by creating an empty type file in the editor Consultant named Panic Button.mq4. The file will be in the directory MQL4 \ Experts. Next, put all the files you need today in the right folders.

Plik "Useful_functions.mqh" should be placed in the MQL4 \ Include \ Panic Button \ directory

Plik "Biblioteka_panic_button.ex4" should be placed in the MQL4 \ Libraries \ Panic Button \ directory

DOWNLOAD A SET OF MQL FILES

First characters

Let's change the code of our application, i.e. the file Panic Button.mq4 from the catalog Experts. Let us inform the compiler that we use the latest syntax of the MQL4 language by typing:

#property strict
MQL4 code

Three times "and"

#include

Using the keyword #include we can attach other source files to your code. In the file Przydatne_funkcje.mqh we have prepared the necessary function in the following part:

bool Zamknij_pozycje_i_zlecenia (bool tylko_ten_instrument)

So that we can use the additional code, we must add it to our file by typing #include and the access path. Exactly as in the box below.

#include ".. \\ Include \\ Panic Button \\ Useful_functions.mqh"
MQL4 code

#import

Key word #import however, it allows us to use external libraries. Libraries are separate, compiled algorithms that share their functions and resources with other programs. Below we import a library named Biblioteka_panic_button.ex4that contains graphic elements and functions for our button. Between elements #import we place a list of function definitions that the library should provide us with.

The correct library import looks like this:

#import "Panic Button \\ Biblioteka_panic_button.ex4"
void Przycisku_utworz_sie (int x, int y, bool wybieram_niebieski); void Przycisku_badz_czujny (); bool Przycisku_czy_jestes_klikniety (string object_name); void Przycisku_bye_bye ();
#import
MQL4 code

input

After adding the source files and libraries, we can proceed to create the functions of our program. Let's start by adding the following line:

input bool Tylko_ten_instrument = false;
MQL4 code

Element input informs the compiler that we are creating a parameter that will be available in the application settings window, and its value will depend on the user's choice. The parameter we have created takes the value of type bool czyli true and/ or false. We called him Tylko_ten_instrument and using this name we will be able to refer to its value in the future, which will help us decide whether to close all positions or only the position of the instrument on which our program is running.

First function

Let's add the following code snippet to our file:

int OnInit ()
{
return INIT_SUCCEEDED;
}
MQL4 code

The above record means that we create a function named OnInitthat will return type values int, that is integers. Functions return values ​​using a keyword return.

We mentioned that using the word return our function returns values ​​in the form of integers. So what is a mysterious record? INIT_SUCCEEDED ? For our convenience in the language there are many automatically defined values ​​and INIT_SUCCEEDED is just one of them. Under its name is an integer, known to the compiler as "everything is ok".

Function OnInit () is part of the MQL API, meaning a function available as part of the language. MQL API functions are marked in the code on purplish. After starting the application, our algorithm will automatically call the function OnInit () and he will do it only once, at the very beginning, to enable us to perform the operations necessary to run and run our program.

The content of the function

Between the braces of each function, we enter the instructions that a given function should perform. Our function OnInit () already contains one instruction - return INIT_SUCCEEDED;

Let's expand the function of the function OnInit (), remembering to code return INIT_SUCCEEDED; it always remained at the very bottom of the function, because we want to call it only when all other instructions are successful.

Conditional operator if

if(IsDemo () == false)
{
 
return INIT_FAILED;
 
}
MQL4 code

The above code, using a conditional operator if, checks whether the environment for our experiments is safe and we work on a demo account.

Operator's construction if is always very similar. A record between the brackets after the operator, that is, the record IsDemo () == false we call a conditional expression. It is a logical expression that can return one of two values ​​- true and/ or false. When the conditional expression returns a value true this will be the code between the braces that appear directly after the operator if.

The symbol == in a conditional expression asks a question if the value on its left is equal to the value on the right. Because the function IsDemo () will return the value falsewhen we enable the program on the live account, our conditional expression, as a whole, will return the value truebecause the value on the left of the == symbol will correspond to the value on the right. When a conditional expression is met, a code will be executed between the operator braces if and the program will return information about failure with the help of the operator return and value automatically generated under the name INIT_FAILED.


expert advisors


We draw a button

We draw our button using the function Przycisku_utworz_sie (int x, int y, bool wybieram_niebieski)that comes from the previously imported library. The function accepts two parameters with a data type int (integers) with names x and ythat serve as the coordinates of the button position on the graph relative to the upper right corner of the window.

In addition, the function assumes a type parameter bool (true and/ or false) named wybieram_niebieski. Because our application will be able to operate in two modes, for the entire account or one instrument, our button will have the appropriate color depending on the selected settings.

We like ify

Let's determine that we want the button to be blue when it works in single-instrument mode and red for the entire account. We will enable the coloring of the button by adding the following instruction to the function OnInit ():

bool blue = false;
if(Tylko_ten_instrument == true)
{
 
blue = true;
}
MQL4 code

In order for the algorithm to determine the color for the button, to start with, we define the variable bool named blue and we assign a value to it false. Then we use the conditional operator ifthat checks for the application settings parameter Tylko_ten_instrument was set by the user to a value true. If this is the case, the code that sets the value of the variable is run blue  also on true. A variable prepared in this way blue we can serve the function Przycisku_utworz_sie (..) by adding the following line of code to the function OnInit ().

Przycisku_utworz_sie (5, 15, blue);
MQL4 code

Last function (for today)

When the program finishes or something changes in the settings, the MQL API function will be called void OnDeinit(const int reason).

Using the function OnDeinit () we perform operations that are needed to properly terminate the program. In our case, after turning off the algorithm, it is necessary to remove the button using the function previously imported from the library Przycisku_bye_bye ().

void OnDeinit(const int reason)
{
 
Przycisku_bye_bye ();
 
}
MQL4 code

Note that there is a word in front of the function name void. It means that our function will not return any value.

Add the semicolons, rub your hands and press compile

After successful compilation, the code that we wrote today will draw a button on the graph and give it a suitable look depending on the configuration entered by the user in the settings window. Congratulations, we have completed the first stage of creating our application, we have created a user interface.

Next week, we will develop our program for order processing and we will add some interesting gadgets.

DOWNLOAD A SET OF MQL FILES

What do you think?
I like it
0%
Interesting
100%
Heh ...
0%
Shock!
0%
I do not like
0%
Detriment
0%
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 www.expertadvisors.pl.
Comments

Leave a Response