# 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: 404
Joined: 14 Jul 2017
Has thanked: 28 times
Been thanked: 102 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)   elsebegin                     // 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;`
These users thanked the author TraderWalrus for the post (total 3): wilkinsw
Posts: 510
Joined: 21 Apr 2013
Has thanked: 131 times
Been thanked: 84 times

### Re: R-square optimization criteria

This looks great!

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