0

我在 MQL5 中尝试了以下代码,但出现错误。我猜这个代码来自 MQL4。

代码:

int OnInit()
{
// if you don't want to execute on the first tick
  IsBarClosed(-1,false);
  return(1);
  if(!IsBarClosed(0,true)) // true/false here allows to keep old bar for check again later in the code and reset
      return(0);
}


//+------------------------------------------------------------------+
//| check if new bar has formed
//+------------------------------------------------------------------+
bool IsBarClosed(int timeframe,bool reset)
{
    static datetime lastbartime;
    if(timeframe==-1)
    {
        if(reset)
            lastbartime=0;
        else
            lastbartime=iTime(NULL,timeframe,0);
        return(true);
    }
    if(iTime(NULL,timeframe,0)==lastbartime) // wait for new bar
        return(false);
    if(reset)
        lastbartime=iTime(NULL,timeframe,0);
    return(true);
}

输出:

'iTime' - function not defined  testing lines and trdae.mq5     243     25
'iTime' - function not defined  testing lines and trdae.mq5     246     8
'iTime' - function not defined  testing lines and trdae.mq5     249     21
3 error(s), 0 warning(s)                4       1

请帮助我使用 MQL5 正确完成它。我正在尝试检测蜡烛条的关闭时间而不是打开时间。我只想在酒吧关闭时尝试。

4

1 回答 1

1
  1. iTime()函数在 MQl5 中不存在,仅在 MQL4 中存在。使用CopyRates()SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE)
  2. static是 MQL4/5 中的一个关键字,具有非常具体的属性。许多版本的工作方式如下:如果将 ea 附加到图表,static则为零,然后更新。如果你重新附加 - 从零到实际。如果您更改 ea/ind 的时间范围或设置 - 静态保持不变(它不会取消初始化,不会变为零,然后是实际值)。最早的 1000+ 版本的 MT4 就是这样工作的(从现在开始有两次更新)。也许有人发现这个关键字在 mql4 中很有用,它允许将变量与其函数保持在一起,而不是在全局变量中;当然记住上面的问题或忽略它。但是没有理由在 MQL5 中使用这个词。如果您需要一组函数 - 创建一个类并保留与之相关的所有变量。然后,您不会遇到尚未重新初始化的静态变量的问题。
  3. 酒吧关闭时间是一个有趣的概念,我不确定你的意思。如果您有一个酒吧(例如,使用 MqlRates),您可以获得它的开放时间。关闭时间没有报告。你当然可以自己算(open time + PeriodSeconds(_Period)-1)。
  4. IsNewBar() 函数/类有一个重要的功能 - 它检查每个分时并说明新柱是否已经开始。可以判断一个新柱开始了,因为 TimeCurrent() 或最后一个刻度的时间 >= 最后一个已知柱的时间 + 60*Period()。同时,无法判断柱是否已关闭(好吧,如果创建了新柱,则可以告知柱已关闭,这是唯一的方法),因为许多分时可能会在之后到达那个(即使在酒吧的最后一秒)。

我不知道你为什么需要在你的代码中重置参数,试试下面的:

datetime iTime=(datetime)(SeriesInfoInteger(_Symbol,Period(),SERIES_LASTBAR_DATE)/PeriodSeconds()*PeriodSeconds()); 

然后iTime()用 iTime 变量替换您的代码,这可能会有所帮助

于 2018-03-30T06:54:00.103 回答