MT4 EA working ONLY on previous close/current open prices?

So the mt4 docs say:

Some traders do not wish to depend on particluarities of intrabar modeling, so they create Expert Advisors trading on the bars already completed. The fact that the current price bar is fully completed can only be known when the next one appears. These are the Expert Advisors, for which the mode of “Open Price” modeling is intended.

How do I make sure that I’m writing an EA that only works on the close price of the previously completed bar?

I looked some online and found that you can do:

init() {
int oldbar=Time[0];
}

start() {
if (oldbar != Time[0])
oldbar = Time[0]
// rest of your EA because you just discovered a new bar
}

But with this code in, the EA acts drastically different than not having the code in the EA, even when I choose to backtest with the “open prices only” model.

Anyone have a surefire way to open an order the instant you get a new tick on a new bar AFTER a bar has just closed that gave you a signal?

int start()
{
//----
Trade();
NewBAR();
if (NewBar==false)
return;
return(0);
}
//----------------------------------NEWBAR FUNCTION---------------------------------------------------------//
bool NewBAR(){
static datetime CurrentTime=0;
NewBar=false;
if(CurrentTime!=Time[0]){
CurrentTime=Time[0];
NewBar=True;
return(True);
}

}
//---------------------------------TRADE FUNCTION----------------------------------------------------------//
int Trade(){
if (OrdersTotal()==0 && Period()==ChartPeriod && NewBAR()==True){
//=============================================================================//
THAT SHOULD ANSWER YOUR QUERY. THANKS.

The code inside the trade function should use the 1 subscript to access data e.g. Open[1] or Close[1] to access the bar that just completed. Subscript 0 is the current bar that is still forming.

And strictly speaking the trade code should not be called before calling Newbar() and initially confirming the existence of at least 1 bar which will be your first ‘previous closed bar’ after you get your first new bar. Back testing will always work but if you run the EA LIVE in a fresh install that starts to build local bar history from scratch your EA will trip up if you don’t structure it right.