programming
Now you are reading
MQL Practically. Ace of Intelligence, ie notifications on the phone [Programming Course]
0

MQL Practically. Ace of Intelligence, ie notifications on the phone [Programming Course]

created Radek SzafronApril 12 2019

Welcome back to the IV part of our practical programming course. Today we will deal with matters of immense importance, we will create Asa of Intelligence, which in our absence will deal with intelligence activities focused on price changes on the stock market. When things get the right pace, ours As will immediately send a secret (not very) message to our mobile phone that will allow us to take matters into our own hands.


Be sure to read: MQL Practically. Panic Button, part II


Our application will allow you to set price levels, upon which we will receive a notification push on our phone. Levels will be determined by dragging the colored lines that will draw after clicking on the figure of our Asa of Intelligence. Let's start with what these notifications are and how to configure our devices to make everything ok.

Notifications push

Notifications push they are a function of the Meta Trader platform, which allows you to send messages from the terminal to an iOS or Android device that has the Meta Trader mobile application installed. If you do not have an app on your mobile yet, please download it, because without it, ours As it can not handle very well.

rys_1 rys_2

Device configuration is very fast. In the first step, you should know yours MetaQuotes ID, i.e. the number by which we will direct the message to the appropriate phone (or refrigerator with android system). Number MetaQuotes ID is in the mobile application on the menu Settings / Chat and Messagesas in the picture below.

Rys_3 forex programming

The number is being read MetaQuotes ID then enter into the MetaTarder program on your computer in the menu Tools / Options / Notifications.

Rys_4 forex programming

Finished!

Technical background

At the beginning, download the package located a bit lower and install the source code of today's application, or file As Intelligence.mq4 In the catalogue MQL4 \ Experts \ .

Our program will also have a small graphic interface placed in the library elementy_graficzne.ex4which you copy to the directory MQL4 \ Libraries \ As Intelligence .

DOWNLOAD FILES

We start

After opening the file As Intelligence.mq4in the upper part you will see the following code snippet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#property strict
#import "Ace of Intelligence \\ elements_graficzne.ex4"
void AgencieRozpocznijMisje (int x, int s);
void AgencieByeBye ();
string AgencieIdentyfikujSie ();
#import
input color Colour = clrDodgerBlue;
input color Kolor_wylaczony = clrGold;
input ENUM_LINE_STYLE Style = STYLE_SOLID;
input int Thickness = 2;
string Name of the instrument = "";
string Id_line = "INTERVIEW_LINES";
MQL4 code

We start the code from the library import, and then using input keywords we create parameters for configuration in the settings window. The settings will allow us to define the appearance of the line, by means of which we will set the price for our notifications.

Global variables

Under the definitions of type input we see something new, variable declarations Nazwa_instrumentu and Id_liniithat are outside the content of any function. What does it mean? In this way we define global variables, which will be available in every place in our code, that is, each function of the algorithm will be able to read them and give them value. From variables declared as input they differ only in that the user can not access them in the settings window.

Tables

When writing software, there is often a need to handle so much data that creating a separate variable for each of them would be at least cumbersome. The tables come to the rescue, allowing you to create an ordered set of data of a given type, e.g. a type double, available with one name we have set. For each table we can (do not have to) define its size, ie how many pieces of data of a given type will be in it. Let's take a look at the following code snippet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string Tablica_nazwy_linii [];
double Tablica_dane_linii [] [4];
#define CZAS_AKTYWACJI 0
#define TYP_SYGNALU 1
#define STATUS_LINII 2
#define CENA_LINII 3
#define SYGNAL_LONG 0
#define SYGNAL_SHORT 1
#define LINIA_WYLACZONA 0
#define LINIA_ZMODYFIKOWANA 1
#define LINIA_AKTYWNA 2
#define LINIA_USUNIETA 3
MQL4 code

One-dimensional boards

In the above code, in the 1 and 2 lines, we created two tables. Tablica_nazwy_linii it will be used to store the names of objects that we will draw on the graph. Each line will represent one, set by the user, notification, and because our application allows you to set any number of notifications you need to use tables.

Tablica_nazwy_linii is a one-dimensional array, which we recognize after one pair of square brackets []. In square brackets we can enter the number of pieces of data we want to put in the table, but in our case we do not know how much the line will be on the graph, because it depends on the user, so leave an empty bracket, which means that we will dynamically, on a regular basis, determine and resize the table according to your needs. Let's take a look at the drawing below, which describes how one-dimensional boards work.

Rys_5 forex programming

Forex Programming - Arrays

Multidimensional arrays

The second board, named Tablica_dane_linii will serve to store additional data for each of the user-defined notifications. We can not put additional information in the same table as object names, because the names are of type stringand type data doubleand one table can contain only one type of data.

Tablica_dane_linii is a two-dimensional board, which symbolizes two square brackets [] [4]. The first dimension will be used for identification, as in the table Tablica_nazwy_liniiwhich line we mean, and the second dimension to store 4 additional data for each of the set notifications.

To make it easier for us to navigate the elements of a two-dimensional table using keywords #define we have created a list of predefined values ​​with, as far as convenient to remember names. Under each name listed after the keyword #define there is a number that we are sure that will be constant.

In the following, we will use the boards in practice, meanwhile, let's look at the following graphic which explains how a two-dimensional table works.

rys_6

Application structure

Our application will use five, most known to us, MQL API functions for event handling.

OnInit () - preparation of the algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int OnInit ()
{
if(IsDemo() == false) return INIT_FAILED;
// Record! Explore Area () is a short version
// Examine Terrain () == false
if(! Invest Area ()) BlurSlady ();
EventSetMillisecondTimer(250);
AgencieRozpocznijMisje (0, 0);
return INIT_SUCCEEDED;
}
MQL4 code

In function OnInit() we start by checking if we are playing in the right place, and then whether the data that the application has is correct. As it is possible, since OnInit() is the first function called by the program to make the algorithm have incorrect data? This can happen, for example, when we run the algorithm on the graph of one instrument, and then change the symbol. In this case, I will have to delete the previously drawn lines and all notifications canceled. This is what the function deals with ZatrzyjSlady ().

In the next step, we run timer and we call a function from the library Agent StartMissions (...)whose task is to draw ours Asa on the graph.

In the source code you will find several other interesting features. For those of you who would like to x-ray ours Asa from A to Z, the interpretation of additional materials should be a good training in navigating the MQL documentation. The whole code of today's application I tried to keep within the discussed issues, so that its interpretation was as easy as possible. I wish you fruitful learning and, because you already know many elements of language, have fun in creating the first, independent solutions!

OnChartEvent (...) - event monitoring

1
2
3
4
5
6
7
8
9
10
11
12
void OnChartEvent(const int id, const long & lParam const double & dparam,
const string & SPAR)
{
if(id == CHARTEVENT_OBJECT_DRAG || id == CHARTEVENT_OBJECT_CHANGE)
{
if(StringFind(sparam, Line_ID)> = 0) Update Data (sparam);
}
else if(id == CHARTEVENT_OBJECT_CLICK)
{
if (sparam == AgentIdentifySame ()) Create ();
}
}
MQL4 code

Thanks to the function OnChartEvent(...) we monitor user interaction with the application. In the first one, we check if the user dragged the line of our notification on the chart (or changed its parameters in the settings window) and if this happened, we update the data regarding our notifications in the function UpdateData (...)which as a parameter assumes the name of the user modified line. Using the received line name, the function locates the appropriate row in the tables and updates the relevant data, among others information about the price set for the notification.

In the second part of the function, we monitor the events of clicking on objects in the graph of our algorithm. Each click in the figure we have drawn Ace of Intelligence creates a new line on the chart. The created lines, after shifting, will call the function UpdateData (...)that will schedule a specific notification depending on the location of the line relative to the price at the time of interaction with the user.


expert advisors


OnTimer () - recurring activities

1
2
3
4
void OnTimer ()
{
SynchronizujDane ();
}
MQL4 code

Timer that is called 4 times per second, which we have defined in the function OnInit(), synchronizes data within our algorithm. For the tasks of the function SynchronizujDane () These include checking which lines have already been removed and giving them the right color depending on whether the notification is enabled.

OnTick () - price observation

All MQL API functions listed so far have been familiar to us. Time for something new. Function onTick() is invoked by the platform at any price change of the instrument on which the algorithm operates. This is a fundamental function for many automatic strategies. Let's look at the code below.

1
2
3
4
void OnTick ()
{
PoczynWywiad ();
}
MQL4 code

In function onTick() our As he goes to work and checks if the price of the instrument has reached the value indicated by the user. We will go to the function PoczynWywiad () a bit further, and now let's analyze one more last function needed to handle events, OnDeinit(...).

OnDeinit (...) - ending the algorithm

1
2
3
4
5
6
7
8
9
void OnDeinit(const int reason)
{
EventKillTimer();
AgencieByeBye ();
if(reason! = REASON_CHARTCHANGE && reason! = REASON_PARAMETERS)
ZatrzyjSlady ();
}
MQL4 code

We stop at the beginning timer and we delete the picture of our agent and then, using the operator if we check what the reason for completing the application is. In some cases, after calling the function OnDeinit(...) the program will restart and start its algorithm from the beginning, that is from the function OnInit(). This is the case, for example, when the user changes the graph's interval. In this situation, if the instrument has not changed, we do not want to delete the notifications set up by us. Checking the reason for calling the function OnDeinit(...)or parameter reason, we can decide whether to delete objects and data, or leave the notifications set unchanged.

Tables in practice

Let's see how our application uses previously created tables on the example of a function SaveData (...), whose task is to create a new row in the tables for the data for the new notification.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool ZapiszDane (string nazwa_linii, double cena_linii)
{
int number_lin = ArraySize(Tablica_nazwy_linii);
ArrayResize(Array_line_line, number_line, + 1);
ArrayResize(Array_path, line_number + 1);
// Write row_number ++ is the abbreviated version number_line = number_line + 1
liczba_linii ++;
Line_name_array [number_ of lines - 1] = name of the line;
Line_Data_array [number_ of lines - 1] [DAY_TIME] = 0;
Line_Data_array [number_ of lines - 1] [SIRY TYPE] = SYGNAL_LONG;
Line_Data_array [number_ of lines - 1] [STATUS_LINES] = LINE_DIRTY;
Line_Data_array [number_ of lines - 1] [PRICE_LINE] = price_line;
return true;
}
MQL4 code

In line 3, check how many items it contains Tablica_nazwy_liniiand because Tablica_nazwy_linii is an array, the one-dimensional number of its elements should correspond to the number of rows in both tables. Then, in lines 5 and 6, we increase the number of rows of our tables by 1 using API ArrayResize(...). A new row will be added at the end of each table.

We increase the variable liczba_linii o 1, because we added one row to the tables, and then we fill in the data about the new notification starting with the line no. 11, where we assign the name of the line to the corresponding row in the array Tablica_nazwy_linii. Remember that the rows (and columns) in the tables are numbered from zero, so if you want, for example, to save the information for the fifth notification, you must use line 4, hence in square brackets, after the name of the array number of lines - 1 as the row number.

For numbering columns in the table Tablica_nazwy_linii we use predefined values ​​at the beginning of our algorithm using keywords #define. Under the value names there are numbers from 0 to 3, because the table has 4 columns. Thanks to the use of a two-dimensional table, we can save 4 additional information for each notification, among others the price and type of signal that you use in the function SaveData (...) we give initial values.

Ace in action

Another function that uses arrays in our algorithm is the function PoczynWywiad ()where we read the data prepared in the tables and interpret them in order to decide whether to send a message to the user. Let's look at the function code PoczynWywiad ().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void PoczynWywiad ()
{
int i = 0;
int number_lin = ArraySize(Tablica_nazwy_linii);
for(i = 0; and <number of lines; i ++)
{
if(Array_data_data [i] [STATUS_LINES] == LINE_ACTIVE)
{
if(Array_data_data [i] [SIRY_TYPE] == SYGNAL_LONG)
{
if(Bid > = Line_Data_Array [i] [PRICE_LINE])
{
WyslijWiadomosc (i);
Array_data_data [i] [STATUS_LINES] = LINE_ INTERVAL;
}
}
else
{
if(Bid <= Line_Data_Array [i] [PRICE_LINE])
{
WyslijWiadomosc (i);
Array_data_data [i] [STATUS_LINES] = LINE_ INTERVAL;
}
}
}
}
}

In the above function, using a loop for we gain access to each row in the table Tablica_dane_linii. This allows us to check the notification status and signal type, and based on the received data to verify whether the price Bid it meets the condition we expect. When ours As he will feel that keeping us ignorant would be imprudent, hurriedly sends us a notification using the function Send a message(…), which cleverly gives the row number where the data about the required notification is stored. After sending the message, the notification is turned off, and As, steadfastly, continues on.

Take a look at the code on your computer for the function Send a message(…)to see how our algorithm uses the MQL API function sendNotification(…) When sending notifications push.

Summation

If the platform has not done it yet for you, compile the file As Intelligence.mq4 and test your new application! To draw a new line just click on the nice picture on the chart.

In the source code you will find several other interesting features. For those of you who would like to x-ray ours Asa from A to Z, the interpretation of additional materials should be a good training in navigating the MQL documentation. The whole code of today's application I tried to keep within the discussed issues, so that its interpretation was as easy as possible. I wish you fruitful learning and, because you already know many elements of language, have fun in creating the first, independent solutions!

What do you think?
I like it
33%
Interesting
67%
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