Harshdeep Banwait
MEC 102 – Final Project
1.) This program that does the following;
i)Prompt user for -
a) ticker symbol, e.g., AAPL,
b) number of days of data to plot
c) fast simply moving average period, e.g., 10
d) slow simple moving average period, e.g. 40
ii) Downloads data from tickerdata.txt
iii) Computes the points whre the slow SMA crosses the fast SMA
% ----- User inputs BEGIN -----
ticker_symbol=input('Enter the ticker symbol: ','s');
data_period=input('Enter the number of days of data to plot: ');
% ----- The following code will load data into the workspace -----
% ----- GIVE APPROPRIATE PATH FOR tickerdata.txt FILE -----
[date open high low close volume closeadj]=...
get_stock_data('C:\Users\CAMCAD\Desktop\tickerdata.txt',ticker_symbol,data_period);
if length(date)<data_period
data_period=length(date);
msg=['The data is available for ' num2str(data_period) ' days only.'];
disp(msg);
end
% ----------------------------------------------------------------
fsma_period=input('Enter the period (number of days) for computing fast SMA: ');
ssma_period=input('Enter the period (number of days) for computing slow SMA: ');
% ----- Enter the stock data variable for which -----
% ----- u want to compute slow and fast SMAs -----
disp('Stock data available: open, high, low, close and closeadj');
stock_data=input('Enter the stock data name: ');
% ----- User inputs END -----
% ----- Computation of fast and slow SMA using function 'npma' -----
% ----- the function 'npma' is stored as m-file titled 'npma.m' -----
fastSMA=npma(stock_data,fsma_period);
slowSMA=npma(stock_data,ssma_period);
% --------------------------------------------------------------------
% ----- Code to identify crossing points of fast SMA and slow SMA -----
% ----- The function x_point (x_point.m file) is used for this purpose ---
xing_from_below=[];
abscissa_below=[];
xing_from_above=[];
abscissa_above=[];
for i=1:length(date)-1
[absc fSMA f]=x_point(date(i:i+1),fastSMA(i:i+1),slowSMA(i:i+1));
if f==-1
abscissa_below=[abscissa_below absc];
xing_from_below=[xing_from_below fSMA];
elseif f==1
abscissa_above=[abscissa_above absc];
xing_from_above=[xing_from_above fSMA];
end
end
if length(abscissa_below)==0 & length(abscissa_above)==0
plot(date,close,'-k',...
date,slowSMA,'-b',...
date,fastSMA,'-r');
elseif length(abscissa_below)~=0 & length(abscissa_above)==0
plot(date,close,'-k',...
date,slowSMA,'-b',...
date,fastSMA,'-r',...
abscissa_below,xing_from_below,'gs');
elseif length(abscissa_below)==0 & length(abscissa_above)~=0
plot(date,close,'-k',...
date,slowSMA,'-b',...
date,fastSMA,'-r',...
abscissa_above,xing_from_above,'rs');
else
plot(date,close,'-k',...
date,slowSMA,'-b',...
date,fastSMA,'-r',...
abscissa_below,xing_from_below,'gs',...
abscissa_above,xing_from_above,'rs');
end
hold on;
grid on;
set(gca,'XTick',date);
set(gca,'XTickLabel',datestr(date,'mm/dd'));
legend('Closing price','Slow SMA','Fast SMA',0);
plot_title=['Technical Analysis of Stock Price History - ' ticker_symbol];
title(plot_title);
xlabel('Date ->');
ylabel('Stock Closing Price, Slow SMA, Fast SMA ->');
hold off;
2.) Function that analyses the data downloaded from the file tickerdata.txt-
function [dt op hg lo cl vol cladj]=get_stock_data(filename,ts,days);
tslen=length(ts);
fid = fopen(filename, 'r');
flag=0;
ctr=0;
while feof(fid) == 0
tline = fgetl(fid);
if strncmpi(ts,tline,tslen)
flag=1;
break;
end
end
sticker=[];
ndate=[];
nopen=[];
nhigh=[];
nlow=[];
nclose=[];
nvolume=[];
ncloseadj=[];
while flag==1 & ctr<days
rec=textscan(tline,'%s%f%f%f%f%f%f%f',1,'delimiter',',');
ctr=ctr+1;
sticker=[sticker rec{1,1}];
ndate=[ndate rec{1,2}];
nopen=[nopen rec{1,3}];
nhigh=[nhigh rec{1,4}];
nlow=[nlow rec{1,5}];
nclose=[nclose rec{1,6}];
nvolume=[nvolume rec{1,7}];
ncloseadj=[ncloseadj rec{1,8}];
tline = fgetl(fid);
if ~strncmpi(ts,tline,tslen)
flag=0;
end
end
fclose(fid);
dt=ndate;
op=nopen;
hg=nhigh;
lo=nlow;
cl=nclose;
vol=nvolume;
cladj=ncloseadj;
3.) Function that will compute the N-point moving average of a vector of time series data
function SMA_vector=npma(data_vector,n)
sma=[];
day=1;
while day<=length(data_vector)
sum=0;
if day<=n
for i=1:day
sum=sum+data_vector(i);
end
sma=[sma sum/day];
else
for i=(day-n+1):day
sum=sum+data_vector(i);
end
sma=[sma sum/n];
end
day=day+1;
end
SMA_vector=sma;
4.) EXTRA CREDIT
function [day_index fastSMA_xing_point_value flag]=x_point(x,y1,y2)
GREEN=-1;
RED=1;
delta=0.0001;
diff=sign(y1-y2);
if (diff(1)==-1 | diff(1)==0) & diff(2)==1
y11=interp1(x(1:2),y1(1:2),x(1):delta:x(2));
y21=interp1(x(1:2),y2(1:2),x(1):delta:x(2));
d=y11-y21;
for i=1:length(d)
if d(i)>=-0.0002 & d(i)<=0.0002
xp_index=i;
break;
end
end
day_index=x(1)+(xp_index-1)*delta;
fastSMA_xing_point_value=y11(xp_index);
flag=GREEN;
elseif (diff(1)==1 | diff(1)==0) & diff(2)==-1
y11=interp1(x(1:2),y1(1:2),x(1):delta:x(2));
y21=interp1(x(1:2),y2(1:2),x(1):delta:x(2));
d=y11-y21;
for i=1:length(d)
if d(i)>=-0.0002 & d(i)<=0.0002
xp_index=i;
break;
end
end
day_index=x(1)+(xp_index-1)*delta;
fastSMA_xing_point_value=y11(xp_index);
flag=RED;
else
day_index=0;
fastSMA_xing_point_value=0;
flag=0;
end
5.) Enter the ticker symbol: AAPL
Enter the number of days of data to plot: 50
Enter the period (number of days) for computing fast SMA: 40
Enter the period (number of days) for computing slow SMA: 10
Stock data available: open, high, low, close and closeadj
Enter the stock data name: open