GlobalVariableGet

Devuelve el valor de una variable global ya existente del terminal de cliente. Existen 2 variantes de la función.

1. Devuelve el valor de la propiedad directamente.

double  GlobalVariableGet(
   string  name      // nombre
   );

 
2. Devuelve true o false dependiendo del éxito de ejecución de la función. En caso del éxito el valor de una variable global se coloca en una variable receptora que es pasada por referencia por el segundo parámetro.

bool  GlobalVariableGet(
   string  name               // nombre
   double& double_var         // aquí recibimos el valor de la variable global
   );

Parámetros

name

[in]  Nombre de la variable global.

double_var

[out]  Variable del tipo double que recibe el valor guardado en una variable global del terminal de cliente.

Valor devuelto

El valor de la variable global existente o 0 en caso del error. Para obtener la información sobre el error hay que llamar a la función GetLastError().

Nota

Las variables globales se guardan en el terminal de cliente durante 4 semanas desde el último acceso, luego se eliminan automáticamente.

 

Ejemplo:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   GV_NAME    "TestGlobalVariableGet"
#define   GV_VALUE   1.23
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- establecemos el valor en la variable global de terminal de cliente denominada GV_NAME
   if(!GlobalVariableSetValue(GV_NAMEGV_VALUE))
      return;
     
//--- obtenemos el valor real de la variable global de terminal de cliente con el nombre GV_NAME
//--- dado que al utilizar la primera forma de llamada GlobalVariableGet, cero es una señal de error,
//--- al leer el resultado, deberemos analizar el último código de error
   double dvalue=GlobalVariableGet(GV_NAME);
   if(dvalue==0 && GetLastError()!=0)
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- mostramos el resultado obtenido
   PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f"dvalue);
   
//--- establecemos en cero la variable global de terminal de cliente denominada GV_NAME
   if(!GlobalVariableSetValue(GV_NAME0))
      return;
     
//--- usando la primera forma de llamada, obtenemos el valor booleano de la variable global de terminal de cliente con el nombre GV_NAME
   bool bvalue=GlobalVariableGet(GV_NAME);
   if(!bvalue && GetLastError()!=0)
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- mostramos el resultado obtenido
   PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s"bvalue, (string)bvalue);
     
//--- establecemos en un valor distinto a cero la variable global de terminal de cliente denominada GV_NAME
   if(!GlobalVariableSetValue(GV_NAMEGV_VALUE*100.0))
      return;
   
//--- leemos nuevamente el valor booleano de la variable de terminal de cliente global denominada GV_NAME
   bvalue=GlobalVariableGet(GV_NAME);
   if(!bvalue && GetLastError()!=0)
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- mostramos el resultado obtenido
   PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s"bvalue, (string)bvalue);
     
//--- obtenemos el valor real de la variable global de terminal de cliente con el nombre GV_NAME utilizando la segunda forma de llamada GlobalVariableGet
   if(!GlobalVariableGet(GV_NAMEdvalue))
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- convertimos el valor real resultante en un valor entero de tipo long y mostramos el resultado
   long lvalue=(long)dvalue;
   PrintFormat("The second form of the GlobalVariableGet() function call returned the value %.2f with type long as %I64d"dvaluelvalue);
   
//--- eliminamos la variable de terminal de cliente global denominada GV_NAME después de su uso
   if(!GlobalVariableDel(GV_NAME))
     {
      Print("GlobalVariableDel() failed. Error ",GetLastError());
     }
   
   /*
   resultado:
   The first form of the GlobalVariableGet() function call returned the value 1.23
   The first form of the GlobalVariableGet() function call returned the value 0.00 with type bool as false
   The first form of the GlobalVariableGet() function call returned the value 1.00 with type bool as true
   The second form of the GlobalVariableGet() function call returned the value 123.00 with type long as 123
   */
  }
//+------------------------------------------------------------------+
//| Establece el valor de una variable terminal global.              |
//| Si no existe la variable, la creará                              |
//+------------------------------------------------------------------+
bool GlobalVariableSetValue(const string gv_nameconst double value)
  {
   if(GlobalVariableSet(gv_namevalue)==0)
     {
      Print("GlobalVariableSet() failed. Error ",GetLastError());
      return(false);
     }
   return(true);
  }