New Bar function gives false positive on EA restart

 

Hello!

I have a new bar function that I found on the internet.

Here it is

bool NewBar()
{
   static datetime lastbar = 0;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
}  

It works very nicely except that it gives a false positive if I change the EA parameters or install a new version of the EA. I have read somewhere on here somebody suggesting a need to add something to deinit. Ideally I'd like to have something within the function itself if at all possible as I like things tidy but any solution to this problem would be appreciated.

Steve
 

 
bool NewBar()
{
   static datetime lastbar = 0;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      if(lastbar==0){
        lastbar=curbar;
        return (false);
      }
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }

//z

 
zzuegg:

//z

Thanks for the quick response. Obvious when you see it :)
 
Stevetrade:
Thanks for the quick response. Obvious when you see it :)



Unfortunately I've just put this onto the system in question and it still triggered a false positive.

 

still on the firstbar after adding the ea?

or when you are switching period with the EA attached?

 
zzuegg:

still on the firstbar after adding the ea?

or when you are switching period with the EA attached?



I don't switch periods on these charts.

I just put your new version into my code,  installed it and restarted MT4 just after I'd had a trade but while all the requirements were met to open a trade ( other than the fact that it wasn't a new bar ) and it triggered a new trade. Is it because of the restart, does it consider it a new bar?

 
that code works fine for me. used it in several EA's. are you shure there is no other bug in your EA?