GlobalVariableGet

Retorna o valor de uma variável global existente do terminal do cliente. Existem 2 variantes da função.

1. Imediatamente retorna o valor da variável global.

double  GlobalVariableGet(
   string  name      // Nome da variável global
   );

 
2. Retorna true ou false dependendo do sucesso da execução da função. Se for bem sucedido, a variável global do terminal do cliente é colocado em uma variável passada por referência no segundo parâmetro.

bool  GlobalVariableGet(
   string  name,              // Nome da variável global
   double& double_var         // Esta variável irá conter o valor da variável global
   );

Parâmetros

name

[in]  Nome da variável global.

double_var

[out]  Alvo variável do tipo duplo, que aceita o valor armazenado em uma variável global do terminal do cliente.

Valor do Retorno

O valor da variável global existente ou 0(zero) em caso de um erro. Para mais detalhes sobre o erro, chamar GetLastError().

Observação

As variáveis globais existem no terminal do cliente durante 4 semanas desde a última utilização, então eles são excluídos automaticamente.

 

Exemplo:

#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_NAMEGV_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_NAME0))
      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_NAMEGV_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_NAMEdvalue))
     {
      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"dvaluelvalue);
   
//--- 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_nameconst double value)
  {
   if(GlobalVariableSet(gv_namevalue)==0)
     {
      Print("GlobalVariableSet() failed. Error ",GetLastError());
      return(false);
     }
   return(true);
  }