Discussion of article "MQL5 Programming Basics: Global Variables of the Terminal"

 

New article MQL5 Programming Basics: Global Variables of the Terminal has been published:

This article highlights object-oriented capabilities of the MQL5 language for creating objects facilitating work with global variables of the terminal. As a practical example I consider a case when global variables are used as control points for implementation of program stages.

In the MQL4/5 environment there is an interesting instrument - global variables of the client terminal. It allows to create a shared data storage ares for all programs of the terminal. Moreover, the lifetime of this area does not stop with the terminal closure. This article suggests using the Object Oriented Programming tools to get a clear idea what global variables of the terminal are.

Further in the article, global variables of the client terminal will be called "global variables" unless otherwise specified.


1. Global Variables, Functions

From a programmer's point of view, a global variable is a named memory area available for all working programs of a trading terminal. Novice programmers should note that if there are several terminals working simultaneously, each of them will have its own independent memory space for global variables. They are not going to overlap.

The language developers specify in the Documentation that there are 11 functions used for working with global variables.

Theory can be found in the "GlobalVariables" section of the MQL4 textbook.

A pattern of tracking stages of module work with global variables is presented on Fig. 8.

Fig.8. Pattern of the processing flag sequence

Fig. 8. Pattern of the processing flag sequence

Author: Dennis Kirichenko

 

@Dennis Kirichenko


Hi Dennis,

I have read this article in an attempt to gain a better understanding of global variables of the platform and their functionality. How is creating or changing a global variable going to impact upon the trading environment?

Are you able to explain what the numerical value of the global variables represent?

For quite sometime I have been researching and trying to understand the global variables of the terminal, though as yet I have not understood their usage and effect of them. If possible could you please give me a simple example of how creation of a global variable can have a positive result on the balance of my trading account?

Thank you.

Regards,

Dale.

 
MetaQuotes Software Corp.:

Published article Fundamentals of MQL5 Programming - Global Terminal Variables:

Author: Dennis Kirichenko

### Assumption - What you want to implement

※ Please check the library file 2 to be used first from the link below.



① Assignment:

Want to write and read global variables (commonly called global variables) for external txt files in MT4 / MT5, which is a currency trading tool provided by MetaQuotes.


② State:

I would like to implement this using the CGlobalVar.mqh and CGlobalVarList.mqh classes described in the reference page below.

(Specifically, it performs a write / read of a global variable using the Save () / Load () method of the CGLobalVarList class)

Basically do not use the Windows API (HANDLE CeateFile W (), etc.). Д.).


③ Operation file storage location:

The default position of the destination file follows the specification of this class

/ Files

or

It is stored when FILE_COMMON is specified as an argument to FileOpen ()

C: \ { User \ User-Name \ AppData \ Roaming \ MetaQuotes \ Terminal \ Common \ Files \ File

And one of.


④ Created files:

Two simple script files as experimental samples

WriteGlobalParameters.mq4

и

ReadGlobalParameters.mq4.

It was created by.


※ remarks

① If only the purpose is to share a global variable with another terminal, you can think about the method of using shared memory, etc. etc. However, this time only think of the method using the external txt file.


② As for the file (WriteGlobalParamaeters.mq 4) on the export side, using the built-in MQL functions GlobalVariablesTotal (), GlobalVariableName (), GlobalVariableName (), GlobalVariableGet (), GlobalVariableTime (), not using Save (HANDLE) Write using the WriteFile () function (↓)

For (int i = GlobalVariablesTotal () - 1; i> = 0; i -) {

string gName = GlobalVariableName (i);

if (this.CheckGlobalVar (gName)) continue;

double gValue = GlobalVariableGet (gName);

datetime gTime = GlobalVariableTime (gName);

WriteFile (hFile, gName, gValue, gTime);

}, It was possible to write the list of variables to a txt file in the specified directory.

However, in the case of the GlobalVariable function group above, even though it could be written out, it cannot be added to the list of global read and terminal variables, so it is left out.


#### Problems encountered - Error message

WriteGlobalParameters.mq 4 Side:

2018.05.26 22: 34: 45.283 WriteGlobalParameters EURUSD, M1: uninit reason 0

2018.05.26 22: 34: 45.283 WriteGlobalParameters EURUSD, M1: File save error

2018.05.26 22: 34: 45.282 WriteGlobalParameters EURUSD, M1: Error creating pointer


ReadGlobalParameters.mq 4 Side:

2018.05.26 22: 43: 08.397 ReadGlobalParameters EURUSD, M1: unclear reason 0

2018.05.26 22: 43: 08.397 ReadGlobalParameters EURUSD, M1: Pointer creation error


* From the above error message

It is clear that the intended operation fails due to flaws in the pointer specification and inadequate file operation,

Since it is not known where the problem lies in the following code, I would like to talk about this point (as well as the unclear reason 0)


```MQL4/MQL5.

WriteGlobalParameters.mq4>

//+------------------------------------------------------------------+

//| WriteGlobalParameters.mq4 |

//| Copyright 2018, MetaQuotes Software Corp. | |

//| https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#include <Arrays\List.mqh>

#include <CGlobalVar.mqh>

#include <CGlobalVarList.mqh>

//+------------------------------------------------------------------+

//| Script programme start function |

//+------------------------------------------------------------------+

void OnStart(){

//Your File Name

string FileName="Write";

string FileType="txt";

string file=FileName+". "+FileType;


//Pointer

CGlobalVarList *list = new CGlobalVarList;

if(list!=NULL){

Print("Pointer Create Error");

}


//Handle

int hFile;

hFile=FileOpen(file,FILE_WRITE|FILE_COMMON); //FILE_CSV|FILE_UNICODE


if(hFile>=0){

if(!list.Save(hFile)){

Print("File Save Error");

delete list;

FileClose(hFile);

}

//Close

FileClose(hFile);

}

//Release the Pointer

delete list;

}

==========================================================================

ReadGlobalParameters.mq4>

//+------------------------------------------------------------------+

//| ReadGlobalParameters.mq4 |

//| Copyright 2018, MetaQuotes Software Corp. | |

//| https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#include <Arrays\List.mqh>

#include <CGlobalVar.mqh>

#include <CGlobalVarList.mqh>

//+------------------------------------------------------------------+

//| Script programme start function |

//+------------------------------------------------------------------+

void OnStart(){

//Your File Name

string FileName="Read";

string FileType="txt";

string file=FileName+". "+FileType;


//Pointer

CGlobalVarList *list = new CGlobalVarList;

if(list!=NULL){

Print("Pointer Create Error");

}


//Handle

int hFile;

hFile=FileOpen(file,FILE_READ|FILE_COMMON); //FILE_CSV|FILE_UNICODE


if(hFile>=0){

if(!list.Load(hFile)){

Print("File Load Error");

delete list;

FileClose(hFile);

}

//Close

FileClose(hFile);

}

//Release the Pointer

delete list;

}


### I tried

起動 Enable MT 4 and register arbitrary names and numeric values (0.0 or 1.0 easily in Coco) to the global variable list on the terminal.

(Time is not set as it is automatically set when registering)


================================================== ===============

First from Write GlobalParameters.mq4 side ---


① WriteGlobalParameters.mq4 is applied to the chart with the setting ⓪.

→ If it works correctly, the file is created in the directory specified above and the global variable information must be entered.


② If you check the directory (C: \ User \ User - Name \ AppData \ Roaming \ MetaQuotes \ Terminal \ Common \ Files \),

The specified "Write.txt" was created, but the file size is 0 kilobytes.

→ When I opened it, of course, the name and value of the global variable were not entered.


================================================== ================

Next on ReadGlobalParameters.mq 4 side ---


③ I created a "Read.txt" file to read a global variable with an arbitrary name and numeric value in the same directory as 2

(We created and tested two types of names/numbers/timestamps, split (unspecified) and split tab (by specifying "\ t" in FileOpen ())))


④ Similar to ①, even if you apply ReadGlobalParameters.mq4 to the chart, the error message above and the opening of the global variables list on the terminal was also empty.


================================================== ================

※ remarks

As a precautionary measure, we did the same experiment with FILE_CSV, FILE_UNICODE, FILE_BIN and FILE_ANSI, which are set by default for the FileOpen () function, but the result did not change.

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
The Synchronized Charts script allows comparing bars of different symbols or different periods of the same symbol. Attach the script to a chart and move bars or change the scale, all opened chart will move synchronously with the current one. The bars on different charts aligned to the border according to their open time. CreateGridOrdersTune A...
 

Now Global Variable are doubles only.

wish can use CMap object with element like key:value pair.

Or use Array or list ..... Or use Array or list as Global Variable .


Or use a pointer point to a memory as Global Variable.