4.82353

4.8 | 18 ratings Rate this file 138 downloads (last 30 days) File Size: 3.23 KB File ID: #18458

Historical Stock Data downloader

by Josiah Renfree

 

24 Jan 2008 (Updated 29 Jan 2008)

Code covered by BSD License  

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

Download Now | 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.

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (26)
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='...

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

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

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.

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);

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 Happy User

That fixed it, thanks

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.

01 Feb 2008 Olivier B.

Works well for me too !

01 Feb 2008 Tsai wei-che

useful code

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.

06 Feb 2008 Austin Kalb

Worked great out of the box!

14 Feb 2008 Lyle Ask

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

21 Feb 2008 Brahim HAMADICHAREF

Works well. Thank you.

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?

31 Mar 2008 Kevin T

Great file. Well written and documented.

Bonus: works on R13 without single modification.

01 Apr 2008 jim cha  
06 Apr 2008 Cassio Carvalho

It works very well!

29 Apr 2008 Gee Zee

thanks for the code

24 Jul 2008 Laura Chico

delivers as promised. thank you for submitting.

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?

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?

24 Sep 2008 Al McJeff

Yes very good.

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

09 Oct 2008 sean brown

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

17 Oct 2008 Janice Hawie  
Please login to add a comment or rating.
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.

29 Jan 2008

Some users are experiencing problems with the urlread command, which I believe occurs because the program is accessing the ticker symbols as cell arrays not as strings. Changed command 'tickers(i)' to 'tickers{i}' in urlread command.

Tag Activity for this File
Tag Applied By Date/Time
finance Josiah Renfree 22 Oct 2008 09:44:37
modeling Josiah Renfree 22 Oct 2008 09:44:37
analysis Josiah Renfree 22 Oct 2008 09:44:37
historical stock data Josiah Renfree 22 Oct 2008 09:44:37
quote prices Josiah Renfree 22 Oct 2008 09:44:37
close pric Josiah Renfree 22 Oct 2008 09:44:37
high Josiah Renfree 22 Oct 2008 09:44:37
low Josiah Renfree 22 Oct 2008 09:44:37
open price Josiah Renfree 22 Oct 2008 09:44:37
finance Daniel 27 Nov 2008 09:33:31
analysis Daniel 27 Nov 2008 09:33:33
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com