#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
*/
|