GlobalVariableTime

Devuelve la hora del último acceso a una variable global.

datetime  GlobalVariableTime(
   string  name      // nombre
   );

Parámetros

name

[in]  Nombre de la variable global.

Valor devuelto

Devuelve la hora del último acceso a la variable global especificada. El direccionamiento a la variable por su valor, por ejemplo, usando las funciones GlobalVariableGet() y GlobalVariableCheck(), también cambia la hora del último acceso. Para obtener la información sobre el error, hay que llamar a la función GetLastError().

Nota

Las variables globales se guardan en el terminal de cliente durante 4 semanas desde el último acceso, luego se eliminan automáticamente.

 

Ejemplo:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   GV_NAME    "TestGlobalVariableTime"
#define   GV_TOTAL   5
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- eliminamos las variables globales de terminal de cliente con el prefijo GV_NAME creadas previamente para la prueba
   GlobalVariablesDeleteAll(GV_NAME);
   
//--- creamos variables globales del terminal del cliente en una cantidad GV_TOTAL
//--- con el prefijo GV_NAME con una pausa de 5 seg. entre la creación de cada uno
   for(int i=0i<GV_TOTALi++)
     {
      string name=GV_NAME+"_"+(string)i;
      ulong value=GetMicrosecondCount();
      ResetLastError();
      datetime time=GlobalVariableSet(name, (double)value);
      if(time==0)
        {
         Print("GlobalVariableSet() failed. Error "GetLastError());
         continue;
        }
      Sleep(5000);
      PrintFormat("GlobalVariableSet(%s, %.0f). Create time: %s"namevalueTimeToString(time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
     }
     
//--- esperamos un par de segundos e imprimimos en el log la hora de creación de las variables globales del terminal con el prefijo GV_NAME
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("Creation time");
   
//--- esperamos un par de segundos más e imprimimos en el log la hora del último acceso a las variables globales del terminal con el prefijo GV_NAME
//--- vemos que el momento del último acceso a cada variable es igual al momento de su creación
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("Last access time");
 
//--- ahora solicitamos el valor de cada una de las variables creadas
   Print(""); 
   int total=GlobalVariablesTotal();
   for(int i=0i<totali++)
     {
      string name=GlobalVariableName(i);
      if(GetLastError()!=0)
        {
         PrintFormat("Error %d occurred while getting global variable name at index %d"GetLastError(), i);
         ResetLastError();
         continue;
        }
      if(StringFind(nameGV_NAME)==WRONG_VALUE)
         continue;
         
      double value=GlobalVariableGet(name);
      if(GetLastError()!=0)
        {
         PrintFormat("Error %d occurred while getting global variable value at index %d"GetLastError(), i);
         ResetLastError();
         continue;
        }
      PrintFormat("Value of global variable named \"%s\": %.0f"namevalue);
     }
   
//--- esperamos un par de segundos más e imprimimos en el log la hora del último acceso a las variables globales del terminal con el prefijo GV_NAME
//--- vemos que ahora el momento del último acceso a cada variable es igual al momento de la solicitud de su valor
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("After getting value, the last access time");
 
//--- eliminamos las variables globales de terminal de cliente con el prefijo GV_NAME creadas para la prueba
   GlobalVariablesDeleteAll(GV_NAME);
   /*
   resultado:
   GlobalVariableSet(TestGlobalVariableTime_03987). Create time2024.11.28 22:00:39
   GlobalVariableSet(TestGlobalVariableTime_15012302). Create time2024.11.28 22:00:44
   GlobalVariableSet(TestGlobalVariableTime_210034365). Create time2024.11.28 22:00:49
   GlobalVariableSet(TestGlobalVariableTime_315045008). Create time2024.11.28 22:00:54
   GlobalVariableSet(TestGlobalVariableTime_420060340). Create time2024.11.28 22:00:59
   
   Creation time of global variable named "TestGlobalVariableTime_0"2024.11.28 22:00:39
   Creation time of global variable named "TestGlobalVariableTime_1"2024.11.28 22:00:44
   Creation time of global variable named "TestGlobalVariableTime_2"2024.11.28 22:00:49
   Creation time of global variable named "TestGlobalVariableTime_3"2024.11.28 22:00:54
   Creation time of global variable named "TestGlobalVariableTime_4"2024.11.28 22:00:59
   
   Last access time of global variable named "TestGlobalVariableTime_0"2024.11.28 22:00:39
   Last access time of global variable named "TestGlobalVariableTime_1"2024.11.28 22:00:44
   Last access time of global variable named "TestGlobalVariableTime_2"2024.11.28 22:00:49
   Last access time of global variable named "TestGlobalVariableTime_3"2024.11.28 22:00:54
   Last access time of global variable named "TestGlobalVariableTime_4"2024.11.28 22:00:59
   
   Value of global variable named "TestGlobalVariableTime_0"3987
   Value of global variable named "TestGlobalVariableTime_1"5012302
   Value of global variable named "TestGlobalVariableTime_2"10034365
   Value of global variable named "TestGlobalVariableTime_3"15045008
   Value of global variable named "TestGlobalVariableTime_4"20060340
   
   After getting valuethe last access time of global variable named "TestGlobalVariableTime_0"2024.11.28 22:01:08
   After getting valuethe last access time of global variable named "TestGlobalVariableTime_1"2024.11.28 22:01:08
   After getting valuethe last access time of global variable named "TestGlobalVariableTime_2"2024.11.28 22:01:08
   After getting valuethe last access time of global variable named "TestGlobalVariableTime_3"2024.11.28 22:01:08
   After getting valuethe last access time of global variable named "TestGlobalVariableTime_4"2024.11.28 22:01:08
   */
  }
//+------------------------------------------------------------------+
//| Imprime en el registro la última hora de acceso                  |
//| a las variables globales del terminal de cliente                 |
//+------------------------------------------------------------------+
void GlobalVariableTimePrint(const string reason)
  {
   int total=GlobalVariablesTotal();
   for(int i=0;i<total;i++)
     {
      string name=GlobalVariableName(i);
      if(GetLastError()!=0)
        {
         PrintFormat("Error %d occurred while getting global variable name at index %d"GetLastError(), i);
         ResetLastError();
         continue;
        }
      datetime time=GlobalVariableTime(name);
      if(GetLastError()!=0)
        {
         PrintFormat("Error %d occurred while getting global variable time at index %d"GetLastError(), i);
         ResetLastError();
         continue;
        }
      PrintFormat("%s of global variable named \"%s\": %s"reasonnameTimeToString(time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
     }
  }

Véase también

GlobalVariableCheck()