#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()
{
//--- vamos definir um valor na variável global do terminal de cliente com o nome GV_NAME
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE))
return;
//--- vamos obter o valor de ponto flutuante da variável global do terminal de cliente com o nome GV_NAME
//--- como ao usar a primeira forma de chamada de GlobalVariableGet zero é um indicador de erro,
//--- ao ler o resultado é necessário analisar o código do último erro
double dvalue=GlobalVariableGet(GV_NAME);
if(dvalue==0 && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- vamos mostrar o resultado obtido
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f", dvalue);
//--- vamos definir o valor zero na variável global do terminal de cliente com o nome GV_NAME
if(!GlobalVariableSetValue(GV_NAME, 0))
return;
//--- mediante a primeira forma de chamada vamos obter o valor booleano da variável global do terminal de cliente com o nome GV_NAME
bool bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- vamos mostrar o resultado obtido
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- vamos definir um valor diferente de zero na variável global do terminal de cliente com o nome GV_NAME
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE*100.0))
return;
//--- vamos ler novamente o valor booleano da variável global do terminal de cliente com o nome GV_NAME
bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- vamos mostrar o resultado obtido
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- vamos obter o valor de ponto flutuante da variável global do terminal de cliente com o nome GV_NAME usando a segunda forma de chamada de GlobalVariableGet
if(!GlobalVariableGet(GV_NAME, dvalue))
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- vamos converter o valor de ponto flutuante obtido para inteiro do tipo long e mostrar o 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);
//--- vamos excluir a variável global do terminal de cliente com o nome GV_NAME após o 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
*/
}
//+------------------------------------------------------------------+
//| Define um valor na variável global do terminal |
//| Se a variável não existir, cria-a |
//+------------------------------------------------------------------+
bool GlobalVariableSetValue(const string gv_name, const double value)
{
if(GlobalVariableSet(gv_name, value)==0)
{
Print("GlobalVariableSet() failed. Error ",GetLastError());
return(false);
}
return(true);
}
|