Calculating half life for mean reverting series

Also you can code just from scratch like below. Code uses ALL bars and produces single value of intercept / slope for all bars.

X = /* independent var */;
Y = /* dependent var */;

bars = BarCount - 1; // use all bars

// calculate mean values
mX = LastValue( MA( X, bars ) );
mY = LastValue( MA( Y, bars ) );

// differences x - mean(x) and y - mean(y)
dX = X - mX;
dY = Y - mY;

// sums of squared differences
sumX2 = LastValue( Sum( dX * dX, bars ) );
sumXY = LastValue( Sum( dX * dY, bars ) );

slope = sumXY / sumX2;
intercept = mY - slope * mX;
9 Likes