simple linear regression slope calculation

183 views (last 30 days)
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,000x1 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.

Accepted Answer

Star Strider
Star Strider on 27 Aug 2016
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)’.
  6 Comments
Star Strider
Star Strider on 27 Aug 2016
Edited: Star Strider on 28 Aug 2016
If you have one column of prices per minute, then I assume the row is the day, and the last column is the closing price. If you want to do a linear regression of closing price as a function of the day, do something like this:
Days = [1:200]'; % ‘Days’ Vector
Hrs = 7; % Hours In A Trading Day (Guess)
Prices = randi(25, 200, Hrs*60); % Create Data (200x420)
Prices = bsxfun(@plus, Prices, 50-25*sin(pi*Days/100)); % Add Trend
StartDay = 50;
StopDay = 150;
DaysRange = StartDay:StopDay;
Select_x = Days(DaysRange); % Select Days Of Desired Days Range
Select_y = Prices(DaysRange, end); % Select Closing Price For Desired Days Range
coefs = polyfit(Select_x, Select_y, 1);
slope = coefs(1);
figure(1)
plot(Days, Prices(:,end))
hold on
plot(DaysRange, polyval(coefs, DaysRange), '-r')
hold off
grid
text(mean(DaysRange), polyval(coefs, mean(DaysRange)), sprintf('\\bfSlope = %.3f \\rightarrow \\rm', slope), 'HorizontalAlignment','right')
With the appropriate changes to work with your data, this should work.
—————————————————————————————————————————————————
EDIT
With the data in the ‘lr.xlsx’ file you posted, this code should get you started:
[d,s,r] = xlsread('kenan alici lr.xlsx');
T = datenum(s(2:end,1))+d(:,1); % Combine Date & Time Columns
Close = d(:,5);
Wndw = 10; % Number Of Points Included In Linear Regression
for k1 = 1:size(T,1)-Wndw
B = [ones(Wndw,1) T(k1:k1+Wndw-1)]\Close(k1:k1+Wndw-1); % Estimate Linear Regression Coefficients
Slope(k1) = B(2); % Slope
TNC(k1) = T(k1+1); % Time Of Next Close
NextClose(k1) = [1 TNC(k1)]*B; % Calculate Next Trading Day Close
end
figure(1)
subplot(2,1,1)
plot(T, Close, '-b')
hold on
plot(TNC, NextClose, '-r')
hold off
grid
datetick('x', 'yyyy.mm.dd')
set(gca, 'XTickLabelRotation', 30, 'FontSize',8)
legend('Close', 'Predicted Close')
subplot(2,1,2)
plot(TNC, Slope, '-g')
grid
datetick('x', 'yyyy.mm.dd')
set(gca, 'XTickLabelRotation', 30, 'FontSize',8)
legend('Slope')
Note that I have no idea what the conventions are for these analyses. I have no background or experience in econometrics or as a technical analyst, and never used technical analyses when I was investing (and I did quite well without them).

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Aug 2016
Try this full demo:
% Demo to illustrate how to use the polyfit routine to fit data to a polynomial
% and to use polyval() to get estimated (fitted) data from the coefficients that polyfit() returns.
% Demo first uses a linear fit.
% Reports the slope of the fitted line in the last 50 points.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%============= LINEAR FIT ===================================
numberOfPoints = 100;
x = 1 : numberOfPoints; % Make numberOfPoints samples along the x axis
% Create a sample training set, a linear relation, with noise
% y is your actual data. For this demo I'm going to simulate some noisy points along a line.
slope = 1.5;
intercept = -1;
noiseAmplitude = 15;
y = slope .* x + intercept + noiseAmplitude * rand(1, length(x));
% Now we have sample, noisy y values that we will fit a line through.
% Plot the training set of data (our noisy y values).
plot(x, y, 'ro', 'MarkerSize', 8, 'LineWidth', 2);
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Linear Fit', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Determine what points to use to fit to the last 50 points in the data.
firstIndex = numberOfPoints - 49;
lastIndex = numberOfPoints;
% Do the regression with polyfit. Fit a straight line through the noisy y values.
coefficients = polyfit(x(firstIndex:lastIndex), y(firstIndex:lastIndex), 1)
% The x coefficient, slope, is coefficients(1).
% The constant, the intercept, is coefficients(2).
% Make fit. It does NOT need to have the same
% number of elements as your training set,
% or the same range, though it could if you want.
% Make 50 fitted samples going from firstIndex to lastIndex.
% Note: the number does not need to be the same number of points as your training data.
xFit = linspace(firstIndex, lastIndex, 50);
% Get the estimated values with polyval()
yFit = polyval(coefficients, xFit);
% Plot the fit
hold on;
plot(xFit, yFit, 'b.-', 'MarkerSize', 15, 'LineWidth', 1);
legend('Training Set', 'Fit', 'Location', 'Northwest');
slope = coefficients(1);
message = sprintf('The slope = %f', slope);
uiwait(helpdlg(message));
  3 Comments
Image Analyst
Image Analyst on 28 Jan 2018
Thank you. Good suggestion. In the meantime, you can use S and delta:
[p,S] = polyfit(x,y,n) also returns a structure S that can be used as an input to polyval to obtain error estimates.
[y,delta] = polyval(p,x,S) uses the optional output structure S generated by polyfit to generate error estimates delta. delta is an estimate of the standard deviation of the error in predicting a future observation at x by p(x). If the coefficients in p are least squares estimates computed by polyfit, and the errors in the data input to polyfit are independent, normal, and have constant variance, then y±delta contains at least 50% of the predictions of future observations at x.
Ankan Biswas
Ankan Biswas on 8 Feb 2018
Edited: Ankan Biswas on 8 Feb 2018
Thanks for the Answer Image Analyst. From this (standard deviation of th error, delta) is there any way to calculate R-sq or p value of the estimate? Or is there a better way or a function to do that? It would be really helpful if we can achieve this using polyfit function as it is very easy to understand and follow. There seems to be so many functions for linear regression, however understanding and implementation of them seems to be little hazy.

Sign in to comment.

Categories

Find more on Financial Data Analytics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!