CalendarValueLast
CalendarValueLast
通过指定change_id和根据国家和/或货币进行排序的能力,从日历数据库状态获得所有事件值数组。
int CalendarValueLast(
ulong& change_id, // 更改ID
MqlCalendarValue& values[], // 值描述数组
const string country_code=NULL, // 国家代码名称(ISO 3166-1 alpha-2)
const string currency=NULL // 国家货币代码名称
);参数
- change_id
[in][out] 更改ID。
- values[]
[out] MqlCalendarValue类型数组,用于接收事件值。
- country_code=NULL
[in] 国家代码名称(ISO 3166-1 alpha-2)
- currency=NULL
[in] 国家货币代码名称。
返回值
接收事件值的数量。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
- 4001 ERR_INTERNAL_ERROR(一般运行时错误)。
- 4004 ERR_NOT_ENOUGH_MEMORY(内存不足,无法执行请求)。
- 5401 ERR_CALENDAR_TIMEOUT(超过请求时限)。
- 5400 ERR_CALENDAR_MORE_DATA(数组大小不足以接收所有值的描述,只可以接收那些适合的值)。
- 执行ArrayResize()失败的错误
注意
所有用于经济日历的函数都使用交易服务器时间(TimeTradeServer)。这意味着MqlCalendarValue结构中的时间和CalendarValueHistoryByEvent/CalendarValueHistory函数中输入的时间都在交易服务器时区中设置,而不是在用户本地时间设置。
如果固定长度的events[]数组被传递到函数且没有足够空间保存整个结果,则会激活ERR_CALENDAR_MORE_DATA (5400)错误。
如果change_id = 0被传递到该函数,那么您将获得这个参数日历数据库的当前change_id;该函数返回0
对于country_code和currency 过滤器,NULL和"“值是相同的,表示没有过滤器。
对于country_code,MqlCalendarCountry结构的code字段,例如应使用 “US”、“RU"或"EU”。
对于currency,MqlCalendarCountry结构的currency字段,例如应使用"USD”、“RUB"或"EUR”。
过滤器是通过连接使用的,即合理’AND’仅用于选择同时满足两个条件(国家和货币)的事件值
该函数返回指定新闻的数组和可用于函数后续调用来接收新闻新值的新change_id。因此,可以通过最后已知的change_id调用这个函数来更新指定新闻的值。
MqlCalendarValue结构提供了检查和设置actual_value、forecast_value、prev_value和revised_prev_value字段值的方法。如果没有指定任何值,则该字段存储LONG_MIN (-9223372036854775808)。
请注意,存储在这些字段中的值要乘以100万。这表示当您使用函数CalendarValueById、CalendarValueHistoryByEvent、CalendarValueHistory、CalendarValueLastByEvent、CalendarValueLast接收MqlCalendarValue中的值时,您应该检查该字段值是否等于LONG_MIN;如果在字段中指定一个值,那么您应该将该值除以1,000,000以获得该值。另一种获取该值的方法是使用MqlCalendarValue结构的函数检查和获取值。
监听经济日历事件的示例EA:
#property description "Example of using the CalendarValueLast function"
#property description " to develop the economic calendar events listener."
#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;
//--- 事件值数组
MqlCalendarValue values[];
//--- 执行初始化 - 获得当前calendar_change_id
if(first)
{
//--- get the Calendar database change ID
if(CalendarValueLast(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 events in CalendarValueLast. Error code: %d",
__FUNCTION__,error_code);
//--- 操作完成失败,请在下一次调用计时器时重新初始化
return;
}
}
}
//--- 我们有“日历”更改ID(change_id)的最后知道的值
ulong old_change_id=calendar_change_id;
//--- 检查是否有新的“日历”事件
if(CalendarValueLast(calendar_change_id,values)>0)
{
PrintFormat("%s: Received new Calendar events: %d",
__FUNCTION__,ArraySize(values));
//--- 在“日志”中显示'value'数组的数据
ArrayPrint(values);
//--- 在“日志”中显示之前和新的“日历ID”的值
PrintFormat("%s: Previous change_id=%d, new change_id=%d",
__FUNCTION__,old_change_id,calendar_change_id);
//--- 在“日志”中显示新事件
ArrayPrint(values);
/*
编写您将在这里处理所发生事件的代码
*/
}
//---
}
/*
监听操作示例:
OnTimer:已接收的“日历数据库”,当前ID:change_id=33281792
OnTimer:已接收的“日历”新事件:1
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 91040 76020013 2019.03.20 15:30:00 1970.01.01 00:00:00 0 -5077000 -1913000 -9223372036854775808 -4077000 2 0
OnTimer: Previous change_id=33281792, new change_id=33282048
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 91040 76020013 2019.03.20 15:30:00 1970.01.01 00:00:00 0 -5077000 -1913000 -9223372036854775808 -4077000 2 0
OnTimer:已接收的“日历”新事件:1
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 91041 76020013 2019.03.27 15:30:00 1970.01.01 00:00:00 0 -9223372036854775808 -5077000 -9223372036854775808 -7292000 0 0
OnTimer: Previous change_id=33282048, new change_id=33282560
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 91041 76020013 2019.03.27 15:30:00 1970.01.01 00:00:00 0 -9223372036854775808 -5077000 -9223372036854775808 -7292000 0 0
*/另见
CalendarValueLast, CalendarValueHistory, CalendarValueHistoryByEvent, CalendarValueById