Stock data from Yahoo into Matlab

Posted on February 21, 2013

0


1. Save the following Matlab code as stock_data.m, say in C:\Desktop.

2. Open Matlab and change the current directory to C:\Desktop.

3. At the Matlab Command >> type

    >>[hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = stock_data(‘GOOG’);

Here, GOOG is the stock symbol for Google Inc.

4. Among others, the above will return hist_close as a (column) vector of daily closing stock prices starting from the 1st trading day of 2005 to the present day. Depending on your needs, you may wish to change the start year on Line 06 below. We can later use hist_close to analyse the continuously compounnded returns of the stock.

stock_data.m


% Script to Retrieve Historical Stock Data from Yahoo! Finance
% modified from LuminousLogic.com
function [hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = stock_data(stock_symbol)

% Define starting year (the further back in time, the longer it takes to download)
start_year = '2005';

% Get current date
[this_year, this_month, this_day, dummy, dummy, dummy] = datevec(date);

% Build URL string
url_string = 'http://ichart.finance.yahoo.com/table.csv?';
url_string = strcat(url_string, 's=', upper(stock_symbol));
url_string = strcat(url_string, '&d=', num2str(this_month-1));
url_string = strcat(url_string, '&e=', num2str(this_day));
url_string = strcat(url_string, '&f=', num2str(this_year));
url_string = strcat(url_string, '&g=d&a=0&b=1&c=', start_year);
url_string = strcat(url_string, '&ignore.csv');

% Open a connection to the URL and retrieve data into a buffer
buffer = java.io.BufferedReader(...
         java.io.InputStreamReader(...
         openStream(...
         java.net.URL(url_string))));

% Read the first line (a header) and discard
dummy   = readLine(buffer);

% Read all remaining lines in buffer
ptr = 1;
while 1

% Read line
buff_line = char(readLine(buffer));

% Break if this is the end
if length(buff_line)<3, break; end

% Find comma delimiter locations
commas    = find(buff_line== ',');

% Extract high, low, open, close, etc. from string
DATEvar   = buff_line(1:commas(1)-1);
OPENvar   = str2num( buff_line(commas(1)+1:commas(2)-1));
HIGHvar   = str2num( buff_line(commas(2)+1:commas(3)-1));
LOWvar    = str2num( buff_line(commas(3)+1:commas(4)-1));
CLOSEvar  = str2num( buff_line(commas(4)+1:commas(5)-1));
VOLvar    = str2num( buff_line(commas(5)+1:commas(6)-1));
adj_close = str2num( buff_line(commas(6)+1:end));

% Adjust for dividends, splits, etc.
DATEtemp{ptr,1} = DATEvar;
OPENtemp(ptr,1) = OPENvar  * adj_close / CLOSEvar;
HIGHtemp(ptr,1) = HIGHvar  * adj_close / CLOSEvar;
LOWtemp (ptr,1) = LOWvar   * adj_close / CLOSEvar;
CLOSEtemp(ptr,1)= CLOSEvar * adj_close / CLOSEvar;
VOLtemp(ptr,1)  = VOLvar;

ptr = ptr + 1;
end

% Reverse to normal chronological order, so 1st entry is oldest data point
hist_date  = flipud(DATEtemp);
hist_open  = flipud(OPENtemp);
hist_high  = flipud(HIGHtemp);
hist_low   = flipud(LOWtemp);
hist_close = flipud(CLOSEtemp);
hist_vol   = flipud(VOLtemp);
Advertisements