Update! I finally passed all the CFA exams and wrote an eBook about the program. If you're interested, click here.

How to Download Historical Stock Data into Matlab

by Lumilog on November 26, 2006

Greetings! You’ve arrived here most likely because you’ve just realized that you’ve been Wasting Your Talents at your job using Matlab to research new medicines, design fuel-efficient engines, or develop next-generation wireless communication technologies when – duh! – you could put those Matlab skills to much better, MUCH better use turning the stock market into your own ATM. From your boat!

Well here it is – the gateway drug – a few simple lines of code that will let you download Yahoo’s free historical stock price data right into Matlab. Then you’re on your own as to what to do with it, but I’ll bet you’ve learned quite a bit of signal processing mojo over the years that you’re salivating to put to good use: particle filters, neural networks, wavelet transforms, …maybe even Viterbi decoders!

Assuming you already speak Matlab well, there are really just a handful of new things to learn.

First, it might help to know exactly where to download from. Yahoo Finance provides a convenient historical data CSV file for each stock currently trading. Type in any ticker, click on Historical Prices, and scroll down to the bottom…

Tricky Move #1 is to crack the URL genome so that you can generate your own URL on the fly. The URLs looks like this:

http://ichart.finance.yahoo.com/table.csv?s=AAPL&a=10&b=15&c=2005&d=01&e=17&f=2006&g=d&ignore=.csv

With a little (VERY little) trial & error, you might come to discover that after s= goes the ticker symbol, after a= the start month (minus 1), after b= the start day, c= the start year and so on. The final g= parameter lets you choose between getting historical stock information on a daily, weekly, or monthly basis. Construct your desired URL as a Matlab character string (let’s call it url_string).

Tricky Move #2 is to tunnel your way directly to a webpage from within Matlab, no browser required! This is done via Matlab’s java interface and your URL string like so:

Tricky Move #3 is to use that connection to read in the individual lines of the webpage’s source code into a buffer.

It’s really no harder than that – just put that last tidbit in a while loop and parse the buffer each time around to grab the data and store it in a matrix. It’s especially easy here since we’re working with CSV. Parsing HTML from other websites is a little trickier, but I’ll bet you’ll graduate quickly (gateway drug, I tell you).

Still sound too hard? OK, just click below to download my source code for free. See you in St. Maarten!

get_hist_stock_data.m

Just Florida. I’m still tweaking my algorithms…










{ 107 comments… read them below or add one }

Lumilog October 8, 2013 at 5:00 am

if you want the history for, say Apple, call it like so:

[hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = get_hist_stock_data(‘AAPL’);

andrew November 29, 2013 at 3:26 pm

when running it says that the first function is not permitted?
How do i fix this?

Lumilog December 1, 2013 at 6:18 pm

hmmm – could you cut and paste what you’re typing into the command line?

Shawn Franklin December 16, 2013 at 11:00 pm

I have noticed that when it comes to calling dividends from the Yahoo site, that the Yahoo server must drop low-traffic requests at time. Example: If I try to access the CSV for dividends for “EMM” using the following url:

http://ichart.finance.yahoo.com/table.csv?s=EMM&a=00&b=1&c=1900&d=11&e=17&f=2013&g=v&ignore=.csv

depending on the time accessed, 80% of time it will return the actual CSV, but 20% of the time, it will return a “404 Not Found” error.

Would be nice to add robustness to your code by re-attempting to download the CSV again if not successful the first time.

Thanks and keep up the great work, you are helping me in my understanding and taking on new challenges,
Shawn

Lumilog December 17, 2013 at 8:59 pm

hi shawn – thanks for stopping by and yes i’ve noticed the same behavior. when i’m doing a long vector of ticker symbols i put it all in a while loop and only proceed to the next ticker if i get valid information. otherwise i do a pause(10); and call the function again.

Dzenan August 28, 2014 at 6:26 pm

Hi,

when start it returns only dates. I have no idea why.

Kind regards.
Dzenan

Lumilog August 28, 2014 at 7:52 pm

you’re probably not providing the output matrices. call it like so:

[hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = get_hist_stock_data(‘AAPL’);

Leave a Comment

{ 6 trackbacks }

Next post: