Need help with New Bar

 
Hi,

I am using the Time[0] and trying to prevent code execution on the tick and only on a new bar

if ( Time[0] == MyTime )
return(0);
else
{
MyTime = Time[0];
Last_Bar = Last_Bar +1;
}


but when several ticks come very close together the Last_Bar gets incremented
I have also tried this

if ( NewBar == Bars )
return(0);
else
{
NewBar = Bars;
Last_Bar = Last_Bar +1;
}

With the same results can anyone tell me a way so that I can process code only once per bar?

Thanks for your Time
EK
 
once per bar is if(Time[0]==MyTime) guaranteed
insert
LastBar=Bars;


at the end of program before return

 
Hello Slawa,

Thanks for taking the time to answer my question.
But I am not looking for the Bar number of the last bar. In my terms the Last_Bar is the number of a bar for a specified event that has taken place in my code and I am trying to count how many complete bars have passed since then.

But the problem is if an event happend and I set my Last_Bar = 3 and then 5 new bars happen I need a way to set Last_Bar = 8.

But when I cant guatantee that Last_Bar = Last_Bar +1; only happens once then I run into problems.


But with this
if ( Time[0] == MyTime )
return(0);
else
{
MyTime = Time[0];
Last_Bar = Last_Bar +1;
}

Last_Bar gets added many times per single timeframe bar which is what I am trying to avoid.

I hope that I am making sense explaining my point of view.

Thanks
EK
 
Slawa,

You are correct and I am wrong.

What I was doing was declaring a local variable MyTime within the start function
so everything a new tick would happen along the start function fires off and resets MyTime back to zero.

I moved the datetime MyTime = 0; to the global section and now problem solved :)

Thanks
EK
 
cool
Reason: