MATLAB: Simple linear regression slope calculation

linear regression slopetrading algorithm

i am new to using matlab i have a trading system written in another language which uses linear regression slope in the algorithm. i thing that is the formula used by metastock originally. now i am trying to port it into matlab. Matlab has lots of linear regression models formulas etc. but i cannot decide which formula to use to define it . suppose i have 10,000×1 rows of data how can i calculate the slope of linear regression line for last 50 rows.
edit: after much reading i am pretty sure the linearregression line formula in trading softwares means ,for last x points of data linearregression(x) is the solution/prediction of next datapoint using linear least squares method. Still i dont know how can i do this. I am very thankful for everyone trying to help. i hope this clarification will help. i am adding a calculated (by some trading software) price,linearregression and linearregressionslope data here.

Best Answer

You don’t give enough information to write specific code, but the easiest way to do a linear regression would be to use the polyfit (and polyval) functions:
coefs = polyfit(x, y, 1);
The slope will be ‘coefs(1)’.