GlobalVariableTime

当全局变量最后访问时返回时间。

datetime  GlobalVariableTime(
   string  name      // 名称
   );

参量

name

[in]  全局变量名称。

返回值

函数返回上次访问的指定全局变量,调用变量获得值也需要考虑是否访问,为了获得错误细节,调用 GetLastError() 函数。

注释

全局变量在客户端保存自最后使用的四周,然后自动删除。

 

示例:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   GV_NAME    "TestGlobalVariableTime"
#define   GV_TOTAL   5
 
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 删除具有为测试创建的 GV_NAME 前缀的客户端的全局变量
   GlobalVariablesDeleteAll(GV_NAME);
   
//--- 创建 GV_TOTAL 数量的客户端全局变量
//--- 使用 GV_NAME 前缀,每个前缀创建之间有 5 秒的暂停
   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));
     }
     
//--- 等待几秒钟,在日志中打印出创建带有 GV_NAME 前缀的终端全局变量的时间
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("Creation time");
   
//--- 再等几秒钟,在日志中打印最后一次访问带有 GV_NAME 前缀的终端全局变量的时间
//--- 我们可以看到,最后一次访问每个变量的时间等于它的创建时间
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("Last access time");
 
//--- 现在请求每个创建的变量的值
   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);
     }
   
//--- 再等几秒钟,在日志中打印最后一次访问带有 GV_NAME 前缀的终端全局变量的时间
//--- 我们可以看到,最后一次访问每个变量的时间等于请求其值的时间
   Sleep(2000);
   Print(""); 
   GlobalVariableTimePrint("After getting value, the last access time");
 
//--- 删除为测试创建的带有 GV_NAME 前缀的客户端的所有全局变量
   GlobalVariablesDeleteAll(GV_NAME);
   /*
   结果:
   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
   */
  }
//+------------------------------------------------------------------+
//| 在日志中打印客户终端的                                              |
//| 全局变量最后访问的时间                                              |
//+------------------------------------------------------------------+
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));
     }
  }

另见

GlobalVariableCheck()