按国家/地区和货币查询事件类型

财经事件和节假日的日历在每个国家/地区都有其自身的特殊性。MQL 程序可以查询特定国家/地区内的事件类型,以及与特定货币相关的事件类型。后者在多个国家/地区使用相同货币的情况下非常重要,例如,大多数欧盟成员国。

int CalendarEventByCountry(const string country, MqlCalendarEvent &events[])

CalendarEventByCountry 函数使用日历中针对指定国家/地区(根据 ISO 3166-1 alpha-2 标准的双字母国家/地区代码)的所有可用事件类型的描述来填充通过引用传递的 MqlCalendarEvent 结构体数组。我们在上一节的日志中看到了此类代码的示例:EU 代表欧盟,US 代表美国,DE 代表德国,CN 代表中国,依此类推。

接收数组可以是动态的,也可以是具有足够大小的固定数组。

该函数返回接收到的描述数量,如果出错则返回 0。特别地,如果固定数组无法容纳所有事件,该函数将用可用数据的适合部分填充固定数组,并将 _LastError 代码设置为等于 CALENDAR_MORE_DATA (5400)。也可能发生内存分配错误 (4004, ERR_NOT_ENOUGH_MEMORY) 或来自服务器的日历请求超时 (5401, ERR_CALENDAR_TIMEOUT)。

如果具有给定代码的国家/地区不存在,则会发生 INTERNAL_ERROR (4001)。

通过指定 NULL 或空字符串 "" 来代替 country,你可以获取所有国家/地区的完整事件列表。

我们使用简单的脚本 CalendarEventKindsByCountry.mq5 来测试该函数的性能。它只有一个输入参数,即我们感兴趣的国家/地区的代码。

input string CountryCode = "HK";

接下来,通过调用 CalendarEventByCountry 发出事件类型请求,如果成功,则将产生的数组记录到日志中。

void OnStart()
{
   MqlCalendarEvent events[];
   if(PRTF(CalendarEventByCountry(CountryCodeevents)))
   {
      Print("Event kinds for country: "CountryCode);
      ArrayPrint(events);
   }
}

以下是结果示例(由于代码行很长,为了在书中发布,它们被人为地分成了 2 个块:第一个块包含 MqlCalendarEvent 结构体的数字字段,第二个块包含字符串字段)。

CalendarEventByCountry(CountryCode,events)=26 / ok

Event kinds for country: HK

[id] [type] [sector] [frequency] [time_mode] [country_id] [unit] [importance] [multiplier] [digits] »

[ 0] 344010001 1 5 2 0 344 6 1 3 1 »

[ 1] 344010002 1 5 2 0 344 1 1 0 1 »

[ 2] 344020001 1 4 2 0 344 1 1 0 1 »

[ 3] 344020002 1 2 3 0 344 1 3 0 1 »

[ 4] 344020003 1 2 3 0 344 1 2 0 1 »

[ 5] 344020004 1 6 2 0 344 1 1 0 1 »

[ 6] 344020005 1 6 2 0 344 1 1 0 1 »

[ 7] 344020006 1 6 2 0 344 2 2 3 3 »

[ 8] 344020007 1 9 2 0 344 1 1 0 1 »

[ 9] 344020008 1 3 2 0 344 1 2 0 1 »

[10] 344030001 2 12 0 1 344 0 0 0 0 »

[11] 344030002 2 12 0 1 344 0 0 0 0 »

[12] 344030003 2 12 0 1 344 0 0 0 0 »

[13] 344030004 2 12 0 1 344 0 0 0 0 »

[14] 344030005 2 12 0 1 344 0 0 0 0 »

[15] 344030006 2 12 0 1 344 0 0 0 0 »

[16] 344030007 2 12 0 1 344 0 0 0 0 »

[17] 344030008 2 12 0 1 344 0 0 0 0 »

[18] 344030009 2 12 0 1 344 0 0 0 0 »

[19] 344030010 2 12 0 1 344 0 0 0 0 »

[20] 344030011 2 12 0 1 344 0 0 0 0 »

[21] 344030012 2 12 0 1 344 0 0 0 0 »

[22] 344030013 2 12 0 1 344 0 0 0 0 »

[23] 344030014 2 12 0 1 344 0 0 0 0 »

[24] 344030015 2 12 0 1 344 0 0 0 0 »

[25] 344500001 1 8 2 0 344 0 1 0 1 »

 

日志的后续内容(右侧片段)。

» [source_url] [event_code] [name]

[ 0]» "https://www.hkma.gov.hk/eng/" "foreign-exchange-reserves" "Foreign Exchange Reserves"

[ 1]» "https://www.hkma.gov.hk/eng/" "hkma-m3-money-supply-yy" "HKMA M3 Money Supply y/y"

[ 2]» "https://www.censtatd.gov.hk/en/" "cpi-yy" "CPI y/y"

[ 3]» "https://www.censtatd.gov.hk/en/" "gdp-qq" "GDP q/q"

[ 4]» "https://www.censtatd.gov.hk/en/" "gdp-yy" "GDP y/y"

[ 5]» "https://www.censtatd.gov.hk/en/" "exports-mm" "Exports y/y"

[ 6]» "https://www.censtatd.gov.hk/en/" "imports-mm" "Imports y/y"

[ 7]» "https://www.censtatd.gov.hk/en/" "trade-balance" "Trade Balance"

[ 8]» "https://www.censtatd.gov.hk/en/" "retail-sales-yy" "Retail Sales y/y"

[ 9]» "https://www.censtatd.gov.hk/en/" "unemployment-rate-3-months" "Unemployment Rate 3-Months"

[10]» "https://publicholidays.hk/" "new-years-day" "New Year's Day"

[11]» "https://publicholidays.hk/" "lunar-new-year" "Lunar New Year"

[12]» "https://publicholidays.hk/" "ching-ming-festival" "Ching Ming Festival"

[13]» "https://publicholidays.hk/" "good-friday" "Good Friday"

[14]» "https://publicholidays.hk/" "easter-monday" "Easter Monday"

[15]» "https://publicholidays.hk/" "birthday-of-buddha" "The Birthday of the Buddha"

[16]» "https://publicholidays.hk/" "labor-day" "Labor Day"

[17]» "https://publicholidays.hk/" "tuen-ng-festival" "Tuen Ng Festival"

[18]» "https://publicholidays.hk/" "hksar-establishment-day" "HKSAR Establishment Day"

[19]» "https://publicholidays.hk/" "day-following-mid-autumn-festival" "The Day Following Mid-Autumn Festival"

[20]» "https://publicholidays.hk/" "national-day" "National Day"

[21]» "https://publicholidays.hk/" "chung-yeung-festival" "Chung Yeung Festival"

[22]» "https://publicholidays.hk/" "christmas-day" "Christmas Day"

[23]» "https://publicholidays.hk/" "first-weekday-after-christmas-day" "The First Weekday After Christmas Day"

[24]» "https://publicholidays.hk/" "day-following-good-friday" "The Day Following Good Friday"

[25]» "https://www.markiteconomics.com" "nikkei-pmi" "S&P Global PMI"

 

int CalendarEventByCurrency(const string currency, MqlCalendarEvent &events[])

CalendarEventByCurrency 函数在传入的 events 数组中填充日历中与指定 currency 相关的各种事件的描述。三位字母代码是所有外汇交易商中通用的。

如果指定的货币代码无效,函数将返回 0(无错误)和一个空数组。

如果指定 NULL 或空字符串 "" 来代替 currency,则可以获得完整的日历事件列表。

我们使用 CalendarEventKindsByCurrency.mq5 脚本测试该函数。输入参数指定了货币代码。

input string Currency = "CNY";

OnStart 处理程序中,我们请求事件并将其输出到日志中。

void OnStart()
{
   MqlCalendarEvent events[];
   if(PRTF(CalendarEventByCurrency(Currencyevents)))
   {
      Print("Event kinds for currency: "Currency);
      ArrayPrint(events);
   }
}

以下是结果示例(用缩写给出)。

CalendarEventByCurrency(Currency,events)=40 / ok

Event kinds for currency: CNY

[id] [type] [sector] [frequency] [time_mode] [country_id] [unit] [importance] [multiplier] [digits] »

[ 0] 156010001 1 4 2 0 156 1 2 0 1 »

[ 1] 156010002 1 4 2 0 156 1 1 0 1 »

[ 2] 156010003 1 4 2 0 156 1 1 0 1 »

[ 3] 156010004 1 2 3 0 156 1 3 0 1 »

[ 4] 156010005 1 2 3 0 156 1 2 0 1 »

[ 5] 156010006 1 9 2 0 156 1 2 0 1 »

[ 6] 156010007 1 8 2 0 156 1 2 0 1 »

[ 7] 156010008 1 8 2 0 156 0 3 0 1 »

[ 8] 156010009 1 8 2 0 156 0 3 0 1 »

[ 9] 156010010 1 8 2 0 156 1 2 0 1 »

[10] 156010011 0 5 0 0 156 0 2 0 0 »

[11] 156010012 1 3 2 0 156 1 2 0 1 »

[12] 156010013 1 8 2 0 156 1 1 0 1 »

[13] 156010014 1 8 2 0 156 1 1 0 1 »

[14] 156010015 1 8 2 0 156 0 3 0 1 »

[15] 156010016 1 8 2 0 156 1 2 0 1 »

[16] 156010017 1 9 2 0 156 1 2 0 1 »

[17] 156010018 1 2 3 0 156 1 2 0 1 »

[18] 156020001 1 6 2 3 156 6 2 3 2 »

[19] 156020002 1 6 2 3 156 1 1 0 1 »

[20] 156020003 1 6 2 3 156 1 1 0 1 »

[21] 156020004 1 6 2 3 156 2 2 3 2 »

[22] 156020005 1 6 2 3 156 1 1 0 1 »

[23] 156020006 1 6 2 3 156 1 1 0 1 »

...

右侧片段。

» [source_url] [event_code] [name]

[ 0]» "http://www.stats.gov.cn/english/" "cpi-mm" "CPI m/m"

[ 1]» "http://www.stats.gov.cn/english/" "cpi-yy" "CPI y/y"

[ 2]» "http://www.stats.gov.cn/english/" "ppi-yy" "PPI y/y"

[ 3]» "http://www.stats.gov.cn/english/" "gdp-qq" "GDP q/q"

[ 4]» "http://www.stats.gov.cn/english/" "gdp-yy" "GDP y/y"

[ 5]» "http://www.stats.gov.cn/english/" "retail-sales-yy" "Retail Sales y/y"

[ 6]» "http://www.stats.gov.cn/english/" "industrial-production-yy" "Industrial Production y/y"

[ 7]» "http://www.stats.gov.cn/english/" "manufacturing-pmi" "Manufacturing PMI"

[ 8]» "http://www.stats.gov.cn/english/" "non-manufacturing-pmi" "Non-Manufacturing PMI"

[ 9]» "http://www.stats.gov.cn/english/" "fixed-asset-investment-yy" "Fixed Asset Investment y/y"

[10]» "http://www.stats.gov.cn/english/" "nbs-press-conference-on-economic-situation" "NBS Press Conference on Economic Situation"

[11]» "http://www.stats.gov.cn/english/" "unemployment-rate" "Unemployment Rate"

[12]» "http://www.stats.gov.cn/english/" "industrial-profit-yy" "Industrial Profit y/y"

[13]» "http://www.stats.gov.cn/english/" "industrial-profit-ytd-yy" "Industrial Profit YTD y/y"

[14]» "http://www.stats.gov.cn/english/" "composite-pmi" "Composite PMI"

[15]» "http://www.stats.gov.cn/english/" "industrial-production-ytd-yy" "Industrial Production YTD y/y"

[16]» "http://www.stats.gov.cn/english/" "retail-sales-ytd-yy" "Retail Sales YTD y/y"

[17]» "http://www.stats.gov.cn/english/" "gdp-ytd-yy" "GDP YTD y/y"

[18]» "http://english.customs.gov.cn/" "trade-balance-usd" "Trade Balance USD"

[19]» "http://english.customs.gov.cn/" "imports-usd-yy" "Imports USD y/y"

[20]» "http://english.customs.gov.cn/" "exports-usd-yy" "Exports USD y/y"

[21]» "http://english.customs.gov.cn/" "trade-balance" "Trade Balance"

[22]» "http://english.customs.gov.cn/" "imports-yy" "Imports y/y"

[23]» "http://english.customs.gov.cn/" "exports-yy" "Exports y/y"

...

细心的读者会注意到,事件类型标识符包含了国家/地区代码、新闻来源编号以及来源内部的序列号(编号从 1 开始)。因此,事件类型标识符的通用格式是:CCCSSNNNN,其中 CCC 是国家/地区代码,SS 是来源,NNNN 是编号。例如,156020001 是中国第二个来源的第一条新闻,而 344030010 是香港第三个来源的第十条新闻。唯一的例外是全球性新闻,其“国家/地区”代码不是 000 而是 1000。