Testing strategies

 

How do others ensure their MQL5 code is functionally correctly?

Coming from a software engineering background, I feel quite limited in the software testing options available for this. I have mostly manually tested my MQL5 code to date; however, as it grows in complexity, this (and the financial nature of it) makes me quite uncomfortable.

I'll share my approach and would love to hear from others about how they approach this problem!

Unit testing

I can't really see how to incorporate unit testing or even how to write a test harness.

There are some examples in the standard library that are implemented as Scripts, and I guess this is workable. But it feels like there will be a lot of boilerplate we need to provide to use this approach.

Scripts/UnitTests/*/*.mq5

I've also found this third-party library: https://www.mql5.com/en/code/33089

I was really hoping to find something standard though. Am I missing something?

End-to-end testing

This is where I'm focusing now. While I'd love good test coverage with unit tests, I've at least tried to catch if/when I accidentally break one of my Expert Advisors (EA).

My thought process is this.

  • Fix the bulk of the parameters for a backtest (symbol, time period, timeframe, EA parameters, etc.)
  • Run a backtest with version X of an EA, save the report
  • Run a backtest with version Y of an EA, save the report
  • Compare the reports from versions X and Y. Are they exactly the same? Pretty close? Totally different?
    • I consider the sequence of trades and all of the reported Key Performance Indicators

While this won't tell me what has gone wrong, it does let me know if the behaviour of my EA has changed, and how (to a degree).

Here is an example for one of my EAs, comparing the same parameters for two versions (v1.05 and v1.06). I first had to manually run the strategy tester and export Open XML reports for each run. I wrote a small Python program to take those two reports, compare them, and provide a summary report in HTML.

python Tests/compare_reports.py EA1_XAUUSD_v1.04.xlsx EA1_XAUUSD_v1.05.xlsx

It's crude, but it does force me to think through when the behaviour of my EAs change.



How do other people think about software testing and quality control?

MQLUnit - Tiny Unit Tests Framework For Complex Expert Advisors
MQLUnit - Tiny Unit Tests Framework For Complex Expert Advisors
  • 2021.01.18
  • www.mql5.com
This unit test frameworks eases the development of unit tests for more complex expert advisor programs. The MQL5 developer can test single components. The test framework starts the strategy tester so that there is test data available if required. I am using the framework to do test driven development (TDD) on my MQL5 programs.
 
Here is another unit testing library: https://github.com/MQLLAB/MQLUNIT

However, these only ensure code correctness and are difficult to use for trading-specific testing.

Do you save each change to the EA code as a separate file? Or do you modify the same code back and forth each time?
 
hini #:
Here is another unit testing library: https://github.com/MQLLAB/MQLUNIT

However, these only ensure code correctness and are difficult to use for trading-specific testing.

Do you save each change to the EA code as a separate file? Or do you modify the same code back and forth each time?

Thanks for sharing. Unfortunately, that library seems to be MQL4 only. I stick with MQL5 exclusively these days but hopefully that could help someone!

I track my whole MQL5 directory as a monolithic git repository, and so if I make changes to a single EA I'd just save changes to the relevant file(s).

Using git means I have full version history. I create a tag for any release so I can easily go back to an earlier version if I need to.