# R-square optimization criteria

Questions about MultiCharts and user contributed studies.
Posts: 63
Joined: 13 Sep 2016
Has thanked: 30 times
Been thanked: 8 times

### R-square optimization criteria

I normally use Net Profit, Return on Account and Profit Factor as criteria for my optimization.

Those criteria aren't bad, but I want to place more emphasis on stable returns over the whole period. This isn't just the smoothness (that is solved by ROA or Sharpe) but rather having a relatively constant slope of equity curve throughout the period.

I believe the coefficient of determination (R-Squared) of the equity curve can be useful for that. Then, perhaps multiply it by the Net Profit and have (P * R^2) as the criteria.

For that, I would like to use a custom signal, using the SetCustomFitnessValue, as explained in https://www.multicharts.com/trading-sof ... timization

Now for my question - is there already such a signal available (or something similar) that I can use, or should I write it from scratch?

Posts: 63
Joined: 13 Sep 2016
Has thanked: 30 times
Been thanked: 8 times

### Re: R-square optimization criteria

One by one, not all at once please

Anna MultiCharts
Posts: 448
Joined: 14 Jul 2017
Has thanked: 30 times
Been thanked: 111 times

### Re: R-square optimization criteria

I’m afraid there’s no such pre-built signal or any other study. You will need to create it yourself or search for it on the Internet, as there’s quite a number of free available studies written in EasyLanguage/PowerLanguage.

Posts: 63
Joined: 13 Sep 2016
Has thanked: 30 times
Been thanked: 8 times

### Re: R-square optimization criteria

Thanks, I wrote it myself. My code is below. Simply, it calculates R squared by Pearson correlation on equity with a vector of running numbers (1 onwards). Hope it helps others.

Code: Select all

``` variables: maxIndex(0), curIndex(0), curXval(0), curYval(0), sumX(0), sumY(0), sumOfxSqr(0), sumOfySqr(0), sumOfxy(0), numerator(0), denominator(0), retVal(0); arrays: equity[](0); maxIndex = array_getmaxindex(equity); equity[maxIndex] = i_OpenEquity; if not LastBarOnChart then array_setmaxindex(equity,maxIndex+1) else begin // Calculate r squared for curIndex = 0 to maxIndex begin curXval = curIndex + 1; // x values are simply running numbers starting from 1 onwards curYval = equity[curIndex]; sumX = sumX + curXval; sumY = sumY + curYval; sumOfxSqr = sumOfxSqr + curXval * curXval; sumOfySqr = sumOfySqr + curYval * curYval; sumOfxy = sumOfxy + curXval * curYval; denominator = (maxIndex * sumOfxSqr - sumX * sumX) * (maxIndex * sumOfySqr - sumY * sumY); if denominator <> 0 then begin numerator = maxIndex * maxIndex * sumOfxy * sumOfxy + sumX * sumX * sumY * sumY - 2 * maxIndex * sumOfxy * sumX * sumY; retVal = numerator / denominator; end else retVal = 0; end; end; G_R_Squared_Equity = retVal; ```

wilkinsw
Posts: 540
Joined: 21 Apr 2013
Has thanked: 137 times
Been thanked: 88 times

### Re: R-square optimization criteria

This looks great!

How have you found your optimization results since using this fitness function?

Posts: 30
Joined: 02 Sep 2014
Has thanked: 2 times
Been thanked: 1 time

### Re: R-square optimization criteria

I would second to have this appear on the detailed equity curve simulations. The graph speaks 1000X what a single digit can.
But despite having this feature, interpreting optimization results goes way beyond simply finding the best number.