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.
Arnold Holm
Challenge Pre-Check: know before you risk. Your rules, tested against prop limits. Historical simulation, not a forecast.
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.