第十五章 经济日历函数
这部分描述了处理可在MetaTrader平台直接使用的经济日历的函数。经济日历是一个即时可用的百科全书,描述了宏观经济指标、指标发布日期以及重要程度。宏观经济指标的相关值会在发布时立即发送至MetaTrader平台,并以标签的形式显示在图表上,使您可以根据国家、货币和重要性直观地跟踪所需的指标。
所有用于经济日历的函数都使用交易服务器时间(TimeTradeServer)。这意味着MqlCalendarValue结构中的时间和CalendarValueHistoryByEvent/CalendarValueHistory函数中输入的时间都在交易服务器时区中设置,而不是在用户本地时间设置。
经济日历函数可以根据自定义重要性标准,从必要的国家/货币的角度对传入事件进行自动分析。
函数 | 功能 |
---|---|
CalendarCountryById | 根据ID获得国家描述 |
CalendarEventById | 根据ID获得事件描述 |
CalendarValueById | 根据ID获得事件值描述 |
CalendarCountries | 获得日历中可用的国家名称数组 |
CalendarEventByCountry | 根据指定国家代码获得日历中可用的所有事件描述数组 |
CalendarEventByCurrency | 根据指定货币获得日历中可用的所有事件描述数组 |
CalendarValueHistoryByEvent | 根据事件ID获得指定时期的所有事件值数组 |
CalendarValueHistory | 通过根据国家和/或货币进行排序的能力,获得指定时期的所有事件值数组 |
CalendarValueLastByEvent | 通过指定change_id,从日历数据库状态根据ID获得事件值数组 |
CalendarValueLast | 通过指定change_id和根据国家和/或货币进行排序的能力,从日历数据库状态获得所有事件值数组 |
# 15.1 CalendarCountryById
根据ID获得国家描述。
bool CalendarCountryById(
const long country_id, //国家 ID
MqlCalendarCountry& country // 用于接收国家描述的变量
);
2
3
4
参数
country_id
[in] 国家ID (ISO 3166-1)。
country
[out] MqlCalendarCountry类型变量,用于接收国家描述。
返回值
如果成功返回true,否则返回false。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
5402 – ERR_CALENDAR_NO_DATA(国家未找到)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
2
3
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 从经济日历中获得国家列表
MqlCalendarCountry countries[];
int count=CalendarCountries(countries);
//--- 检查结果
if(count==0)
PrintFormat("CalendarCountries() returned 0! Error %d",GetLastError());
//--- 如果有两个或两个以上的国家
if(count>=2)
{
MqlCalendarCountry country;
//--- 现在根据ID获得国家描述
if(CalendarCountryById(countries[1].id, country))
{
//--- 准备国家描述
string descr="id = "+IntegerToString(country.id)+"\n";
descr+=("name = " + country.name+"\n");
descr+=("code = " + country.code+"\n");
descr+=("currency = " + country.currency+"\n");
descr+=("currency_symbol = " + country.currency_symbol+"\n");
descr+=("url_name = " + country.url_name);
//--- 显示国家描述
Print(descr);
}
else
Print("CalendarCountryById() failed. Error ",GetLastError());
}
//---
}
/*
结果:
id = 999
name = European Union
code = EU
currency = EUR
currency_symbol = �
url_name = european-union
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
另见
CalendarCountries,CalendarEventByCountry
# 15.2 CalendarEventById
根据ID获得事件描述。
bool CalendarEventById(
ulong event_id, // 事件ID
MqlCalendarEvent& event // 用于接收事件描述的变量
);
2
3
4
参数
event_id
[in] 事件ID。
event
[out] MqlCalendarEvent类型变量,用于接收事件描述。
返回值
如果成功返回true,否则返回false。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
5402 – ERR_CALENDAR_NO_DATA(国家未找到)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
2
3
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 德国国家代码(ISO 3166-1 Alpha-2)
string germany_code="DE";
//--- 获得德国事件
MqlCalendarEvent events[];
int events_count=CalendarEventByCountry(germany_code,events);
//--- 在“日志”中显示德国事件
if(events_count>0)
{
PrintFormat("Germany events: %d",events_count);
ArrayPrint(events);
}
else
{
PrintFormat("Failed to receive events for the country code %s, error %d",
germany_code,GetLastError());
//--- 脚本提前完成
return;
}
//--- 从events[]数组获得最后事件的描述
MqlCalendarEvent event;
ulong event_id=events[events_count-1].id;
if(CalendarEventById(event_id,event))
{
MqlCalendarCountry country;
CalendarCountryById(event.country_id,country);
PrintFormat("Event description with event_id=%d received",event_id);
PrintFormat("Country: %s (country code = %d)",country.name,event.country_id);
PrintFormat("Event name: %s",event.name);
PrintFormat("Event code: %s",event.event_code);
PrintFormat("Event importance: %s",EnumToString((ENUM_CALENDAR_EVENT_IMPORTANCE)event.importance));
PrintFormat("Event type: %s",EnumToString((ENUM_CALENDAR_EVENT_TYPE)event.type));
PrintFormat("Event sector: %s",EnumToString((ENUM_CALENDAR_EVENT_SECTOR)event.sector));
PrintFormat("Event frequency: %s",EnumToString((ENUM_CALENDAR_EVENT_FREQUENCY)event.frequency));
PrintFormat("Event release mode: %s",EnumToString((ENUM_CALENDAR_EVENT_TIMEMODE)event.time_mode));
PrintFormat("Event measurement unit: %s",EnumToString((ENUM_CALENDAR_EVENT_UNIT)event.unit));
PrintFormat("Number of decimal places: %d",event.digits);
PrintFormat("Event multiplier: %s",EnumToString((ENUM_CALENDAR_EVENT_MULTIPLIER)event.multiplier));
PrintFormat("Source URL: %s",event.source_url);
}
else
PrintFormat("Failed to get event description for event_d=%s, error %d",
event_id,GetLastError());
}
/*
结果:
德国事件:50
[id] [type] [sector] [frequency] [time_mode] [country_id] [unit] [importance] [multiplier] [digits] [source_url] [event_code] [name] [reserved]
[ 0] 276010001 1 6 2 0 276 1 1 0 1 "https://www.destatis.de/EN/Homepage.html" "exports-mm" "Exports m/m" 0
[ 1] 276010002 1 6 2 0 276 1 1 0 1 "https://www.destatis.de/EN/Homepage.html" "imports-mm" "Imports m/m" 0
[ 2] 276010003 1 4 2 0 276 1 1 0 1 "https://www.destatis.de/EN/Homepage.html" "import-price-index-mm" "Import Price Index m/m" 0
[ 3] 276010004 1 4 2 0 276 1 1 0 1 "https://www.destatis.de/EN/Homepage.html" "import-price-index-yy" "Import Price Index y/y" 0
....
[47] 276500001 1 8 2 0 276 0 2 0 1 "https://www.markiteconomics.com" "markit-manufacturing-pmi" "Markit Manufacturing PMI" 0
[48] 276500002 1 8 2 0 276 0 2 0 1 "https://www.markiteconomics.com" "markit-services-pmi" "Markit Services PMI" 0
[49] 276500003 1 8 2 0 276 0 2 0 1 "https://www.markiteconomics.com" "markit-composite-pmi" "Markit Composite PMI" 0
接收event_id=276500003事件描述
国家:德国(国家代码= 276)
事件名称:Markit综合PMI
事件代码:markit-composite-pmi
事件重要性: CALENDAR_IMPORTANCE_MODERATE
事件类型:CALENDAR_TYPE_INDICATOR
事件版块:CALENDAR_SECTOR_BUSINESS
事件频率:CALENDAR_FREQUENCY_MONTH
事件发布模式: CALENDAR_TIMEMODE_DATETIME
事件计量单位:CALENDAR_UNIT_NONE
小数位数:1
乘数值:CALENDAR_MULTIPLIER_NONE
源URL:https://www.markiteconomics.com
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
另见
CalendarEventByCountry, CalendarEventByCurrency, CalendarValueById
# 15.3 CalendarValueById
根据ID获得事件值描述。
bool CalendarValueById(
ulong value_id, // 事件值ID
MqlCalendarValue& value // 用于接收事件值的变量
);
2
3
4
参数
value_id
[in] 事件值ID。
值
[out] MqlCalendarValue类型变量,用于接收事件描述。
返回值
如果成功返回true,否则返回false。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
5402 – ERR_CALENDAR_NO_DATA(国家未找到)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
2
3
注意
所有用于经济日历的函数都使用交易服务器时间(TimeTradeServer)。这意味着MqlCalendarValue结构中的时间和CalendarValueHistoryByEvent/CalendarValueHistory函数中输入的时间都在交易服务器时区中设置,而不是在用户本地时间设置。
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结构的函数检查和获取值。
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 日本国家代码(ISO 3166-1 Alpha-2)
string japan_code="JP";
//--- 设置事件发生间隔的边界
datetime date_from=D'01.01.2018'; // 获取2018年的所有事件
datetime date_to=0; // 0 指所有已知事件,包括尚未发生的事件
//--- 获得日本事件值的数组
MqlCalendarValue values[];
int values_count=CalendarValueHistory(values,date_from,date_to,japan_code);
//--- 沿着检测到的事件值移动
if(values_count>0)
{
PrintFormat("Number of values for Japan events: %d",values_count);
//--- 删除所有“空”值(actual_value==-9223372036854775808)
for(int i=values_count-1;i>=0;i--)
{
if(values[i].actual_value==-9223372036854775808)
ArrayRemove(values,i,1);
}
PrintFormat("Number of values after deleting empty ones: %d",ArraySize(values));
}
else
{
PrintFormat("Failed to receive events for the country code %s, error %d",
japan_code,GetLastError());
//--- 脚本提前完成
return;
}
//--- 在values[]数组中保留不超过10个值
if(ArraySize(values)>10)
{
PrintFormat("Reduce the list of values to 10 and display them");
ArrayRemove(values,0,ArraySize(values)-10);
}
ArrayPrint(values);
//--- 现在,让我们展示如何基于已知的value_id获得事件值描述
for(int i=0;i<ArraySize(values);i++)
{
MqlCalendarValue value;
CalendarValueById(values[i].id,value);
PrintFormat("%d: value_id=%d value=%d impact=%s",
i,values[i].id,value.actual_value,EnumToString(ENUM_CALENDAR_EVENT_IMPACT(value.impact_type)));
}
//---
}
/*
结果:
日本事件值的数量:1734
删除空值后的值数量:1017
将值列表减少到10并显示
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 56500 392030004 2019.03.28 23:30:00 2019.03.01 00:00:00 0 900000 600000 -9223372036854775808 500000 1 0
[1] 56501 392030005 2019.03.28 23:30:00 2019.03.01 00:00:00 0 700000 700000 -9223372036854775808 700000 0 0
[2] 56502 392030006 2019.03.28 23:30:00 2019.03.01 00:00:00 0 1100000 1100000 -9223372036854775808 900000 1 0
[3] 56544 392030007 2019.03.28 23:30:00 2019.02.01 00:00:00 0 2300000 2500000 -9223372036854775808 2200000 2 0
[4] 56556 392050002 2019.03.28 23:30:00 2019.02.01 00:00:00 0 1630000 1630000 1610000 1620000 1 0
[5] 55887 392020003 2019.03.28 23:50:00 2019.02.01 00:00:00 0 400000 600000 -9223372036854775808 1300000 2 0
[6] 55888 392020004 2019.03.28 23:50:00 2019.02.01 00:00:00 0 -1800000 -3300000 -9223372036854775808 -2000000 1 0
[7] 55889 392020002 2019.03.28 23:50:00 2019.02.01 00:00:00 0 200000 -2300000 -1800000 300000 2 0
[8] 55948 392020006 2019.03.28 23:50:00 2019.02.01 00:00:00 1 1400000 -3400000 -9223372036854775808 -300000 1 0
[9] 55949 392020007 2019.03.28 23:50:00 2019.02.01 00:00:00 1 -1000000 300000 -9223372036854775808 -100000 2 0
显示基于value_id的关于事件值的简要数据
0: value_id=56500 value=900000 impact=CALENDAR_IMPACT_POSITIVE
1: value_id=56501 value=700000 impact=CALENDAR_IMPACT_NA
2: value_id=56502 value=1100000 impact=CALENDAR_IMPACT_POSITIVE
3: value_id=56544 value=2300000 impact=CALENDAR_IMPACT_NEGATIVE
4: value_id=56556 value=1630000 impact=CALENDAR_IMPACT_POSITIVE
5: value_id=55887 value=400000 impact=CALENDAR_IMPACT_NEGATIVE
6: value_id=55888 value=-1800000 impact=CALENDAR_IMPACT_POSITIVE
7: value_id=55889 value=200000 impact=CALENDAR_IMPACT_NEGATIVE
8: value_id=55948 value=1400000 impact=CALENDAR_IMPACT_POSITIVE
9: value_id=55949 value=-1000000 impact=CALENDAR_IMPACT_NEGATIVE
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
另见
CalendarValueHistoryByEvent, CalendarValueHistory, CalendarValueLastByEvent, CalendarValueLast
# 15.4 CalendarCountries
获得“日历”中可用的国家名称数组。
int CalendarCountries(
MqlCalendarCountry& countries[] // 用于接收“日历”国家描述列表的数组
);
2
3
参数
countries[]
[out] MqlCalendarCountry类型数组,用于接收所有“日历”国家的描述。
返回值
所接收的描述数量。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
5400 – ERR_CALENDAR_MORE_DATA(数组大小不足以接收所有国家的描述,只可以接收那些适合的国家)。
2
3
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 从经济日历中获得国家列表
MqlCalendarCountry countries[];
int count=CalendarCountries(countries);
//--- 在“日志”中显示数组
if(count>0)
ArrayPrint(countries);
else
PrintFormat("CalendarCountries() returned 0! Error %d",GetLastError());
/*
结果:
[id] [name] [code] [currency] [currency_symbol] [url_name] [reserved]
[ 0] 0 "Worldwide" "WW" "ALL" "" "worldwide" 0
[ 1] 999 "European Union" "EU" "EUR" "�" "european-union" 0
[ 2] 840 "United States" "US" "USD" "$" "united-states" 0
[ 3] 124 "Canada" "CA" "CAD" "$" "canada" 0
[ 4] 36 "Australia" "AU" "AUD" "$" "australia" 0
[ 5] 554 "New Zealand" "NZ" "NZD" "$" "new-zealand" 0
[ 6] 392 "Japan" "JP" "JPY" "Ґ" "japan" 0
[ 7] 156 "China" "CN" "CNY" "Ґ" "china" 0
[ 8] 826 "United Kingdom" "GB" "GBP" "Ј" "united-kingdom" 0
[ 9] 756 "Switzerland" "CH" "CHF" "₣" "switzerland" 0
[10] 276 "Germany" "DE" "EUR" "�" "germany" 0
[11] 250 "France" "FR" "EUR" "�" "france" 0
[12] 380 "Italy" "IT" "EUR" "�" "italy" 0
[13] 724 "Spain" "ES" "EUR" "�" "spain" 0
[14] 76 "Brazil" "BR" "BRL" "R$" "brazil" 0
[15] 410 "South Korea" "KR" "KRW" "₩" "south-korea" 0
*/
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
另见
CalendarEventByCountry, CalendarCountryById
# 15.5 CalendarEventByCountry
根据指定国家代码获得日历中可用的所有事件描述数组。
int CalendarEventByCountry(
string country_code, // 国家代码名称(ISO 3166-1 alpha-2)
MqlCalendarEvent& events[] //用于接收描述数组的变量
);
2
3
4
参数
country_code
[in] 国家代码名称(ISO 3166-1 alpha-2)
events[]
[out] MqlCalendarEvent类型数组,用于接收指定国家的所有事件描述。
返回值
所接收的描述数量。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
4004 – ERR_NOT_ENOUGH_MEMORY(内存不足,无法执行请求)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
执行 ArrayResize()失败的错误
2
3
4
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 欧盟国家代码(ISO 3166-1 Alpha-2)
string EU_code="EU";
//--- 获得欧盟事件
MqlCalendarEvent events[];
int events_count=CalendarEventByCountry(EU_code,events);
//--- 在“日志”中显示欧盟事件
if(events_count>0)
{
PrintFormat("EU events: %d",events_count);
ArrayPrint(events);
}
//---
}
/*
结果:
欧盟事件:56
[id] [type] [country_id] [unit] [importance] [multiplier] [digits] [event_code]
[ 0] 999010001 0 999 0 2 0 0 "ECB Non-monetary Policy Meeting"
[ 1] 999010002 0 999 0 2 0 0 "ECB Monetary Policy Meeting Account
[ 2] 999010003 0 999 0 3 0 0 "ECB Monetary Policy Press Conferenc
[ 3] 999010004 0 999 0 3 0 0 "ECB President Draghi Speech"
[ 4] 999010005 0 999 0 2 0 0 "ECB Vice President Constancio Speec
[ 5] 999010006 1 999 1 3 0 2 "ECB Deposit Facility Rate Decision"
[ 6] 999010007 1 999 1 3 0 2 "ECB Interest Rate Decision"
[ 7] 999010008 0 999 0 2 0 0 "ECB Economic Bulletin"
[ 8] 999010009 1 999 2 2 3 3 "ECB Targeted LTRO"
[ 9] 999010010 0 999 0 2 0 0 "ECB Executive Board Member Praet Sp
[10] 999010011 0 999 0 2 0 0 "ECB Executive Board Member Mersch S
...
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
另见
CalendarCountries,CalendarCountryById
# 15.6 CalendarEventByCurrency
根据指定货币获得日历中可用的所有事件描述数组。
int CalendarEventByCurrency(
const string currency, // 国家货币代码名称
MqlCalendarEvent& events[] // 用于接收描述数组的变量
);
2
3
4
参数
currency
[in] 国家货币代码名称。
events[]
[out] MqlCalendarEvent类型数组,用于接收指定货币的所有事件描述。
返回值
所接收的描述数量。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
4004 – ERR_NOT_ENOUGH_MEMORY(内存不足,无法执行请求)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
执行 ArrayResize()失败的错误
2
3
4
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 声明用于接收经济日历事件的数组
MqlCalendarEvent events[];
//--- 获得欧盟货币事件
int count = CalendarEventByCurrency("EUR",events);
Print("count = ", count);
//--- 10个事件足以满足当前示例
if(count>10)
ArrayResize(events,10);
//--- 在“日志”中显示事件
ArrayPrint(events);
}
/*
结果:
[id] [type] [country_id] [unit] [importance] [source_url] [event_code] [name]
[0] 999010001 0 999 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-non-monetary-policy-meeting" "ECB Non-monetary Policy Meeting"
[1] 999010002 0 999 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-monetary-policy-meeting-accounts" "ECB Monetary Policy Meeting Accounts"
[2] 999010003 0 999 0 3 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-monetary-policy-press-conference" "ECB Monetary Policy Press Conference"
[3] 999010004 0 999 0 3 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-president-draghi-speech" "ECB President Draghi Speech"
[4] 999010005 0 999 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-vice-president-vitor-constancio-speech" "ECB Vice President Constancio Speech"
[5] 999010006 1 999 1 3 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-deposit-rate-decision" "ECB Deposit Facility Rate Decision"
[6] 999010007 1 999 1 3 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-interest-rate-decision" "ECB Interest Rate Decision"
[7] 999010008 0 999 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-economic-bulletin" "ECB Economic Bulletin"
[8] 999010009 1 999 2 2 "https://www.ecb.europa.eu/home/html/index.en.html" "targeted-ltro" "ECB Targeted LTRO"
[9] 999010010 0 999 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-executive-board-member-praet-speech" "ECB Executive Board Member Praet Speech"
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
另见
CalendarEventById,CalendarEventByCountry
# 15.7 CalendarValueHistoryByEvent
根据事件ID获得指定时期的所有事件值数组。
bool CalendarValueHistoryByEvent(
ulong event_id, // 事件ID
MqlCalendarValue& values[], // 值描述数组
datetime datetime_from, // 时间范围的左边框
datetime datetime_to=0 // 时间范围的右边框
);
2
3
4
5
6
参数
event_id
[in] 事件ID。
values[]
[out] MqlCalendarValue类型数组,用于接收事件值。
datetime_from
[in] 时间范围事件的初始日期通过指定ID选择,而datetime_from < datetime_to。
datetime_to=0
[in] 时间范围事件的结束日期通过指定ID选择。如果datetime_to 没有设置(或设置为0),那么返回在“日历”数据库中从指定datetime_from日期开始的所有事件值(包括未来事件的值)。
返回值
如果成功返回true,否则返回false。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
4004 – ERR_NOT_ENOUGH_MEMORY(内存不足,无法执行请求)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
5400 – ERR_CALENDAR_MORE_DATA(数组大小不足以接收所有值的描述,只可以接收那些适合的值)。
执行 ArrayResize()失败的错误
2
3
4
5
注意
所有用于经济日历的函数都使用交易服务器时间(TimeTradeServer)。这意味着MqlCalendarValue结构中的时间和CalendarValueHistoryByEvent/CalendarValueHistory函数中输入的时间都在交易服务器时区中设置,而不是在用户本地时间设置。
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结构的函数检查和获取值。
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 欧盟国家代码(ISO 3166-1 Alpha-2)
string EU_code="EU";
//--- 获得欧盟事件
MqlCalendarEvent events[];
int events_count=CalendarEventByCountry(EU_code,events);
//--- 在“日志”中显示欧盟事件
if(events_count>0)
{
PrintFormat("EU events: %d",events_count);
//--- 减少事件列表,10个事件足以分析
ArrayResize(events,10);
ArrayPrint(events);
}
//--- 查阅“ECB利率决策”事件event_id=999010007
ulong event_id=events[6].id; // 事件ID可在“日历”中更改,因此一定进行验证
string event_name=events[6].name; // “日历”事件的名称
PrintFormat("Get values for event_name=%s event_id=%d",event_name,event_id);
//--- 获得“ECB利率决策”事件的所有值
MqlCalendarValue values[];
//--- 设置事件发生间隔的边界
datetime date_from=0; // 获取可用历史开始的所有事件
datetime date_to=D'01.01.2016'; // 获取2016年以后的事件
if(CalendarValueHistoryByEvent(event_id,values,date_from,date_to))
{
PrintFormat("Received values for %s: %d",
event_name,ArraySize(values));
//--- 减少值列表,10个事件足以分析
ArrayResize(values,10);
ArrayPrint(values);
}
else
{
PrintFormat("Error! Failed to get values for event_id=%d",event_id);
PrintFormat("Error code: %d",GetLastError());
}
}
//---
/*
结果:
欧盟事件:56
[id] [type] [sector] [frequency] [time_mode] [country_id] [unit] [importance] [multiplier] [digits] [source_url] [event_code] [name] [reserv
[0] 999010001 0 5 0 0 999 0 2 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-non-monetary-policy-meeting" "ECB Non-monetary Policy Meeting"
[1] 999010002 0 5 0 0 999 0 2 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-monetary-policy-meeting-accounts" "ECB Monetary Policy Meeting Accounts"
[2] 999010003 0 5 0 0 999 0 3 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-monetary-policy-press-conference" "ECB Monetary Policy Press Conference"
[3] 999010004 0 5 0 0 999 0 3 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-president-draghi-speech" "ECB President Draghi Speech"
[4] 999010005 0 5 0 0 999 0 2 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-vice-president-vitor-constancio-speech" "ECB Vice President Constancio Speech"
[5] 999010006 1 5 0 0 999 1 3 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-deposit-rate-decision" "ECB Deposit Facility Rate Decision"
[6] 999010007 1 5 0 0 999 1 3 0 2 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-interest-rate-decision" "ECB Interest Rate Decision"
[7] 999010008 0 5 0 0 999 0 2 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-economic-bulletin" "ECB Economic Bulletin"
[8] 999010009 1 5 0 0 999 2 2 3 3 "https://www.ecb.europa.eu/home/html/index.en.html" "targeted-ltro" "ECB Targeted LTRO"
[9] 999010010 0 5 0 0 999 0 2 0 0 "https://www.ecb.europa.eu/home/html/index.en.html" "ecb-executive-board-member-praet-speech" "ECB Executive Board Member Praet Speech"
获得event_name=ECB Interest Rate Decision值 event_id=999010007
已接收的“ECB利率决策”事件值:102
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 2776 999010007 2007.03.08 11:45:00 1970.01.01 00:00:00 0 3750000 4250000 -9223372036854775808 -9223372036854775808 0 0
[1] 2777 999010007 2007.05.10 11:45:00 1970.01.01 00:00:00 0 3750000 3750000 -9223372036854775808 -9223372036854775808 0 0
[2] 2778 999010007 2007.06.06 11:45:00 1970.01.01 00:00:00 0 4000000 3750000 -9223372036854775808 -9223372036854775808 0 0
[3] 2779 999010007 2007.07.05 11:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
[4] 2780 999010007 2007.08.02 11:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
[5] 2781 999010007 2007.09.06 11:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
[6] 2782 999010007 2007.10.04 11:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
[7] 2783 999010007 2007.11.08 12:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
[8] 2784 999010007 2007.12.06 12:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
[9] 2785 999010007 2008.01.10 12:45:00 1970.01.01 00:00:00 0 4000000 4000000 -9223372036854775808 -9223372036854775808 0 0
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
另见
CalendarCountries, CalendarEventByCountry, CalendarValueHistory, CalendarEventById,CalendarValueById
# 15.8 CalendarValueHistory
通过根据国家和/或货币进行排序的能力,获得指定时期的所有事件值数组。
bool CalendarValueHistory(
MqlCalendarValue& values[], // 值描述数组
datetime datetime_from, // 时间范围的左边框
datetime datetime_to=0 //时间范围的右边框
const string country_code=NULL, // 国家代码名称(ISO 3166-1 alpha-2)
const string currency=NULL // 国家货币代码名称
);
2
3
4
5
6
7
参数
values[]
[out] MqlCalendarValue类型数组,用于接收事件值。
datetime_from
[in] 时间范围事件的初始日期通过指定ID选择,而datetime_from < datetime_to。
datetime_to=0
[in] 时间范围事件的结束日期通过指定ID选择。如果datetime_to 没有设置(或设置为0),那么返回在“日历”数据库中从指定datetime_from日期开始的所有事件值(包括未来事件的值)。
country_code=NULL
[in] 国家代码名称(ISO 3166-1 alpha-2)
currency=NULL
[in] 国家货币代码名称。
返回值
如果成功返回true,否则返回false。若要获得有关错误的信息,请调用GetLastError()函数。可能错误:
4001 – ERR_INTERNAL_ERROR(一般运行时错误)。
4004 – ERR_NOT_ENOUGH_MEMORY(内存不足,无法执行请求)。
5401 – ERR_CALENDAR_TIMEOUT(超过请求时限)。
5400 – ERR_CALENDAR_MORE_DATA(数组大小不足以接收所有值的描述,只可以接收那些适合的值)。
执行ArrayResize()失败的错误
2
3
4
5
注意
所有用于经济日历的函数都使用交易服务器时间(TimeTradeServer)。这意味着MqlCalendarValue结构中的时间和CalendarValueHistoryByEvent/CalendarValueHistory函数中输入的时间都在交易服务器时区中设置,而不是在用户本地时间设置。
如果固定长度的events[]数组被传递到函数且没有足够空间保存整个结果,则会激活ERR_CALENDAR_MORE_DATA (5400)错误。
如果datetime_to没有设置(或设置为0),那么返回在“日历”数据库中从指定datetime_from日期开始的所有事件值(包括未来事件的值)。
对于country_code和currency 过滤器,NULL和""值是相同的,表示没有过滤器。
对于country_code,MqlCalendarCountry结构的code字段,例如应使用 "US"、"RU"或"EU"。
对于currency,MqlCalendarCountry结构的currency字段,例如应使用"USD"、"RUB"或"EUR"。
过滤器是通过连接使用的,即合理'AND'仅用于选择同时满足两个条件(国家和货币)的事件值。
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结构的函数检查和获取值。
例如:
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 欧盟国家代码(ISO 3166-1 Alpha-2)
string EU_code="EU";
//--- 获得所有欧盟事件值
MqlCalendarValue values[];
//--- 设置事件发生间隔的边界
datetime date_from=D'01.01.2018'; // 获取2018年的所有事件
datetime date_to=0; // 0 指所有已知事件,包括尚未发生的事件
//--- 请求2018年以来的欧盟事件历史
if(CalendarValueHistory(values,date_from,date_to,EU_code))
{
PrintFormat("Received event values for country_code=%s: %d",
EU_code,ArraySize(values));
//--- 减少输出到“日志”的数组大小
ArrayResize(values,10);
//--- 在“日志”中显示事件值
ArrayPrint(values);
}
else
{
PrintFormat("Error! Failed to receive events for country_code=%s",EU_code);
PrintFormat("Error code: %d",GetLastError());
}
//---
}
/*
结果:
已接收的country_code=EU事件值:1384
[id] [event_id] [time] [period] [revision] [actual_value] [prev_value] [revised_prev_value] [forecast_value] [impact_type] [reserved]
[0] 54215 999500001 2018.01.02 09:00:00 2017.12.01 00:00:00 3 60600000 60600000 -9223372036854775808 60500000 1 0
[1] 54221 999500002 2018.01.04 09:00:00 2017.12.01 00:00:00 3 56600000 56500000 -9223372036854775808 56000000 1 0
[2] 54222 999500003 2018.01.04 09:00:00 2017.12.01 00:00:00 3 58100000 58000000 -9223372036854775808 58400000 2 0
[3] 45123 999030005 2018.01.05 10:00:00 2017.11.01 00:00:00 0 600000 400000 -9223372036854775808 100000 1 0
[4] 45124 999030006 2018.01.05 10:00:00 2017.11.01 00:00:00 0 2800000 2500000 -9223372036854775808 1500000 1 0
[5] 45125 999030012 2018.01.05 10:00:00 2017.12.01 00:00:00 1 900000 900000 -9223372036854775808 1000000 2 0
[6] 45126 999030013 2018.01.05 10:00:00 2017.12.01 00:00:00 1 1400000 1500000 -9223372036854775808 1500000 2 0
[7] 54953 999520001 2018.01.05 20:30:00 2018.01.02 00:00:00 0 127900000 92100000 -9223372036854775808 76400000 0 0
[8] 22230 999040003 2018.01.08 10:00:00 2017.12.01 00:00:00 0 9100000 8200000 8100000 7600000 1 0
[9] 22231 999040004 2018.01.08 10:00:00 2017.12.01 00:00:00 0 18400000 16300000 16400000 16800000 1 0
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
另见
CalendarCountries, CalendarEventByCountry, CalendarValueHistoryByEvent, CalendarEventById, CalendarValueById
# 15.9 CalendarValueLastByEvent
通过指定change_id,从日历数据库状态根据ID获得事件值数组。
int CalendarValueLastByEvent(
ulong event_id, // 事件ID
ulong& change_id, // 事件值ID
MqlCalendarValue& values[] // 值描述数组
);
2
3
4
5
参数
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(数组大小不足以接收所有值的描述,只可以接收那些适合的值)。
执行 ArrayResize()失败的错误
2
3
4
5
注意
所有用于经济日历的函数都使用交易服务器时间(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万。这表示当您使用函数CalendarValueById、CalendarValueHistoryByEvent、CalendarValueHistory、CalendarValueLastByEvent、CalendarValueLast接收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
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
另见
CalendarValueLast, CalendarValueHistory, CalendarValueHistoryByEvent, CalendarValueById
# 15.10 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 // 国家货币代码名称
);
2
3
4
5
6
参数
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()失败的错误
2
3
4
5
注意
所有用于经济日历的函数都使用交易服务器时间(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
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
另见
CalendarValueLast, CalendarValueHistory, CalendarValueHistoryByEvent, CalendarValueById