GlobalVariableTime

Retorna o tempo em que a variável global foi acessado pela última vez.

datetime  GlobalVariableTime(
   string  name      // nome
   );

Parâmetros

name

[in]  Nome da variável global.

Valor do Retorno

A função retorna hora do último acesso à variável global especificada. Chamando uma variável para obter um valor também é considerado como um acesso a ele. A fim de obter os detalhes do erro chamar a função GetLastError().

Observação

As variáveis globais existem no terminal do cliente durante 4 semanas desde a última utilização. Depois disso, 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    "TestGlobalVariableTime"
#define   GV_TOTAL   5
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- vamos excluir as variáveis globais do terminal de cliente criadas para teste com o prefixo GV_NAME
   GlobalVariablesDeleteAll(GV_NAME);
   
//--- vamos criar variáveis globais do terminal de cliente na quantidade GV_TOTAL
//--- com o prefixo GV_NAME com uma pausa de 5 s. entre a criação de cada uma
   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));
     }
     
//--- vamos aguardar alguns segundos e imprimir no diário o horário de criação das variáveis globais do terminal com o prefixo GV_NAME
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("Creation time");
   
//--- vamos aguardar mais alguns segundos e imprimir no diário o horário do último acesso às variáveis globais do terminal com o prefixo GV_NAME
//--- veremos que o horário do último acesso a cada variável é igual ao horário de sua criação
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("Last access time");
 
//--- agora para cada variável criada solicitaremos seu valor
   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);
     }
   
//--- vamos aguardar mais alguns segundos e imprimir no diário o horário do último acesso às variáveis globais do terminal com o prefixo GV_NAME
//--- veremos que agora o horário do último acesso a cada variável é igual ao horário da solicitação do seu valor
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("After getting value, the last access time");
 
//--- vamos excluir todas as variáveis globais do terminal de cliente criadas para teste com o prefixo GV_NAME
   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 no diário o horário do último acesso                     |
//| às variáveis globais do 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));
     }
  }

Também Veja

GlobalVariableCheck()