How to trigger order on the next bar open (or on current bar close)

 
Hello folks,

I am trying to code a simple EA that opens an oder on the opening of a bar (and closes any open order there may be)

I am having trouble coding the action to happen at the close or open of bar.

I was using the following to determine if a new bar opened...


bool newbaropen=false;
if(Time[0]==TimeCurrent()) //thinking that Time[0] would be equal to the current time at bar open.
{
newbaropen=true;
Print("NEW BAR JUST OPENED!");
}

... This seems to work on the visual mode of the strategy tester but does not seem to work on forward demo.

Any ideas on how I can code logic to determine that a bar has closed or a new one has opened would be appreciated.

Luperon
 

Time[0] is the start time of the bar (always 0 seconds with increments of the period)

TimeCurrent() is the current server time.

They will only match if the first tick comes during the first second of the new bar. No bar is painted until a new tick comes. If
the first tick is "late", Time[0] and TimeCurrent() will not match.

init(){

oldBarTime = Time[0];

}

start(){

if(oldBarTime != Time[0]){

oldBarTime = Time[0];

... and do stuff becuase you just disovered a new bar...

}

 
works like a charm! A thousand thanks to you phy.

Luperon
 
Ray:

Time[0] is the start time of the bar (always 0 seconds with increments of the period)

TimeCurrent() is the current server time.

They will only match if the first tick comes during the first second of the new bar. No bar is painted until a new tick comes. If
the first tick is "late", Time[0] and TimeCurrent() will not match.

init(){

oldBarTime = Time[0];

}

start(){

if(oldBarTime != Time[0]){

oldBarTime = Time[0];

... and do stuff becuase you just disovered a new bar...

}

I know it's an old thread but do u know the code for Mql5 pls
 

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          New candle - MQL4 programming forum #3 2014.04.04

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

 
aodunusi: I know it's an old thread but do u know the code for Mql5 pls

There is no predefine Time[] so you use iTime().

Reason: