#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_NAME, GV_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_NAME, 0))
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_NAME, GV_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_NAME, dvalue))
{
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", dvalue, lvalue);
//--- 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_name, const double value)
{
if(GlobalVariableSet(gv_name, value)==0)
{
Print("GlobalVariableSet() failed. Error ",GetLastError());
return(false);
}
return(true);
}
|