Download Financial Data From Yahoo! Finance With Matlab

stock-pricesIn this post I will show an alternative method for downloading financial data directly to MATLAB without using the Datafeed Toolbox.

Datafeed Toolbox provides access to current, intraday, historical, and real-time market data from leading financial data providers. According to MATLAB website:

With a single function call, the toolbox lets you customize queries to access all or selected fields from multiple securities over a specified time period.

Even if this sound pretty easy, in reality it is not! Trying to download different datasets from Yahoo! Finance and Bloomberg I discovered that the process is quite cumbersome.

In this example I will use the Historical Stock Data Downloader that can be found in Matlab File Exchange. Also, we will need a list of the symbols that are used in Yahoo! Finance. A very good source of these symbols can be found in this excel file (scroll to the end of the new opening article to find the download link).

The first step is to decided which data do we need. Suppose that we need the closing prices of the Barclays stock. By looking at the excel file we find that the ticker symbol is ‘BARC.L’. Now, the next step is to download the data. Suppose that we need the data from 01/01/2013 till 31/12/2014. Now we can call the historical stock data downloader like this:

stocks = hist_stock_data('01012013', '31122014', 'BARC.L');

Alternatively, we can download data from multiple stocks at the same time. We can do that by creating a ID cell that stores all the ticker symbols:

stocks = hist_stock_data('01012013', '31122014', ID{:});

Finally, we can store the ticker symbols in a txt file and then use the above function to read the symbols from the txt file. Assuming that the txt file is called ‘tickers.txt’ then we can download the data as follows:

myfile = 'tickers.txt';
stocks = hist_stock_data('01012013', '31122014', myfile);

The function hist_stock_data returns a structure (e.g. ‘stocks’ in the previous example) that contains the following information:  the Date, Open, High, Low, Close, Volume, and Adjusted Close price adjusted for dividends splits as well as the Ticker.

6 thoughts on “Download Financial Data From Yahoo! Finance With Matlab

  1. Frankie Politi

    This has actually been really one of the best articles i have checked out. It was actually really informative.Looking forward for a lot more blogs of this particular in near future

    Reply
  2. Denis Alaev

    It seems that after several changes Yahoo Finance closed their API forever. The API was closed on May 15, one month ago already.

    I’ve tested several alternatives and found that https://eodhistoricaldata.com the best one for those who used Yahoo Finance. They provide raw data, adjusted closes and splits/dividends.

    They also have CSV output, with very similar format for Yahoo Finance users.

    Also there is a https://intrinio.com/ data provider, looks good, but they much more expensive, have no data for Mutual Funds and API is very different in compare to Yahoo Finance. Then you need to significantly change your code.

    Reply
  3. ousmane faye

    hello Professor,
    thank you for sharing this interesting tip for download stock data. My question is how to do if I just want to deal with the “date and Close” columns of the data dowloaded. Thank you for any help.

    Reply
  4. John

    Thats a great example. Combine Yahoo with a good intradata source like http://intradata.co for timeframes of 1 minute interval as Yahoo doesn’t have the most clean data and is not very realiable. A combination of both I am sure is a good way to go.

    Reply

Leave a Reply to ousmane faye Cancel reply

Your email address will not be published.