Super newbie here and as per this thread am working on developing the code to back test a momentum strategy. I believe I was able to track down the code the author used on this page. Based on my rudimentary analysis of this code, I believe it will get me to an adjusted slope which is essentially an annualized exponential slope number * r-squared. Have I read that right?

P.S. There will be errors in here as I have not defined the periods.

//Determine the Adjusted Slope for each stock in the defined universe which is the exponential slope * R-squared for each stock in the defined universe.

public double GetSlope()

{

double n = this.periods -1;

double x, y;

double[] xVals;

double[] yVals;

xVals = new double[this.periods];

yVals = new double[this.periods];

for (int i = 0; i < this.periods ; i++)

{

x = ((int)n- i);

y = Math.Log(inputs[0].LookBack(i));

xVals = x;

yVals = y;

}

double rsquared, yintercept, slope;

LinearRegression(xVals,yVals,0,this.periods - 1,out rsquared, out yintercept,out slope);

double annualSlope;

double dayCount = 250;

annualSlope = ((Math.Pow(Math.Exp(slope), dayCount))-1) *100; // Annualized percentage

//

annualSlope = annualSlope *rsquared;

return annualSlope;

}

public static void LinearRegression(double[] xVals, double[] yVals,

int inclusiveStart, int exclusiveEnd,

out double rsquared, out double yintercept,

out double slope)

{

double sumOfX = 0;

double sumOfY = 0;

double sumOfXSq = 0;

double sumOfYSq = 0;

double ssX = 0;

double ssY = 0;

double sumCodeviates = 0;

double sCo = 0;

double count = exclusiveEnd - inclusiveStart;

for (int ctr = inclusiveStart; ctr < exclusiveEnd; ctr++)

{

double x = xVals[ctr];

double y = yVals[ctr];

sumCodeviates += x * y;

sumOfX += x;

sumOfY += y;

sumOfXSq += x * x;

sumOfYSq += y * y;

}

ssX = sumOfXSq - ((sumOfX * sumOfX) / count);

ssY = sumOfYSq - ((sumOfY * sumOfY) / count);

double RNumerator = (count * sumCodeviates) - (sumOfX * sumOfY);

double RDenom = (count * sumOfXSq - (sumOfX * sumOfX))

* (count * sumOfYSq - (sumOfY * sumOfY));

sCo = sumCodeviates - ((sumOfX * sumOfY) / count);

double meanX = sumOfX / count;

double meanY = sumOfY / count;

double dblR = RNumerator / Math.Sqrt(RDenom);

rsquared = dblR * dblR;

yintercept = meanY - ((sCo / ssX) * meanX);

slope = sCo / ssX;

}

Author