Code covered by the BSD License  

Highlights from
Historical Stock Data downloader

4.82609

4.8 | 24 ratings Rate this file 157 Downloads (last 30 days) File Size: 3.23 KB File ID: #18458

Historical Stock Data downloader

by

 

24 Jan 2008 (Updated )

Used to retrieve historical stock data for a user-specified date range

| Watch this File

File Information
Description

This program uses the Yahoo! Finance website to download and sort historical stock data for a user-specified time period. The user can either supply the program with individual ticker symbols or with a text file containing a list of ticker symbols. The program returns a structure array that holds information on the date, opening price, closing price, high, low, volume, and adjusted closing price. The user must specify the date range for the desired information, and has the option of specifying a sample frequency of daily, weekly, or monthly. Refer to the help section in the m-file for data formats and options.

Acknowledgements

This file inspired Historical Volatility and Historical Stock Data Download (Alternate Method).

MATLAB release MATLAB 7 (R14)
Other requirements The computer must be connected to the Internet in order to download stock data. If an error occurs regarding downloading information, refer to the Matlab command 'urlread' in order to determine how to correct the issue
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (43)
27 May 2014 francesco

doesn't run varagin

20 Feb 2014 nizam

Good

22 Feb 2013 Louise  
31 Jan 2013 Zoe Zhang

love it!

29 Jan 2013 Dineshkumar Hari

I'm a hopeless noob, please help me with this I'm currently working on Brownian Modelling of Timeseries
??? Input argument "start_date" is undefined.

Error in ==> hist_stock_data at 68
bd = start_date(1:2); % beginning day

29 Jan 2013 Dineshkumar Hari

I'm a hopeless noob, please help me with this I'm currently working on Brownian Modelling of Timeseries
??? Input argument "start_date" is undefined.

Error in ==> hist_stock_data at 68
bd = start_date(1:2); % beginning day

15 Jul 2012 Alex

Hello!

I have a very simple code for downloading the historical prices from yahoo finance. If I use:

start_date = '01032012';
end_date = '11072012';
stocks = hist_stock_data(start_date, end_date, '^GSPC');
stocks.Date=datenum(stocks.Date);
stocks.Date = stocks.Date(end:-1:1); stocks.Close = stocks.Close(end:-1:1); Prices=stocks.Close

then the vector prices has 4 decimals. However, if I instead of '^GSPC' use 'EURUSD=X', then the 3rd and 4th decimals are always 0, i.e. the numbers are rounded. However, on the website http://finance.yahoo.com/q?s=EURUSD%3DX&ql=0

the quotes have 4 decimals. Is there a way to download currency data with 4 decimals instead of 2?

Thank you very much for your time and consideration!

Alex

16 Jun 2012 Zhongwei Tian  
15 Feb 2012 Martina

Hi again, I solved it :)... nice class.

M.

15 Feb 2012 Martina

Hi Joshiah,

Sorry for the silly question. The code runs fine. But somehow I can't access the data which I downloaded from yahoo... do you have an example how to access them?

Thanks.

Martina.

08 Dec 2010 Josiah Renfree

@Matt: Unfortunately, daily is the highest frequency that the program can obtain. I'm simply pulling data off of the Yahoo! Finance Historical Price webpage, in which you'll see that your only options are Daily, Weekly, or Monthly.

@David: As I mentioned to Matt, I pull data off of the Historical Prices section of the Yahoo! Finance page. Unfortunately, you'll notice that for the symbol JPYUSD=X, there is no Historical Prices section. I'm not sure why Yahoo doesn't include that, though, as their charts include historical data.

28 Nov 2010 David Wozabal

If I try to retrieve the exchange rate between JPY and USD by typing

hist_stock_data('01012005','28112010', 'JPYUSD=X', 'frequency', 'w')

I get back

ans =

0x0 struct array with no fields.

Other ticker symbols seem to work fine with me. What am I doing wrong?

02 Jun 2010 Matt Fetterman

Can it be set for higher frequency data? Maybe every hour or every fifteen minutes? Good program !

22 Apr 2010 mklcst mklcst

Very good! But I think that we need to sync the data and not to have each series in different dimension!

30 Jan 2010 Sunil Tyagi

Thanks it is useful

15 Dec 2009 Josiah Renfree

To answer Gary's question, the data to account for stock splits can be found in the returned structure array under the variable AdjClose.

08 Dec 2009 Gary

Josiah, code works well. Thanks.
Say, Is there any way to access, download or otherwise account for stock spits? Using 'LMT' as an example, dates 01/01/1979 to present, for example: Code dutifully downloads historical data but is radically different from that displayed on Yahoo. This is due, largely, to the stock splits. Any way to include these details?

17 Oct 2008 Janice Hawie  
09 Oct 2008 sean brown

I find this source http://blog.digvan.com/?p=6
it works great. try it.

28 Sep 2008 David Succaria

Hi Gents,

While I agree with you that there are some free charting as well as some historical data which can be downloaded sparingly, I would suggest that to do real trend analysis, modeling and systems development for algorithmic trading strategies, along with associative research and back testing. You need a complete local database of historical data to use with your charting and analysis package of choice. To see the dramatic difference involved between looking up symbol data online and havinga complete database try the free trial offered by these guys...
http://www.forextickdata.com

They have end of day data and intraday high frequency historical data, across multiple asset classes. Best of all they include all delisted issues for a survivorship free database.

Happy research.

David

24 Sep 2008 Al McJeff

Yes very good.

17 Sep 2008 Francisco Azeredo

Thank you for the code, it works like a charm.

One request: Can the code be modified to download the dividend series as well?

04 Sep 2008 raphael rubin

I have a problem because of the prompt which opens up when downloading the file.
How do I get over that problem?

24 Jul 2008 Laura Chico

delivers as promised. thank you for submitting.

29 Apr 2008 Gee Zee

thanks for the code

06 Apr 2008 Cassio Carvalho

It works very well!

01 Apr 2008 jim cha  
31 Mar 2008 Kevin T

Great file. Well written and documented.

Bonus: works on R13 without single modification.

26 Feb 2008 bob n

Hi! Where can i get information on the yahoo api from where you are pulling the stocks info?? i've tried to find info on yahoo's site but i cant. any suggestions?

21 Feb 2008 Brahim HAMADICHAREF

Works well. Thank you.

14 Feb 2008 Lyle Ask

Great! With a little modification of the date format, it works well with the Financial Time Series Toolbox.

06 Feb 2008 Austin Kalb

Worked great out of the box!

02 Feb 2008 Chris Troutner

Hey! Thank you so much for making such an awesome script! This was exactly what I was looking for and you saved me so much time. It worked perfectly on the first try.

01 Feb 2008 Tsai wei-che

useful code

01 Feb 2008 Olivier B.

Works well for me too !

29 Jan 2008 John D'Errico

It seems to work now. My thanks to the author for getting it to work. I can't complain at all about the help - its really quite readable. This could be a useful utility.

28 Jan 2008 Happy User

That fixed it, thanks

28 Jan 2008 Josiah Renfree

I've uploaded a new m-file that hopefully corrects for this, so it should be up in the next couple of days.

The only thing I've changed though is that in the urlread command, 'tickers(i)' is now 'tickers{i}'. I'm guessing that the problem was that varargins is a cell array, and the program was retrieving the data as a cell, and not converting it to a string. So if you don't want to wait around for the new file to upload, then just make that small little change.

If that does alleviate the problem, could you let me know? It's hard for me to tell exactly what's causing this error, as this program is working perfectly for me.

Sorry that some of you are experiencing some troubles with this. I'm hoping that other people are having more success with this.

28 Jan 2008 John D'Errico

I tried the new version. It gets further, but still dies.

stocks = hist_stock_data('01012000','27012008','TMA')

??? Undefined function or method 'eq' for input arguments of type 'cell'.

Error in ==> iofun/private/urlreadwrite at 14
protocol = urlChar(1:min(find(urlChar==':'))-1);

14 protocol = urlChar(1:min(find(urlChar==':'))-1);

27 Jan 2008 Josiah Renfree

I noticed that error a couple days ago, and uploaded a newer version that corrected this error, but apparently Mathworks hasn't posted it yet. So yes you have a valid point regarding the tickers with fewer than 4 characters, but the new version already corrects for this. Hopefully Mathworks will post it tomorrow.

As for the date inputs, the m-file documents on the format it needs to be. I know I could have put in better error-checking and what not, but if you follow the directions of the m-file then it should be straightforward. It states the first date is the beginning time, and the second is the ending time. Also, they are input as strings, with the format ddmmyyyy. I believe it should all be there in the help.

Thanks for the comments and let me know if you find anything buggy with the new version.

27 Jan 2008 John D'Errico

Buggy code. BEWARE!

I'm running R2007a, and I too get errors for all test cases I've tried, including those given as examples by the author. I also tried changing the urlread call as suggested. So then I looked at the errors returned. Things are done like this:

77 if strcmp(varargin{1}(end-3:end),'.txt') % If text file supplied

If the user supplies stock ticker symbols for stocks that are shorter then 4 characters, then what do you think will happen? My guess is its an error.

This code is well documented. The help is good. But the code is sloppy, in the sense that it is not carefully written. There are no error checks. Are the dates provided valid dates? Are they even the correct length as dates? Are the two dates increasing in order? Are they even strings? It merely indexes into them as vectors.

More importantly, the other arguments are indexed as strings also. The code looks at the last 4 characters of a string to see if it is '.txt'. But if the string is a ticker symbol that is only 3 characters long, oops!

I really wanted to give this a lower rating. "needs improvement" seemed about right at first given the outright bugs. On the other hand, the code is well documented, it has an H1 line, it has lots of comments internally, and the author seems willing to repair the problems.

My advice to the author is to please test your code better. Also put in error checks, to make sure that you get the arguments that you expect. If not, then have it return a friendly error message that tells the user what they did wrong.

I'll happily return to this code in a few days to look for improvements.

27 Jan 2008 Josiah Renfree

It's not giving me that error on the Matlab version I'm running, so it's hard for me to pinpoint what the problem is. But, I believe that it may be that it's inputting the ticker symbol into the urlread command as a cell array.

In the urlread command, you'll find the command 'tickers(i)'. Try changing that to 'tickers{i}' instead, so that it's not returning it as a cell array but as a string.

Let me know if that solves your problem, so that I can make the necessary corrections for future versions of the program

26 Jan 2008 Olivier B.

got an error at urlread level with the provided Goog example ... any hint ?

??? Undefined function or method 'eq' for input arguments of type 'cell'.

Error in ==> urlreadwrite at 14
protocol = urlChar(1:min(find(urlChar==':'))-1);

Error in ==> urlread at 68
[urlConnection,errorid,errormsg] = urlreadwrite(mfilename,urlChar);

Error in ==> hist_stock_data at 95
[temp, status] = urlread(strcat('http://ichart.finance.yahoo.com/table.csv?s='...

Updates
25 Jan 2008

Updated file to handle instances when stock data is not available. If the ticker symbol is not valid or simply does not return data, then that stock will be ignored.

Also, added examples in the help section.

28 Jan 2008

The following items have been changed:

- There was an error when the first ticker symbol input to the function was less than 3 characters.

- The user may now choose the sample frequency of daily, weekly, or monthly.

Contact us