Closing trades at end of candle

 

I'm trying a very simple system.

Buy if EMA(5) crosses EMA(13) --> using opening prices

Close all open positions at close of current candle or at open of next candle.

Opening goes ok but somehow it never closes on the same bar.

I know backtesting is flawed but not for simple OHLC data I think/hope...

Can someone please help me with this weird problem?

MetaTrader4 code

 

bumping this one as well to see if anyone can fix it now

 

This is best to program in zerocode.

First, setup EMA 5. Parameters: Method = EMA, Period = 5, Applied Price = PRICE_OPEN

Second, setup EMA 13.

Third, add logics as seen in attached screenshot below.

That's it!

Reason: