Search code examples
excelmatlabfile-ioyahoo-finance

How to download yahoo historical stock data into xls. format via matlab?


I have an xls sheet called Tickers (matrix 1 column 500 rows) with yahoo tickers. I want matlab to download the historical data for last 5 years for each stock ticker into a separate xls spreadsheet and save it in a given directory with title of the sheet = ticker. So that means i want a code that will create and save 500 tickers worth of data in 500 separate spreadhseets :) can anyone help or direct?


Solution

  • If you have the Datafeed Toolbox, you can use it to download historical financial data from Yahoo.

    Here is an example using only three tickers, but could be easily changed to read the values from file and applied to all 500 tickers you have:

    endDate = date;                                              %# today
    startDate = datestr(addtodate(datenum(endDate),-1,'year'));  %# last year
    tickers = {'GOOG' 'IBM' 'AAPL'};
    
    headers = {'Date' 'Open' 'High' 'Low' 'Close' 'Volume' 'Adj Close'};
    
    y = yahoo;
    for i=1:numel(tickers)
        %# fetch daily data
        data = fetch(y, tickers{i}, startDate, endDate, 'd');
    
        %# format dates, and add header row
        A = [headers; cellstr(datestr(data(:,1))) num2cell(data(:,2:end))];
    
        %# write to XLS file
        xlswrite([tickers{i} '.xls'], A);
    end
    close(y);
    

    An example of the data you get:

    >> A
    A = 
        'Date'           'Open'      'High'      'Low'       'Close'     'Volume'      'Adj Close'
        '21-Nov-2011'    [ 370.4]    [371.68]    [365.91]    [369.01]    [15999300]    [   369.01]
        '18-Nov-2011'    [378.92]    [379.99]    [374.88]    [374.94]    [13283500]    [   374.94]
        '17-Nov-2011'    [383.98]    [384.58]    [ 375.5]    [377.41]    [17139300]    [   377.41]
        '16-Nov-2011'    [389.25]    [391.14]    [384.32]    [384.77]    [12449900]    [   384.77]
        '15-Nov-2011'    [ 380.8]    [ 389.5]    [379.45]    [388.83]    [15386100]    [   388.83]
        ...