myeatrade

MQL4 Reference

MQL4 Global variables

A group of functions intended for working with global variables.

Global variables of the client terminal should not be mixed up with variables declared in the global scope of the MQL4 program.

Global variables are kept in the client terminal within 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.

Global variables of the client terminal are accessible simultaneously from all MQL4 programs launched in the client terminal.

 

GlobalVariableCheck()

bool GlobalVariableCheck(string name)

Returns TRUE if the global variable exists, otherwise, returns FALSE.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

name - Global variable name. 

Sample:

  // check variable before use
  if(!GlobalVariableCheck("g1"))
    GlobalVariableSet("g1",1);

GlobalVariableDel()

bool GlobalVariableDel(string name)

Deletes the global variable. If the function succeeds, the returned value will be TRUE, otherwise, it will be FALSE.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

name - Global variable name. 

Sample:

  // deleting of the global variable named "gvar_1"
  GlobalVariableDel("gvar_1");

GlobalVariableGet()

double GlobalVariableGet(string name)

Returns the value of an existing global variable or 0 if an error occurs.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

name - Global variable name. 

Sample:

  double v1=GlobalVariableGet("g1");
  //---- check function call result
  if(GetLastError()!=0) return(false);
  //---- continue processing

GlobalVariableName()

string GlobalVariableName(int index)

The function returns the name of a global variable by its index in the list of global variables.

To get the detailed error information, one has to call the GetLastError().

Parameters:

index - Index in the list of global variables. 
        It must exceed or be equal to 0 and be less than GlobalVariablesTotal(). 

Sample:

  int    var_total=GlobalVariablesTotal();
  string name;
  for(int i=0;i<var_total;i++)
    {
     name=GlobalVariableName(i);
     Print(i,": Global variable name - ",name);
    }

GlobalVariableSet()

datetime GlobalVariableSet(string name, double value)

Sets a new value of the global variable. If it does not exist, the system creates a new gloabl variable. If the function succeeds, the returned value will be the last access time. Otherwise, the returned value will be 0.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

name  - Global variable name. 
value - The new numeric value. 

Sample:

  //---- try to set new value
  if(GlobalVariableSet("BarsTotal",Bars)==0)
    return(false);
  //---- continue processing

GlobalVariableSetOnCondition()

bool GlobalVariableSetOnCondition(string name, 
                                  double value, 
                                  double check_value)

Sets the new value of the existing global variable if the current value equals to the third parameter check_value. If there is no global variable, the function will generate error ERR_GLOBAL_VARIABLE_NOT_FOUND (4058) and return FALSE. When successfully executed, the function returns TRUE, otherwise, it returns FALSE.

To get the detailed error information, one has to call the GetLastError() function.

If the current value of the global variable differs from the check_value, the function will return FALSE.

The function provides atomic access to the global variable, this is why it can be used for providing of a semaphore at interaction of several experts working simultaneously within one client terminal.

Parameters:

name        - Global variable name. 
value       - New value. 
check_value - Value to be compared to the current global variable value. 

Sample:

  int init()
    {
     //---- create global variable
     GlobalVariableSet("DATAFILE_SEM",0);
     //...
    }
  
  int start()
    {
     //---- try to lock common resource
     while(!IsStopped())
       {
        //---- locking
        if(GlobalVariableSetOnCondition("DATAFILE_SEM",1,0)==true)  break;
        //---- may the variable be deleted?
        if(GetLastError()==ERR_GLOBAL_VARIABLE_NOT_FOUND) return(0);
        //---- sleeping
        Sleep(500);
       }
     //---- resource locked
     // ... do some work
     //---- unlock resource
     GlobalVariableSet("DATAFILE_SEM",0);
    }

GlobalVariablesDeleteAll()

int GlobalVariableSet(string prefix_name=NULL)

Deletes global variables. If the name prefix is not specified, all global variables will be deleted. Otherwise, only those variables will be deleted, the names of which begin with the specified prefix. The function returns the count of deleted variables.

Parameters:

prefix_name - Name prefix of the global variables to be deleted. 

Sample:

Print(GlobalVariablesDeleteAll("test_")," test globals deleted");

GlobalVariablesTotal()

int GlobalVariablesTotal()

The function returns the total count of global variables.

Sample:

Print(GlobalVariablesTotal()," global variables detected");
This article url: http://www.myeatrade.com/446/

Please login to post comment, if you haven't account you can register free