A guard that lives in a module variable is decoration. It looks like a limit, it logs like a limit, and it disappears the moment the process restarts.
This is the failure I keep finding in trading automation, and it is hard to see because nothing breaks loudly. The EA recompiles, you switch charts, the terminal restarts after an update, and the guard comes back with a clean counter. The limit you thought was spending the whole day starts over.
Config is not state
Configuration is what the limit should be. State is how much of it you have already used today. They live in different places, and only one of them survives a restart.
If your guard reads maxDailyLoss from config on init and starts a counter at zero, you have a limit but no memory. The restart does not violate the limit. It resets the thing that tracks it.
The test takes five minutes
- Set a limit small enough to trip during normal trading.
- Trip it. Try to send an order. It should be refused.
- Kill the terminal process, not just the EA. Keep the position open.
- Reopen the terminal and send the same order.
If the order goes through, the guard was in memory. If it is refused, the guard is persisted. Same request, same broker, different result, and that difference is the whole test.
What actually needs to be persisted
- the trading day, taken from the broker's server clock rather than the local one
- the start-of-day balance used as the baseline
- whether the day is already blocked
- the consecutive loss count, if lot sizing depends on it
- any scaling factor you apply to position size
Write first, then act. If the write fails, refuse the order instead of trading with unknown state.
The trading day detail matters more than it looks. Persist the blocked flag without the day, and the limit either never expires or never applies, depending on how you compare.
Why this is easy to miss
None of these failures produce a red line. A guard that resets does not throw an error. It just allows an order it should have refused. The account keeps trading, the log stays clean, and the loss looks like bad luck.
Which is why the test is worth running once. It is five minutes, and it tells you which kind of guard you have.
One question. If you killed your terminal right now and reopened it, would it refuse the next order? If you have not tested it, you do not know.
Top comments (0)