- Basic concepts of the calendar
- Getting the list and descriptions of available countries
- Querying event types by country and currency
- Getting event descriptions by ID
- Getting event records by country or currency
- Getting event records of a specific type
- Reading event records by ID
- Tracking event changes by country or currency
- Tracking event changes by type
- Filtering events by multiple conditions
- Transferring calendar database to tester
- Calendar trading
Getting event records of a specific type
If necessary, an MQL program has the ability to request events of a specific type: to do this, it is enough to know the event identifier in advance, for example, using the CalendarEventByCountry or CalendarEventByCurrency functions which were presented in the section Querying event types by country and currency.
int CalendarValueHistoryByEvent(ulong id, MqlCalendarValue &values[], datetime from, datetime to = 0)
The CalendarValueHistoryByEvent function fills the array passed by reference with records of events of a specific type indicated by the id identifier. Parameters from and to allow you to limit the range of dates in which events are searched.
If an optional parameter to is not specified, all calendar entries will be placed in the array, starting from the from time and further into the future. To query all the past events, set from to 0. If both from and to parameters are 0, all history and scheduled events will be returned. In all other cases, when to is not equal to 0, it must be greater than from.
The values array can be dynamic (then the function will automatically expand or reduce it according to the amount of data) or of fixed size (then only a part that fits will be copied into the array).
The function returns the number of copied elements.
As an example, consider the script CalendarStatsByEvent.mq5, which calculates the statistics (frequency of occurrence) of events of different types for a given country or currency in a given time range.
The analysis conditions are specified in the input variables.
input string CountryOrCurrency = "EU";
|
Depending on the length of the CountryOrCurrency string, it is interpreted as a country code (2 characters) or currency code (3 characters).
To collect statistics, we will declare a structure; its fields will store the identifier and name of the event type, its importance, and the counter of such events.
struct CalendarEventStats
|
In the OnStart function, we first request all kinds of events using the CalendarEventByCountry or CalendarEventByCurrency function to the specified depth of history and into the future, and then, in a loop through the event descriptions received in the events array, we call CalendarValueHistoryByEvent for each event ID. In this application, we are not interested in the contents of the values array, as we just need to know their count.
void OnStart()
|
Upon successful function call, we fill the CalendarEventStats structure and add it to the array of structures stats. Next, we sort the structure in the way we already know (the SORT_STRUCT macro is described in the section Comparing, sorting, and searching in arrays).
Running the script with default settings generates something like this in the log (abbreviated).
CalendarEventByCountry(CountryOrCurrency,events)=82 / ok
|
Please note that a total of 82 types of events were received, however, in the statistics array, we had only 74. This is because the CalendarValueHistoryByEvent function returns false (failure) and zero error code in _LastError if there were no events of any kind in the specified date range. In the above test, there are 8 such entries that theoretically exist but were never encountered within the year.