如何检测一个新酒吧

 

大家好。

我是一个完全的MQL5新手,但我已经为其他平台做了相当多的编码工作。

我想知道为什么一个EA只在新条形图上执行是如此困难。我看了一些文章,包括 "新条形 "事件处理程序,这似乎非常困难

如果这行不通,我必须使用 "新条形 "事件处理程序,是否可以直接下载相关文件并将其复制到Files(?)文件夹中,然后#include它们?对于像我这样基本上不知道自己在做什么的人来说,有没有什么地方有这方面的记录?

我正在学习https://www.mql5.com/en/articles/100,我注意到的第一件事是 "Bars "函数是用来确保有足够的条形图来进行计算的,但下一段代码显然是用来检测新的条形图的,(反正在我看来)是非常复杂的一段代码。难道不能用Bars函数 来检测一个新条形吗?比如 if(Bars > int LastBarCount) {bool IsNewBar = true; LastBarCount = Bars;} else {IsNewBar = false;}。这样的东西能行吗?

有一个愿望清单。如果有一个我们可以覆盖的OnNewBar事件,而不是OnTick,那不是很好吗?因为我见过的每一个指标都是在条上工作的,而不是在点上,我想这将使每个人的生活变得更容易。

刚刚发现了另一种可能性。"BarsCalculated"。从描述中也不完全确定这意味着什么,但它看起来像是EA已经处理过的条数。如果这是真的,那么我需要检查的就是if(Bars > BarsCalculated),如果是真的就去做我的处理,否则就返回?我是不是错过了什么?

谢谢。

伊恩

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • 2010.06.09
  • Samuel
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
 


在你的EA中,在OnTick函数的开头,试试这个。

// 我们将使用静态的Old_Time变量来提供交易时间。

// 在每次执行OnTick函数时,我们将检查当前条形时间和保存的时间。

// 如果条形时间不等于保存的时间,则表明我们有一个新的刻度。


static datetime Old_Time;

datetime New_Time[1];

bool IsNewBar=false;


//复制最后一个小节的时间到元素New_Time[0]。

int copied=CopyTime(_Symbol,_Period,0,1,New_Time)。

if(copied>0) // ok,数据已经被成功复制了。

{

if(Old_Time!=New_Time[0]) // 如果旧的时间不等于新的酒吧时间

{

IsNewBar=true; // 如果不是第一次调用,则新的条形图已经出现。

如果(MQL5InfoInteger(MQL5_DEBUGGING))Print("We have new bar here" ,New_Time[0], " old time was" ,Old_Time);

Old_Time=New_Time[0]; //保存酒吧时间。

}

}

否则

{

Alert("复制历史时间数据时出错,错误=",GetLastError())。

ResetLastError()。

返回。

}


如果(IsNewBar==false)

{

返回。

}

//---- 我们是否有足够的条形图可以使用?

int Mybars=Bars(_Symbol,_Period);

if(Mybars<60) // 如果总条数少于60条

{

Alert("我们少于60条,EA现在将退出!!")。

返回。

}


 

嗨,oneillj。

我以为我已经回复了,但似乎已经消失在互联网上了。

你提供的代码片段几乎就是我最初抱怨的那个。为什么这么复杂?我只是觉得一定有一个更简单的方法。我想出了这个办法,它似乎是有效的。如果我说错了,请纠正我。(我也尝试过使用BarsCalculated,但它总是等于Bars,所以测试结果总是错误的)。

static int LastBarCount = 0;
如果(Bars(_Symbol, _Period) > LastBarCount)
LastBarCount = Bars(_Symbol, _Period);
否则
返回。

;-)伊恩
Documentation on MQL5: Timeseries and Indicators Access / BarsCalculated
  • www.mql5.com
Timeseries and Indicators Access / BarsCalculated - Documentation on MQL5
 
我用这个...
static int BARS;
//+------------------------------------------------------------------+
//| NewBar function                                                  |
//+------------------------------------------------------------------+
bool IsNewBar()
   {
      if(BARS!=Bars(_Symbol,_Period))
        {
            BARS=Bars(_Symbol,_Period);
            return(true);
        }
      return(false);
   }
 
"New Bar" Event Handler
  • 2010.10.11
  • Konstantin Gruzdev
  • www.mql5.com
MQL5 programming language is capable of solving problems on a brand new level. Even those tasks, that already have such solutions, thanks to object oriented programming can rise to a higher level. In this article we take a specially simple example of checking new bar on a chart, that was transformed into rather powerful and versatile tool. What tool? Find out in this article.
 
ssn:
我使用这个方法...
这种方法的风险在于,如果你的符号的价格数据不是从1971年1月4日开始的,那么当专家连接到图表时,任何加载的历史数据 都会被读作一个新的条形图......
 
与本主题无关的评论已被移至"Can mql5 be used in MT4 ?".
 

iTime函数 给出了酒吧的时间。

当它发生变化时,就是一个新的条形图。

 
Nathan5:

iTime函数给出了酒吧的时间。

当它发生变化时,就是一个新的条形图。

本主题是关于mql5的。没有iTime()函数
 

你也可以试试这个。


// Rates structure array for last two bars 
   MqlRates mrate[2];                 
   CopyRates(Symbol(), Period(), 0, 2, mrate);

// NEW BAR CHECK.
//---------------
   static double   dBar_Open;     
   static double   dBar_High;
   static double   dBar_Low;
   static double   dBar_Close;
   static long     lBar_Volume;
   static datetime nBar_Time;

// Boolean for new BAR confirmation. 
   bool bStart_NewBar = false;

// Check if the price data has changed tov the previous bar.   
   if(mrate[0].open != dBar_Open || mrate[0].high != dBar_High || mrate[0].low != dBar_Low || mrate[0].close != dBar_Close || mrate[0].tick_volume != lBar_Volume || mrate[0].time != nBar_Time)
         {
         bStart_NewBar = true; // A new BAR has appeared!        

// Update the new BAR data.     
         dBar_Open   = mrate[0].open;      
         dBar_High   = mrate[0].high;
         dBar_Low    = mrate[0].low;
         dBar_Close  = mrate[0].close;                 
         lBar_Volume = mrate[0].tick_volume;
         nBar_Time   = mrate[0].time;
         }

// Check if a new bar has formed.   
   if(bStart_NewBar == true)
         {
         
// Your code.          
         
         }
 
Stephen Njuki:
我使用这个...
遗憾的是,这在MQL5中似乎不起作用。