Can anyone explain how to implement the objective function?

I tried to optimize based on different performance metrics but the results don't make sense to me.

As an example I tried to find the value that maximizes R2:

var objective()
{
if(!NumWinTotal && !NumLossTotal) return 0.;
return ThisPerformance.vR2;
}

I also set(PEAK) and used Commission=Slippage=RollLong=RollShort=0;


The optimize chart shows that the best parameter has an R2 value of about .75.

However, running a test with this parameter results in a performance of R2 = 0.87.

I also tried to insert other parameter values and tested and have never been able to match test report with the optimize chart.


Can anyone shed some light on how to properly utilize the objective function?

Thanks.