CalendarValueLastByEvent

通过指定change_id,从日历数据库状态根据ID获得事件值数组。

int  CalendarValueLastByEvent(
   ulong                event_id,      // 事件ID 
   ulong&               change_id,     // 事件值ID 
   MqlCalendarValue&    values[]       // 值描述数组
   );

参数

event_id

[in]  事件ID。

change_id

[in][out]  更改ID。

values[]

[out] MqlCalendarValue类型数组,用于接收事件值。

返回值

接收事件值的数量。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:

  • 4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
  • 4004 – ERR_NOT_ENOUGH_MEMORY(内存不足,无法执行请求)。
  • 5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
  • 5400 – ERR_CALENDAR_MORE_DATA(数组大小不足以接收所有值的描述,只可以接收那些适合的值)。

注意

所有用于经济日历的函数都使用交易服务器时间(TimeTradeServer)。这意味着MqlCalendarValue结构中的时间和CalendarValueHistoryByEvent/CalendarValueHistory函数中输入的时间都在交易服务器时区中设置,而不是在用户本地时间设置。

如果固定长度的events[]数组被传递到函数且没有足够空间保存整个结果,则会激活ERR_CALENDAR_MORE_DATA (5400)错误。

如果change_id = 0被传递到该函数,那么该函数会始终返回零值,但当前日历数据库返回到change_id

该函数返回指定新闻的数组和可用于函数后续调用来接收新闻新值的新change_id。因此,可以通过最后已知的change_id调用这个函数来更新指定新闻的值。

MqlCalendarValue结构提供了检查和设置actual_value、forecast_value、prev_value和revised_prev_value字段值的方法。如果没有指定任何值,则该字段存储LONG_MIN (-9223372036854775808)。

请注意,存储在这些字段中的值要乘以100万。这表示当您使用函数CalendarValueByIdCalendarValueHistoryByEventCalendarValueHistoryCalendarValueLastByEventCalendarValueLast接收MqlCalendarValue中的值时,您应该检查该字段值是否等于LONG_MIN;如果在字段中指定一个值,那么您应该将该值除以1,000,000以获得该值。另一种获取该值的方法是使用MqlCalendarValue结构的函数检查和获取值。

EA听取非农就业人口报告发布的样本:

#property description "Example of using the CalendarValueLastByEvent function"
#property description " for tracking the release of the Nonfarm Payrolls report."
#property description "To achieve this, get the current change ID"
#property description " of the Calendar database. Then, use this ID to receive"
#property description " only new events via the timer survey"
//+------------------------------------------------------------------+
//| EA交易初始化函数                                                    |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 创建计时器
   EventSetTimer(60);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| EA交易去初始化函数                                                   |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- 销毁计时器
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| EA报价函数                                                         |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
 
  }
//+------------------------------------------------------------------+
//| Timer函数                                                         |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- 日历数据库更改ID
   static ulong calendar_change_id=0;
//--- 第一次启动属性
   static bool first=true;
//--- 事件ID  
   static ulong event_id=0;
//--- 事件名称  
   static string event_name=NULL;
//--- 事件值数组
   MqlCalendarValue values[];
//--- 执行初始化 - 获得当前calendar_change_id
   if(first)
     {
      MqlCalendarEvent events[];
      //--- 美国国家代码(ISO 3166-1 Alpha-2) 
      string USA_code="US";
      //--- 获得美国事件
      int events_count=CalendarEventByCountry(USA_code,events);
      //--- 'events'数组中必要事件的位置
      int event_pos=-1;
      //--- 在“日志”中显示美国事件
      if(events_count>0)
        {
         PrintFormat("%s: USA events: %d",__FUNCTION__,events_count);
         for(int i=0;i<events_count;i++)
           {
            string event_name_low=events[i].name;
            //--- 将事件名称更改为小写            
            if(!StringToLower(event_name_low))
              {
               PrintFormat("StringToLower() returned %d error",GetLastError());
               //--- 提前退出函数
               return;
              }
            //--- 寻找“非农就业人口”事件            
            if(StringFind(event_name_low,"nonfarm payrolls")!=-1)
              {
               //--- 事件已找到,请记住其ID
               event_id=events[i].id;
               //--- 书写“非农就业人口”事件名称
               event_name=events[i].name;
               //--- 记住'events[]'数组中事件的位置             
               event_pos=i;
               //--- 请记住,该“日历”中包含多个名称中有“非农就业人口”的事件
               PrintFormat("Event \"Nonfarm Payrolls\" found: event_id=%d  event_name=%s",event_id,event_name);
               //--- 通过注释'break'操作符来查看所有事件,以便更好地理解这个示例
               break;
              }
           }
         //--- 通过删除“非农就业人口”之后的事件来减少列表
         ArrayRemove(events,event_pos+1);
         //--- 将9个事件放在“非农就业人口”之前,以便分析更方便         
         ArrayRemove(events,0,event_pos-9);
         ArrayPrint(events);
        }
      else
        {
         PrintFormat("%s: CalendarEventByCountry(%s) returned 0 events, error code=%d",
                     USA_code,__FUNCTION__,GetLastError());
         //--- 操作完成失败,请在下一次调用计时器时重试        
         return;
        }
 
      //--- 获得指定事件的“日历”数据库更改ID   
      if(CalendarValueLastByEvent(event_id,calendar_change_id,values)>0)
        {
         //--- 第一次启动时不可执行这个代码块,但可以添加它
         PrintFormat("%s: Received the Calendar database current ID: change_id=%d",
                     __FUNCTION__,calendar_change_id);
         //--- 设置该标识并在计时器的下一个事件之前退出
         first=false;
         return;
        }
      else
        {
         //--- 无法接收数据(对于第一次启动,这是正常的),请检查是否有错误
         int error_code=GetLastError();
         if(error_code==0)
           {
            PrintFormat("%s: Received the Calendar database current ID: change_id=%d",
                        __FUNCTION__,calendar_change_id);
            //--- 设置该标识并在计时器的下一个事件之前退出
            first=false;
            //--- 现在我们有calendar_change_id值
            return;
           }
         else
           {
            //--- 并且这确实是一个错误            
            PrintFormat("%s: Failed to get values for event_id=%d",__FUNCTION__,event_id);
            PrintFormat("Error code: %d",error_code);
            //--- 操作完成失败,请在下一次调用计时器时重试         
            return;
           }
        }
     }
 
//--- 我们有“日历”更改ID(change_id)的最后知道的值
   ulong old_change_id=calendar_change_id;
//--- 请检查新的“非农就业人口”事件值
   if(CalendarValueLastByEvent(event_id,calendar_change_id,values)>0)
     {
      PrintFormat("%s: Received new events for \"%s\": %d",
                  __FUNCTION__,event_name,ArraySize(values));
      //--- 在“日志”中显示'value'数组的数据
      ArrayPrint(values);
      //--- 在“日志”中显示之前和新的“日历ID”的值
      PrintFormat("%s: Previous change_id=%d, new change_id=%d",
                  __FUNCTION__,old_change_id,calendar_change_id);
/* 
      编写您将在这里处理“非农就业人口”数据发布的代码
      */
     }
//---     
  }
/*
  结果:
   OnTimer: USA events: 202
  Event "Nonfarm Payrolls" found: event_id=840030016  event_name=Nonfarm Payrolls
            [id] [type] [sector] [frequency] [time_mode] [country_id] [unit] [importance] [multiplier] [digits]          [source_url]                             [event_code]                   [name] [reserved]
   [0] 840030007      1        4           2           0          840      1            1            0        1 "https://www.bls.gov" "consumer-price-index-yy"                "CPI y/y"                         0
   [1] 840030008      1        4           2           0          840      1            1            0        1 "https://www.bls.gov" "consumer-price-index-ex-food-energy-yy" "Core CPI y/y"                    0
   [2] 840030009      1        4           2           0          840      0            1            0        3 "https://www.bls.gov" "consumer-price-index-nsa"               "CPI n.s.a."                      0
   [3] 840030010      1        4           2           0          840      0            1            0        3 "https://www.bls.gov" "consumer-price-index-ex-food-energy"    "Core CPI"                        0
   [4] 840030011      1        4           2           0          840      1            1            0        1 "https://www.bls.gov" "import-price-index-mm"                  "Import Price Index m/m"          0
   [5] 840030012      1        4           2           0          840      1            1            0        1 "https://www.bls.gov" "import-price-index-yy"                  "Import Price Index y/y"          0
   [6] 840030013      1        4           2           0          840      1            1            0        1 "https://www.bls.gov" "export-price-index-mm"                  "Export Price Index m/m"          0
   [7] 840030014      1        4           2           0          840      1            1            0        1 "https://www.bls.gov" "export-price-index-yy"                  "Export Price Index y/y"          0
   [8] 840030015      1        3           2           0          840      1            2            0        1 "https://www.bls.gov" "unemployment-rate"                      "Unemployment Rate"               0
   [9] 840030016      1        3           2           0          840      4            3            1        0 "https://www.bls.gov" "nonfarm-payrolls"                       "Nonfarm Payrolls"                0
   OnTimer: Received the Calendar database current ID: change_id=33986560
 
*/  

另见

CalendarValueLastCalendarValueHistoryCalendarValueHistoryByEventCalendarValueById