• No results found

Function GlobalVariableDel()

In document MQL4 Book - PDF format (Page 113-117)

bool GlobalVariableDel( string name)

This function deletes a global variable. In case of a successful deletion the function returns TRUE, otherwise - FALSE. To get an error information, function GetLastError() should be called.

Parameters:

name - Name of a global variable.

To show the convenience and benefit of using GlobalVariables, let's solve the following problem:

Note, global variable of client terminal and global variable are different variables with similar names. The scope of global variables is one program, in which the variable is declared; while the scope of global variables of client terminal is all programs launched in the client terminal.

Page 1 of 6

GlobalVariables - Variables - MQL4 Tutorial

Calculation of the sum allocated to an EA for trading is not difficult. However, for conducting this calculation we need to know the number of Expert Advisors launched in a program at the same time. There is no function in MQL4 that could answer this question.

The only possibility of counting the number of launched programs is that each program should announce itself by changing the value of a certain GV. Further all programs needing this information can refer to this GV and detect the current condition.

It should be noted here, that in general case not every program is intended for solving such a problem. If an Expert Advisor occurs that does not announce its existence, it will not be counted. That is why in this case the problem definition presupposes using only those EAs that contain a necessary code - both for changing the GV value and for further reading the value of this variable.

Here is an Expert Advisor that demonstrates using GlobalVariables (globalvar.mq4); it can be used for solving Problem 24:

//--- // globalvar.mq4

// The code should be used for educational purpose only.

//---

Experts=GlobalVariableGet(Quantity); // Getting current value Experts=Experts+1; // Amount of EAs

GlobalVariableSet(Quantity, Experts); // New value Money=Depo*Persent/100/Experts; // Money for EAs Alert("For EA in window ", Symbol()," allocated ",Money);

return; // Exit init() }

//--- int start() // Special funct. start() {

int New_Experts= GlobalVariableGet(Quantity);// New amount of EAs if (Experts!=New_Experts) // If changed

GlobalVariableSet(Quantity, Experts-1); //..diminish by 1

Alert("EA detached from window ",Symbol()); // Alert about detachment return; // Exit deinit()

}

//---

This EA contains three special functions. Briefly: all special functions are started by a client terminal: function init() - when an EA is attached to a security window, deinit() - when an EA id detached from a security window, start() - when ticks come. The head program part contains declaration of global variables (the scope of such variables is the whole program).

Problem 24. Several Expert Advisors work in a terminal at the same time. Deposit is $10 000. Total cost of all opened orders must not exceed 30% of the deposit. Equal amount should be allocated to each Expert Advisor. Create an EA program that would calculate the sum allocated for trading.

Page 2 of 6

GlobalVariables - Variables - MQL4 Tutorial

Money allocation between the EAs depends on one changeable parameter - number of simultaneously working EAs. That is why the GV that reflects the amount of EAs should be the only one, Its name is set in the line:

string Quantity = "GV_Quantity"; // GV name

Let's analyze in details how the value of the variable Quantity is changed and processed when the program is executed. First of all, EA that is attached to a security window must announce its existence so that other EAs working in the terminal could know about it. This must be done possibly early (possibly close to the moment of attaching an EA to a security window). The best suiting place for it is the special function init(). In the first line of this function, EA requests the current value of the variable Quantity; function GlobalVariableGet() is used for this purpose:

Experts = GlobalVariableGet(Quantity); // Getting current value

Now the value of GV Quantity, no matter what it was at the moment of EA's attachment, must be increased by 1. It means that the EA that is being attached increases by 1 the amount of EAs simultaneously working in the terminal:

Experts = Experts+1; // Amount of EAs

Global variable Experts is used in the program for convenience sake. Its value is not available for other EAs. To change the value of GV Quantity, use the function GlobalVariableSet() that sets new GV value:

GlobalVariableSet(Quantity, Experts); // New value

It means new value of Experts is assigned to GV Quantity. Now this new GV value is available for all programs operating in the terminal. After that it calculates the desired sum allocated for trading to a just-attached EA and an Alert is created (here alerts are needed only for illustrating when and in what EA events happen; in a real program alerts are used only when needed).

Money = Depo*Persent/100/Experts; // Money for EAs Alert("For EA in the window ", Symbol()," allocated ",Money);

Please note, that our EA calculated the desired sum only on the basis of attached EAs (it also counted itself). When init() execution is finished, control is passed to the client terminal and the EA starts waiting for a new tick. When a new tick comes, terminal will launch the special function start().

Now inside our Problem the purpose o the EA is tracing a current amount of attached EAs - Expert Advisors can be attached and detached; consequently, the amount of simultaneously working EAs may change. Depending on this our EA should recalculate the sum allocated in accordance with problem settings. So, the first thing done by the EA at each new tick is requesting the new value of GV Quantity:

int New_Experts= GlobalVariableGet(Quantity);// New amount of EAs

and if this new value New_Experts differs from the last known Experts, the new value is considered as a current one, Money allocated to an EA for trading is recalculated and the corresponding Alert is created:

if (Experts != New_Experts) // If changed {

Experts = New_Experts; // Now current

Money = Depo*Persent/100/Experts; // New money amount Alert("New value for EA ",Symbol(),": ",Money);

}

If variables New_Experts and Experts are identical, calculation is not made, in further EA code (in the function start()) the value of the variable Money calculated earlier is used. So depending on the situation at each tick either a new Money value is calculated or the previous one is used.

At the stage of detachment each Expert Advisor included into calculations in Problem 24 must inform other Expert Advisors that it has Note: GlobalVariable name can be calculated in an executable program (names of other variables are set

by a programmer at the stage of program creation).

Page 3 of 6

GlobalVariables - Variables - MQL4 Tutorial

been detached, i.e. the number of Expert Advisors working at the same time diminished. Moreover, if this EA is the last one, GV must be deleted. The execution of deini() identifies the detachment of an EA, so the corresponding code should be located exactly in this function:

int deinit() // Special funct. deinit() {

if (Experts ==1) // If one EA..

GlobalVariableDel(Quantity); //..delete GV else // Otherwise..

GlobalVariableSet(Quantity, Experts-1); //..diminish by 1

Alert("EA detached from window ",Symbol()); // Alert about detachment return; // Exit deinit()

}

All calculations in deinit() are conducted within one operator - if. If the number of EAs is equal to 1, i.e. this EA is the last one, GV is deleted using the function ClobalVariableDel(), in other cases (i.e. when the number of EAs is more than 1) a new value smaller by 1 is assigned to the variable Quality using GlobalVariableSet() function. EAs that remain attached to a security window will detect the new Quality value at the beginning of start() execution and will recalculated the desired value of Money.

It's easy to see that values of GlobalVariables can be read or changed from any executed EA using corresponding functions. Direct calculations with GV values are not allowed. For using GV values in a usual expression, this value must be assigned to any other variable and use this variable in calculations. In our case for this purpose two variables are used - Experts and New_Experts in the following lines:

Experts = GlobalVariableGet(Quantity); // Getting current value

int New_Experts= GlobalVariableGet(Quantity);// New amount of EAs

It is recommended to compile and start globalvar.mq4 in several windows of different securities. Depending on events sequence, corresponding events are displayed in the window of Alert function. For example:

Fig. 55. Alerts in the window of Alert function as a result of successive attachment and detachment of the EA globalvar.mq4 in windows of three different securities.

There is an option in the client terminal to open the toolbar "Global Variables" where in real time mode one can see all currently open GlobalVariables and their values. This toolbar is available via client terminal menu Service >> Global Variables (F3 key):

Page 4 of 6

GlobalVariables - Variables - MQL4 Tutorial

Fig. 56. Toolbar of GlobalVariables at the moment when at the same time three EAs globalvar.mq4 are executed.

After all EAs have been detached, this toolbar does not contain any records about open global variables of client terminal.

In document MQL4 Book - PDF format (Page 113-117)