I know this has been talked about before but considering the recent update to the optimization criteria in Strategy Tester for MT5 I figured I'd share an easy way to add custom max criteria to your EAs.
Open MetaQuotes Language Editor (or hit F4) then click crtl + F to find the words "custom functions" in the code. Past your OnTester() function in this section under the header. For instance I prefer to optimize to max balance multiplied by max profit factor. The code I use looks like this:

Now all you need to do is select "custom max" as your optimization criteria and it will optimize your EA according to this function.
Re: onTester()


