How to Download Historical Stock Data into Matlab

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:

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!


Just Florida. I’m still tweaking my algorithms…

118 thoughts on “How to Download Historical Stock Data into Matlab”

  1. Nevermind, i ran the function as a script from the editor 🙂

    Thanks for a great scipt!


    Alex, Sweden

  2. This is such a great great code and article. definitely a lot better than Matlab datafeed tool box.. Thank you so much for making my life a lot easier.. ..

  3. HI when i copy and paste the above data into mathlab i get the following error:
    ‘the selected selection cannot be evaluated because it contains an invalid statement’,

    I have briefly studied mathlab before but not to a major extent. I now need it to do a project on historical prices from finance. Can you please help me with what i’m doing wrong?

  4. Lumilog I did exactly that and this is the error I get:

    ‘function [hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = get_hist_stock_data(stock_symbol)
    Error: Function definitions are not permitted in this context. ‘

    Can you see what I am doing wrong?

  5. 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’);

  6. 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:

    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,

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

  8. 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’);

  9. Hi, im trying to run this script an MATLAB R2008a.
    I’m not a MATLAB newbie (but not uset to do java and www stuff).
    Did something change about the csv tables, since 2015?

    Error in ==> get_hist_stock_data at 8
    start_year = ‘2004’;

    ??? Output argument “hist_date” (and maybe others) not assigned during call to

    Cheers, Dieter

  10. hi dieter,

    i don’t use Matlab anymore (switched to R) but this sounds like 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 Reply

Your email address will not be published.