//+------------------------------------------------------------------+
//| OnBookEvent_Sample.mq5 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "https://www.mql5.com/en/articles/2635"
#property version "1.00"
#property description "Example of measuring the market depth refresh rate using OnBookEvent()"
#property description "The code is taken from the article https://www.mql5.com/en/articles/2635"
//--- 输入参数
input ulong ExtCollectTime =30; // test time in seconds
input ulong ExtSkipFirstTicks=10; // number of ticks skipped at start
//--- 订阅BookEvent事件的标识
bool book_subscribed=false;
//--- 接受来自市场深度的请求的数组
MqlBookInfo book[];
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 显示开始
Comment(StringFormat("Waiting for the first %I64u ticks to arrive",ExtSkipFirstTicks));
PrintFormat("Waiting for the first %I64u ticks to arrive",ExtSkipFirstTicks);
//--- 启用市场深度广播
if(MarketBookAdd(_Symbol))
{
book_subscribed=true;
PrintFormat("%s: MarketBookAdd(%s) function returned true",__FUNCTION__,_Symbol);
}
else
PrintFormat("%s: MarketBookAdd(%s) function returned false! GetLastError()=%d",__FUNCTION__,_Symbol,GetLastError());
//--- 成功初始化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 去初始化EA交易 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- 显示去初始化原因代码
Print(__FUNCTION__,": Deinitialization reason code = ",reason);
//--- 取消订阅以获取市场深度事件
if(book_subscribed)
{
if(!MarketBookRelease(_Symbol))
PrintFormat("%s: MarketBookRelease(%s) returned false! GetLastError()=%d",_Symbol,GetLastError());
else
book_subscribed=false;
}
//---
}
//+------------------------------------------------------------------+
//| BookEvent函数 |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol)
{
static ulong starttime=0; // 测试开始时间
static ulong tickcounter=0; // 市场深度更新计数器
//--- 只有当我们自己订阅市场深度事件时处理它们
if(!book_subscribed)
return;
//--- 只为特定交易品种计算更新
if(symbol!=_Symbol)
return;
//--- 跳过第一个报价来清空队列和进行准备
tickcounter++;
if(tickcounter<ExtSkipFirstTicks)
return;
//--- 记住开始时间
if(tickcounter==ExtSkipFirstTicks)
starttime=GetMicrosecondCount();
//--- 请求市场深度数据
MarketBookGet(symbol,book);
//--- 什么时候停止?
ulong endtime=GetMicrosecondCount()-starttime;
ulong ticks =1+tickcounter-ExtSkipFirstTicks;
// 从测试开始以后,已经过去多少毫秒?
if(endtime>ExtCollectTime*1000*1000)
{
PrintFormat("%I64u ticks for %.1f seconds: %.1f ticks/sec ",ticks,endtime/1000.0/1000.0,ticks*1000.0*1000.0/endtime);
ExpertRemove();
return;
}
//--- 在评论字段中显示计数器
if(endtime>0)
Comment(StringFormat("%I64u ticks for %.1f seconds: %.1f ticks/sec ",ticks,endtime/1000.0/1000.0,ticks*1000.0*1000.0/endtime));
}
|