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