Unit tests for trading logic: the cheap insurance
Posted: Sat Oct 31, 2026 7:02 pm
Most trading bugs are ordinary software bugs. A sign error in a stop calculation, a wrong index, an off-by-one in a time filter. Because they occur in live money systems, the cost is high. Unit tests catch many of them before deployment.
For a scalping system, test the pieces: position sizing under various account sizes and symbols, stop and target calculation for both directions, spread filters, session filters, and the handling of error codes. Feed in known inputs and assert the expected outputs.
Edge cases matter. What happens at midnight server time? When the spread is zero? When the account has no free margin? When the symbol has a different number of digits?
Even simple tests written in a script catch errors that a visual backtest misses. They also protect against regressions when the code changes.
If the platform's language makes testing awkward, separate the logic into functions that can be tested in an external environment.
Treat the code with the same care as any system that handles money. Because it does.
Which parts of the trading code have automated tests, and which have none?
For a scalping system, test the pieces: position sizing under various account sizes and symbols, stop and target calculation for both directions, spread filters, session filters, and the handling of error codes. Feed in known inputs and assert the expected outputs.
Edge cases matter. What happens at midnight server time? When the spread is zero? When the account has no free margin? When the symbol has a different number of digits?
Even simple tests written in a script catch errors that a visual backtest misses. They also protect against regressions when the code changes.
If the platform's language makes testing awkward, separate the logic into functions that can be tested in an external environment.
Treat the code with the same care as any system that handles money. Because it does.
Which parts of the trading code have automated tests, and which have none?