R^2 of returns in the custom backtest procedure

I’d like to be able to select the r squared of returns as a metric for qualifying optimisations. Has anyone done any coding in this area?

You can take the code from the manual
or this code

and modify it to calculate whatever statistic you need.