Unit testing the boring bits of your EA before they touch money
Most EA bugs I've seen weren't in the clever part. They were in the plumbing. Lot size rounding, pip size on JPY pairs vs everything else, a stop calculated on the wrong side for shorts, a session check that breaks across midnight.
These are exactly the kind of things you can test without the market. Pull the helper functions out so they take inputs and return outputs, then feed them known cases and check the answers. Lot size for a given balance, risk and stop on EURUSD, USDJPY and gold. Stop price for a long and a short. Session check at 23:59 and 00:01. That sort of thing.
In MQL it's a bit clunky, there's no proper test framework built in, but a script that calls your functions with fixed inputs and prints pass or fail does the job. In Python it's easy with pytest.
I'd add a test every time you find a bug. Then that bug can't quietly come back after you change something else.
It sounds like overkill for a small EA. It isn't, honestly. The stuff that blows up accounts is usually one wrong sign or one wrong multiplier, and tests catch those in seconds.
Unit testing the boring bits of your EA before they touch money
Re: Unit testing the boring bits of your EA before they touch money
A good set of cases to include is the edge of the broker's limits. Minimum lot, maximum lot, lot step, and stop level. If your risk calc gives 0.004 lots and the minimum is 0.01, what does the function do? Round up and over-risk, or skip the trade? Either can be right, but it should be a choice you made, not an accident. Same for a stop distance that comes out smaller than the broker's stop level. Write the expected answer in the test so the next time you change the function, you'll see straight away if the behaviour shifted.
It’s Fairman 
Re: Unit testing the boring bits of your EA before they touch money
For anything that depends on account currency, test with a couple of different ones. Pip value for a USD account on EURGBP isn't the same as for a GBP account, and code that assumes USD everywhere tends to break the moment someone runs it on a EUR or GBP account. If you share your EA or sell it, that's going to happen sooner or later, so build those cases in early. JPY quote pairs and gold deserve their own test cases too, since pip size and contract size are where a lot of these bugs live. One wrong decimal there can multiply risk by ten or a hundred.
It’s Fairman 
Re: Unit testing the boring bits of your EA before they touch money
Testing rounding is another boring but important case. Prices and lots need to be rounded to the symbol's digits and lot step, and the rounding rules differ. If your stop calculation gives 1.085347 on a five digit pair, the order needs 1.08535 or 1.08534, and rounding the wrong way can put the stop inside the stop level and get the order rejected. A few tests with awkward values catch this before it shows up live. The MathRound and NormalizeDouble functions behave in ways that surprise people, so testing them with real values from your broker's symbols is a good idea. It takes minutes and removes a whole class of rejected orders.
It’s Fairman 
-
FXS Prop Desk
- Posts: 1689
- Joined: Wed Oct 07, 2026 9:52 am
Re: Unit testing the boring bits of your EA before they touch money
The plumbing is exactly where bugs hide. Midnight crossing in session checks is a particularly common one. A session from 22:00 to 02:00 needs different logic than one from 08:00 to 12:00, and many EAs get it wrong.
Writing a few test cases with known inputs and expected outputs, even in a simple script, catches most of these before they reach a live account. It also makes it safer to change the code later.
Writing a few test cases with known inputs and expected outputs, even in a simple script, catches most of these before they reach a live account. It also makes it safer to change the code later.
FXS Official Team | The Idea Lab & Strategy
Re: Unit testing the boring bits of your EA before they touch money
For MQL code, a simple approach is a separate script that calls your functions with known inputs and prints whether the results match expected values. It doesn't need a framework. Running it after any change to the functions catches regressions. In Python there's pytest, which is easy to set up. The point isn't to test everything, just the calculations where a silent mistake would cost money, like position sizing and stop placement. Once the tests exist, running them after every edit becomes a habit, and they take seconds. They also make it less scary to refactor old code, since you can see straight away if something you didn't mean to change has broken.
It’s Fairman 