//+------------------------------------------------------------------+
//| OnTimer_Sample.mq5 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "Example of using the timer for calculating the trading server time"
#property description "It is recommended to run the EA at the end of a trading week before the weekend"
//+------------------------------------------------------------------+
//| エキスパート初期化関数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 1秒の周期でタイマーを作成する
EventSetTimer(1);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| エキスパート初期化解除に使用される関数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- 作業終了後にタイマーを破壊する
EventKillTimer();
}
//+------------------------------------------------------------------+
//| エキスパートティック関数 |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer 関数 |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- OnTimer()の最初の呼び出しの時刻
static datetime start_time=TimeCurrent();
//--- 最初のOnTimer() 呼び出し中の取引サーバ時刻
static datetime start_tradeserver_time=0;
//--- 計算された取引サーバ時刻
static datetime calculated_server_time=0;
//--- ローカルPC時刻
datetime local_time=TimeLocal();
//--- 現在の推測取引サーバ時刻
datetime trade_server_time=TimeTradeServer();
//--- 何らかの理由でサーバの時刻が不明な場合は、先に終了する
if(trade_server_time==0)
return;
//--- 取引サーバ初期値がまだ設定されていない場合
if(start_tradeserver_time==0)
{
start_tradeserver_time=trade_server_time;
//--- 取引サーバの計算された現在時刻を設定する
Print(trade_server_time);
calculated_server_time=trade_server_time;
}
else
{
//--- OnTimer()の初めの呼び出しの時刻を増やす
if(start_tradeserver_time!=0)
calculated_server_time=calculated_server_time+1;;
}
//---
string com=StringFormat(" Start time: %s\r\n",TimeToString(start_time,TIME_MINUTES|TIME_SECONDS));
com=com+StringFormat(" Local time: %s\r\n",TimeToString(local_time,TIME_MINUTES|TIME_SECONDS));
com=com+StringFormat("TimeTradeServer time: %s\r\n",TimeToString(trade_server_time,TIME_MINUTES|TIME_SECONDS));
com=com+StringFormat(" EstimatedServer time: %s\r\n",TimeToString(calculated_server_time,TIME_MINUTES|TIME_SECONDS));
//--- チャート上のすべてのカウンタの値を表示する
Comment(com);
}
|