Indicate a new bar - page 2

 
Doerk Hilger:

The easiest way:

static datetime tlastbar=0;

datetime tnewbar=iTime(NULL,PERIOD_CURRENT,0);

bool isnewbar=tnewbar!=tlastbar;

tlastbar=tnewbar; 

As far as I know the topic is for mql5. Your code is mql4 only.
 
Willbur:

Here is my final version.  

In fact I'm a bit worried becuase it is so easy.

Alain: It would be nice if you would issue the blessing.

// -----------------------------------------------------------------------
bool NewBar(void)
{
bool iNewBar = false;
static double currPeriodProgress = 0;

   if(MathMod(TimeCurrent(),PeriodSeconds()) < currPeriodProgress) iNewBar = true;

   currPeriodProgress = MathMod(TimeCurrent(),PeriodSeconds());

   return(iNewBar);
}
// ------------------------------------------------------------------------
void OnTick()
{
    if(NewBar())     PlaySound("tick.wav");


Greetings from Cologne
    Willbur

The problem is it can only be used from OnTick(). From other event handlers you are never sure if TimeCurrent() is related to the symbol you want.

And I suggest you to never do the same calculation twice :

bool NewBar(void)
  {
   bool iNewBar=false;
   static double currPeriodProgress=0;

   double lastPeriodProgress=MathMod(TimeCurrent(),PeriodSeconds());

   if(lastPeriodProgress<currPeriodProgress) iNewBar=true;

   currPeriodProgress=lastPeriodProgress;

   return(iNewBar);
  }
 
Alain Verleyen:
As far as I know the topic is for mql5. Your code is mql4 only.
What do you mean by that? Its just a snipped and can be included whereever one wants, eg within his NewBar() function. And imo its the simplest and quickest, reliable solution. I included this in a derivate of CSymbolInfo class for all timeframes separately and it works perfect. 
 

I used these in MQL4

 

//global
long Bars_in_Chart;

   // detected new bar
   if(Bars_in_Chart<Bars)
   {
      Bars_in_Chart=Bars;
   }
 
Doerk Hilger:
What do you mean by that? Its just a snipped and can be included whereever one wants, eg within his NewBar() function. And imo its the simplest and quickest, reliable solution. I included this in a derivate of CSymbolInfo class for all timeframes separately and it works perfect. 
There is no iTime() function in mql5.
 
Alain Verleyen:
There is no iTime() function in mql5.
MT5, really? But MQL5 is not only MT5, that is at least that how I understood it always, cause MT4 uses the MQL5 compiler. But in fact did not know that, I use mainly MT4. Anyway, thanks for the info about the restriction.
 
Doerk Hilger:
MT5, really? But MQL5 is not only MT5, that is at least that how I understood it always, cause MT4 uses the MQL5 compiler. But in fact did not know that, I use mainly MT4. Anyway, thanks for the info about the restriction.
No, mql4 is for MT4, mql5 is for MT5. They have a subset of functions in common, but that's 2 different languages.
 

MQL4 iTime() it's easy and understandable for traders as is with most of MQL4 other Functions.

MQL5 is different, you need a degree in computer science to code that, and that is what puts most people down.

Heres an example i encountered last week, when i wanted to start and convert my MT4 EA's to MT5 and the solution i found.


 
Alain Verleyen:
No, mql4 is for MT4, mql5 is for MT5. They have a subset of functions in common, but that's 2 different languages.

The language is the same but they have a small subset of functions not in common ;) Anyway, youre probably right with this definition. MQ did a lot to generate confusion at this point. 

 
Doerk Hilger:
MT5, really? But MQL5 is not only MT5, that is at least that how I understood it always, cause MT4 uses the MQL5 compiler. But in fact did not know that, I use mainly MT4. Anyway, thanks for the info about the restriction.
Do you know if there is a public library/class which implements the old iClose, iTime ... functions just for compatibility purposes? Would make sense during a step by step conversion.