GlobalVariableTime

グローバル変数の最終アクセス時刻を返します。

datetime  GlobalVariableTime(
  string  name      // 名称
  );

パラメータ

name

[in]  グローバル変数名

戻り値

指定されたグローバル変数の最終アクセス時刻。GlobalVariableGet() 及び GlobalVariableCheck() 関数などを使用して変数を値によって参照する場合にもアクセス時刻が変更されます。エラーの詳細を取得するには GetLastError() 関数が呼ばれます。

注意事項

グローバル変数は、最終アクセス後4週間クライアント端末に保存されます。その後、自動的に削除されます。

 

例:

#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);
 
//--- 各作成間に5秒の待機を入れて、GV_NAME接頭辞付きのクライアント端末グローバル変数を
//--- GV_TOTAL個作成する
  for(int i=0; i<GV_TOTAL; i++)
    {
    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", name, value, TimeToString(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=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;
       }
    if(StringFind(name, GV_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", name, value);
    }
 
//--- さらに数秒待機して、GV_NAME接頭辞付き端末グローバル変数の最終アクセス時刻を操作ログに出力する
//--- これにより、各変数への最終アクセス時刻が値のリクエスト時刻と同じであることが確認できる
  Sleep(2000);
  Print("");
  GlobalVariableTimePrint("After getting value, the last access time");
 
//--- テスト用に作成されたGV_NAME接頭辞を持つクライアント端末のグローバル変数を削除する
  GlobalVariablesDeleteAll(GV_NAME);
  /*
   結果:
  GlobalVariableSet(TestGlobalVariableTime_0, 3987). Create time: 2024.11.28 22:00:39
  GlobalVariableSet(TestGlobalVariableTime_1, 5012302). Create time: 2024.11.28 22:00:44
  GlobalVariableSet(TestGlobalVariableTime_2, 10034365). Create time: 2024.11.28 22:00:49
  GlobalVariableSet(TestGlobalVariableTime_3, 15045008). Create time: 2024.11.28 22:00:54
  GlobalVariableSet(TestGlobalVariableTime_4, 20060340). Create time: 2024.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 value, the last access time of global variable named "TestGlobalVariableTime_0": 2024.11.28 22:01:08
  After getting value, the last access time of global variable named "TestGlobalVariableTime_1": 2024.11.28 22:01:08
  After getting value, the last access time of global variable named "TestGlobalVariableTime_2": 2024.11.28 22:01:08
  After getting value, the last access time of global variable named "TestGlobalVariableTime_3": 2024.11.28 22:01:08
  After getting value, the 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", reason, name, TimeToString(time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
    }
 }

参照

GlobalVariableCheck()