GlobalVariableGet

Restituisce il valore di una variabile globale esistente del terminale client. Ci sono due varianti della funzione.

1. Restituisce immediatamente il valore della variabile globale.

double  GlobalVariableGet(
   string  name      // Nome della variabile globale
   );

 
2. Restituisce true o false a seconda del successo dell'esecuzione della funzione. In caso di successo, la variabile globale del terminale client è posta in una variabile passata per riferimento nel secondo parametro.

bool  GlobalVariableGet(
   string  name,              // Nome variabile globale
   double& double_var         // Questa variabile conterrà il valore della variabile globale
   );

Parametri

name

[in]  Nome della variabile globale.

double_var

[out] Variabile target di tipo double, che accetta il valore memorizzato in una variabile globale del terminale client.

Valore restituito

Il valore della variabile globale esistente oppure 0 in caso di errore. Per ulteriori informazioni sull'errore, chiamare GetLastError().

Nota

Le variabili globali presenti nel terminale client per 4 settimane dopo il loro ultimo uso, vengono dunque cancellati automaticamente.

 

Esempio:

#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()
  {
//--- impostare il valore della variabile globale del terminale client denominata GV_NAME
   if(!GlobalVariableSetValue(GV_NAMEGV_VALUE))
      return;
     
//--- ottenere il valore reale della variabile globale del terminale client con il nome GV_NAME
//--- poiché quando si utilizza la prima forma di chiamata GlobalVariableGet, zero è un segnale di errore,
//--- è necessario analizzare l'ultimo codice di errore durante la lettura del risultato
   double dvalue=GlobalVariableGet(GV_NAME);
   if(dvalue==0 && GetLastError()!=0)
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- mostrare il risultato ottenuto
   PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f"dvalue);
   
//--- impostare il valore zero alla variabile globale del terminale client denominata GV_NAME
   if(!GlobalVariableSetValue(GV_NAME0))
      return;
     
//--- utilizzando la prima forma della chiamata otteniamo il valore Booleano della variabile globale del terminale client con il nome GV_NAME
   bool bvalue=GlobalVariableGet(GV_NAME);
   if(!bvalue && GetLastError()!=0)
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- mostrare il risultato ottenuto
   PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s"bvalue, (string)bvalue);
     
//--- impostare il valore diverso da zero alla variabile globale del terminale client denominata GV_NAME
   if(!GlobalVariableSetValue(GV_NAMEGV_VALUE*100.0))
      return;
   
//--- leggere nuovamente il valore Booleano della variabile globale del terminale client denominata GV_NAME
   bvalue=GlobalVariableGet(GV_NAME);
   if(!bvalue && GetLastError()!=0)
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- mostrare il risultato ottenuto
   PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s"bvalue, (string)bvalue);
     
//--- ottenere il valore reale della variabile globale del terminale client con il nome GV_NAME utilizzando la seconda forma della chiamata GlobalVariableGet
   if(!GlobalVariableGet(GV_NAMEdvalue))
     {
      Print("GlobalVariableGet() failed. Error "GetLastError());
      return;
     }
//--- convertire il valore reale risultante in un numero intero di tipo long e mostrare il risultato
   long lvalue=(long)dvalue;
   PrintFormat("The second form of the GlobalVariableGet() function call returned the value %.2f with type long as %I64d"dvaluelvalue);
   
//--- eliminare la variabile globale del terminale client denominata GV_NAME dopo l'uso
   if(!GlobalVariableDel(GV_NAME))
     {
      Print("GlobalVariableDel() failed. Error ",GetLastError());
     }
   
   /*
   risultato:
   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
   */
  }
//+------------------------------------------------------------------+
//| Impostare il valore della variabile globale del terminale.       |
//| Se non c'è alcuna variabile, crearla                             |
//+------------------------------------------------------------------+
bool GlobalVariableSetValue(const string gv_nameconst double value)
  {
   if(GlobalVariableSet(gv_namevalue)==0)
     {
      Print("GlobalVariableSet() failed. Error ",GetLastError());
      return(false);
     }
   return(true);
  }