image thumbnail

Output set of stock prices from yahoo finance as a matrix with header and dates into excel

by

 

29 May 2012 (Updated )

Output historical stock prices for a basket of stocks and given period as a matrix with header

[B,C,name2]=M_Tri_EXL(Start_Date,End_Date,ty)
function [B,C,name2]=M_Tri_EXL(Start_Date,End_Date,ty)

%% Document Title
% Download share prices ( daily, weekly or monthly) from yahoo finance for 
% a set of companies and save them into an Excel file as a Matrix

%% Author
% Author : Haidar Haidar University of Sussex Email: h.haidar@sussex.ac.uk
% Date:    Jan - 2011, homepage: http://www.maths.sussex.ac.uk/~hh56

%% Required

%  Datafeed Toolbox
%  Financial Toolbox 

%% Inputs

% ty        : is an option, daily prices are downloaded if left blank or 0
%           : -1 if monthly prices are required.
%           : 1,2,..,7 if weekly prices are required, 1 for Fridays's, 2 for
%           Saturday's,.. , and 7 for Thursdays.
% Start_Date: Is the first day of the downloading period.
% End_Date  : Is the last day of the downloading period.
% Date can be '29-Jan-2004', '01/29/2000' or ,'29 Jan 2002'

%% Inputs
% Write the symbols of the required companies in the this file <SP.txt>
% After typing a symbol leave one space and then Enter for a new line to
% add another symbol. The last line should be blank.

%% Output
% <S_AEXL.xlsx> ,   
% B : Share prices,   
% C : Dates,  
% name2 : List of symbols that have prices during the provided period.

%% Example

% [B,C,name2]=M_Tri_EXL('01-Jan-2004','29-Feb-2012'); for daily prices
% [B,C,name2]=M_Tri_EXL('01-Jan-2004','29-Feb-2012',-1); for monthly prices
% [B,C,name2]=M_Tri_EXL('01-Jan-2004','29-Feb-2012',2); for daily 
% (Tuesdays) prices

%% Code

if nargin==2
    ty=0;
end

[name]=Tri_Name;
    
SN=length(name);
connect = yahoo;

a=isconnection(connect);

if a~=1
    connect =yahoo('http://download.finance.yahoo.com');
    a=isconnection(connect);
end
if a~=1
    connect= yahoo('http://quote.yahoo.com');
    a=isconnection(connect);
end

if a==1
    B=[];
    ok=1;
    Nm=[];
    for i=1:SN
        name(i)
        try
            A=fetch(connect,name(i),'Adj Close',Start_Date,End_Date);
        catch
            A=[];
        end
            s=size(B,1);
            if ok==1
                B=[B A(end:-1:1,1)];
                ok=0;
            end
            sa=size(A,1);
            if sa>s
                B=[A(end:-1:1,1)];
                B=[B A(end:-1:1,2)];
                Nm=i;
            elseif sa==s
                Nm=[Nm i];
                B=[B A(end:-1:1,2)];                
            end
%         end
    end

    fts1 = fints(B);
    fts1.freq = 'daily';
    
    if ty==-1
        fts1=tomonthly(fts1);
    elseif ty>=1 && ty<=7 && rem(ty,1)==0
        fts1=toweekly(fts1,'EOW',ty-1);
    end
    B= fts2mat(fts1, 1);    

    C=datestr(B(:,1));
    B(:,1)=[]; 
    C=cellstr(C);
    name2=name(Nm);
%     warning off MATLAB:xlswrite:AddSheet;
    xlswrite('S_AEXL.xlsx', [{'Date'} name2]);
    xlswrite('S_AEXL.xlsx', C,'Sheet1','A2');
    xlswrite('S_AEXL.xlsx', B,'Sheet1','B2');

else
        B='No connection with yahoo finance';
end

return


function [name]=Tri_Name
fid = fopen('SP.txt','r+')
A = fscanf(fid,'%c');
size(A)
H = strfind(A,' ');
y=H+3;
y(end)=[];
y=[1 y];
SH=length(H);
name={};
    for i=1:SH
        name(i)=cellstr(A(y(i):(H(i)-1)));
    end
fclose(fid);
return

Contact us