OnTimer

Timer事件在固定时间间隔内由程序端生成期间,在EA中调用这个函数。

void  OnTimer(void);

返回值

无返回值

注意

Timer事件是由客户端为EA定期生成,使用EventSetTimer()函数来激活计时器。通常,这个函数在OnInit()函数中调用。EA停止工作时,应使用通常在OnDeinit()函数中被调用的EventKillTimer()消除计时器。

每个EA和每个指标都使用其自己的计时器,并从该计时器单独接收事件。mql5程序关闭期间,如果计时器已经创建但没有被EventKillTimer()函数禁用,那么计时器则被强制销毁。

如果您不满足于每秒一次,需要更频繁地接收计时器事件,请使用EventSetMillisecondTimer()来创建高分辨率计时器。

一般来说,当计时器周期减少时,测试时间会增加,因为计时器事件的处理程序会更频繁地被调用。在实时模式下工作时,由于硬件的限制,计时器事件在10-16毫秒内生成不超过1次。

每个程序只能启动一个计时器。每个mql5应用程序和图表都有其自己的事件队列,所有新到达的事件也都包括其中。如果这个队列已经包括Timer事件,或者这个事件正在处理阶段,那么新的Timer事件就不能添加到mql5应用程序队列中。

EA样例,OnTimer()处理程序

//+------------------------------------------------------------------+
//|                                               OnTimer_Sample.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#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"
//+------------------------------------------------------------------+
//| EA交易初始化函数                                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 创建一个1秒周期的计时器
   EventSetTimer(1);
 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| EA交易去初始化函数                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- 完成工作后销毁计时器
   EventKillTimer();
 
  }
//+------------------------------------------------------------------+
//| EA报价函数                                                        |
//+------------------------------------------------------------------+
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);
  }

另见

EventSetTimerEventSetMillisecondTimerEventKillTimerGetTickCountGetMicrosecondCount客户端事件