如何在 MQL5 中创建 MqlTick 列表、堆栈或队列?

问题描述

我想创建 MqlTick 实例的堆栈、队列或列表。

以下代码

#include <Generic\Queue.mqh>
CQueue<MqlTick> myQueue;

产生这些错误

'' - objects are passed by reference only ICollection.mqh 14 18

'm_array' - objects are passed by reference only Queue.mqh 140 19

还有这个:

#include <Generic\Queue.mqh>
CQueue<MqlTick *> longQueue;

给出: class type expected,pointer to type 'MqlTick' is not allowed MyTest.mq5

如果我这样做:

#include <Generic\Queue.mqh>
#include <Object.mqh>
CQueue<CObject *> longQueue;

MqlTick currentTick;
longQueue.Add(&currentTick);

编译器说:

'Add' - no one of the overloads can be applied to the function call MyTest.mq5
Could be one of 2 function(s)   MyTest.mq5
   bool CQueue<CObject*>::Add(CObject*) Queue.mqh   30  22
   bool ICollection<CObject*>::Add(CObject*)    ICollection.mqh 14  14

因为 MqlTick 是一个结构体而不是 CObject 的实例。

解决方法

如果您尝试访问以前的数据,您可以使用 CopyTicksRange,输出数组被组织为一个堆栈,在包含报价信息的 MqlTick 结构的 0 索引中具有最近的报价。>

MqlTick ticksarray[];
double Most_recent_tick_last_price;
datetime Most_recent_tick_time;

#Setting the time period
input datetime Start_date = D'2021.07.26 09:06';
input datetime End_date = D'2021.07.27 18:00'; 
ulong Start = ulong(Start_date)*1000;
ulong End   = ulong(End_date  )*1000;

#Saving ticks on a array:
CopyTicksRange(_Symbol,ticksarray,COPY_TICKS_INFO,Start,End);

#Acessing specific values:
Most_recent_tick_time= ticksarray[0].time
Most_recent_tick_last_price= ticksarray[0].last

如果您只需要当前的刻度信息,可以使用 SymbolInfoTick

MqlTick current_tick[];
double current_ask,current_bid;
void OnTick()
  {
    SymbolInfoTick(_Symbol,current_tick);
    current_bid = current_tick.bid;
    current_ask = current_tick.ask;
  }