#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define GV_NAME "TestGlobalVariableTemp"
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
double value=0; // we will receive the values of the global variable here
//--- 如果客户端还没有临时全局变量,则:
//--- 1. 要么程序尚未启动,
//--- 2. 或者在程序运行的情况下重新启动了终端
if(!GlobalVariableCheck(GV_NAME))
{
//--- 为客户端创建一个新的临时全局变量
if(!GlobalVariableTemp(GV_NAME))
{
Print("GlobalVariableTemp() failed. Error ", GetLastError());
return;
}
//--- 在全局变量中设置当前日期和时间
if(!GlobalVariableSet(GV_NAME,(double)TimeCurrent()))
{
Print("GlobalVariableSet() failed. Error ", GetLastError());
return;
}
//--- 获取临时全局变量的值,并在日志中显示第一次程序启动或终端重启的时间
if(!GlobalVariableGet(GV_NAME,value))
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
Print("First start or starting the program after rebooting the terminal at ", TimeToString((datetime)value,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
}
//--- 如果客户端的临时全局变量已经创建,则这是程序重新启动
else
{
//--- 在全局变量中设置当前日期和时间
if(!GlobalVariableSet(GV_NAME,(double)TimeCurrent()))
{
Print("GlobalVariableSet() failed. Error ", GetLastError());
return;
}
//--- 获取临时全局变量的值,并在日志中打印程序重启时间
if(!GlobalVariableGet(GV_NAME,value))
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
Print("Restarting the program at ", TimeToString((datetime)value, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
}
/*
在第一次启动期间或重新启动终端的结果:
First start or starting the program after rebooting the terminal at 2024.11.29 15:03:18
连续几次程序重启的结果:
Restarting the program at 2024.11.29 15:03:25
Restarting the program at 2024.11.29 15:03:33
Restarting the program at 2024.11.29 15:03:45
*/
}
|