The new-bar helper also needs a startup rule. With lastBar initially zero, the first tick after a restart counts as a new bar even if that candle began earlier. I would add one comparison case: restart midway through a candle with an existing position, then check that the closed-bar signal does not create a second entry. Decide explicitly whether startup may act on that old signal or must wait for the next bar.
Good catch Arnold, thanks. You're right, with lastBar at 0 the first tick after attaching or restarting fires as a new bar even mid candle. The fix is to seed it in OnInit with lastBar = iTime(_Symbol, _Period, 0); so the EA waits for the next real bar open. Agreed on the restart comparison case too, I've added it to the checklist.
Your updated OnInit now seeds the current bar, so attaching midway through a candle waits for the next bar. One further negative case is a new bar whose CopyBuffer values are not ready yet. If the helper advances lastBar before a successful read, that closed-bar evaluation can be lost. I would advance the processed-bar marker only after the required values are available, keeping entry deduplication separate.
Arnold Holm
I turn your trading setup into an MT5 bot with your prop firm's daily and max loss rules built in. Try it free on demo first.
The new-bar helper also needs a startup rule. With lastBar initially zero, the first tick after a restart counts as a new bar even if that candle began earlier. I would add one comparison case: restart midway through a candle with an existing position, then check that the closed-bar signal does not create a second entry. Decide explicitly whether startup may act on that old signal or must wait for the next bar.