How to Download Historical Stock Data from Google Into Matlab

Note: Please read the disclaimer. The author is not providing professional investing advice.

A while back I posted a Matlab script that downloads historical stock prices from Yahoo Finance using the java interface. Got a couple requests to write one to do the same for Google Finance so here it is.

get_hist_google_stock_data.m

Personally, I prefer Yahoo over Google as they have the extra Adjusted Close column which allows you to normalize for splits, dividends, etc.

But to each his own…

13 thoughts on “How to Download Historical Stock Data from Google Into Matlab”

  1. I also like yahoo historical data because of the Adjusted Close. However, if you want to download realtime quotes into matlab from yahoo, I think you need to pay a subscription fee. Have you looked into writing a script that would download real time quotes from google? I would definitely be interested, especially if the script could let download several tickers at a same time listed in a string or text file.

  2. hi rigby,

    i wrote a quick matlab script to retrieve the last trade (real-time) from Google. here’s a link to it:

    get_last_trade_google.m

    i think matlab’s line-by-line execution doesn’t let you retrieve multiple tickers simultaneously though.

    best –
    – lumi

  3. Hey How are you doing?

    I really enjoy this blog, and I must say I’ve seen you’re adept market coding improve over the last year. I’ve got a question about the get_stock_data_google function. I’ve thrown it into a timer object embedded within a pairs trading algorithim to provide entry and exit signals throughout the work day so that I don’t have to keep my finger on the market pulse whilst day-jobbing it. I’ve found that once in every couple of hundred times the code will just pause as it is waiting for data to come back from google… it is lost somewhere in the while loop at line 22, or trying to open a connection. I cannot ascertain where it is getting stuck because I cannot break the code either.

    Do you have any suggestions as to how I may embed something within the code to kick out of the function if it fails to retrieve data after a few seconds?

    Once again, cheers on a solid blog.

    A.

  4. Hi Alec – thanks for the comments. 🙂

    I am constantly battling these Matlab pauses when retrieving data from the web. In google’s case, I think if you query too often via Matlab it might redirect you to a captcha.

    I don’t have a 100% solution for these but sometimes I can add some additional code that tests to see whether what comes back from the query is a NaN or empty matrix instead of the data I’m expecting. Put this inside a while loop and keep repeating the query after a short pause until you get a valid return, then kick out of the while loop.

    As I said, this doesn’t work perfectly and but does handle a lot of cases for the various scripts I use (I actually don’t use the google one here though – was written by request).

    I’ve also found that the pauses happen more in some versions of Matlab than others. In older versions of Matlab I sometimes have to kill Matlab entirely as CTRL-C won’t break the long pause. Doesn’t seem to happen as much in newer versions, but still happens frequently. 🙁

    Hope this helps!
    Lumi

  5. Hi
    Lumilog on April 15, 2009

    I am trying to get hist quote of IBM
    by using the get_last_trade_google.m

    my fuction statement is:
    [hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = get_hist_google_stock_data(nyse:ibm)

    While running this function I am getting the following errors
    ————————————————————————–
    ??? Input argument “stock_symbol” is undefined.

    Error in ==> get_hist_google_stock_data at 18
    url_string = strcat(url_string, upper(stock_symbol), ‘&output=csv’);

    —————————————————————————-
    Kindly provide me the correct function statement to get the price of IBM stock

    With regards
    veepsirtt

  6. Velappan – it’s been a while since I wrote the script so it’s possible Google has made a small change that broke the script. However you should be using text for your stock string argument. So instead of nyse:ibm try ‘nyse:ibm’ or maybe just ‘ibm’. Either way, you need those little quote marks around the ticker you’re passing. If neither of those work let me know, because it probably means I need to update the script.

    Thanks – lumi

  7. Lumi,

    Great, great tool even though I haven’t started to use it yet because I just came cross this today. But I can feel it will help me a lot. I too have experience with Matlab, FFT, etc and have been thinking about some mechanical trading development. But never started and I think your tool is the last element I need for my financial freedom journey 😀

    One specific question: I am thinking to get historical and daily option data as well. Do you have experience with this? One tricky thing I can think of right now is that option has different symbols from stock ticker.

    Thanks a lot in advance and I will come back often.

    xiaobai

  8. Hi Xiaobai – thanks for taking the time to post a comment. I don’t think I’ve ever been able to find a free source of historical option data on the web. If you happen to come across one, let me know and we’ll see what we can do about the funny tickers.

    – lumi

  9. Hi Guys,

    i am trying to download financial data (balance sheet, income statement)from CNBC, i.e., from the following site:
    http://data.cnbc.com/quotes/AAPL/tab/7

    the site’s source code does not contain any information listed in the financial tables, I guess the table information is hidden behind some links. However, I can view the source code of the table section with IE source code viewer, right click on the table only, the text editor shows me a much shorter source code, containing the quarterly and annual financial data listed in the table.

    Do you guys know how to download the source code for the table?

    Cheers
    terry

  10. Everyone has end of day stock prices. I am looking for stock indicator/index data – past four days of values for RSI, ADX, PPO and Money Flow Index.
    Can you please help me and suggest one or more places I can access this data to be downloaded/exported to my Ecal spreadsheet.
    Thanks for your help!
    Mike

  11. Mike – since these are technical indicators computed from the stock price, I think your best best is to look up the formula for each and reproduce them yourself.

    Best,
    – Lumi

  12. Lumilog,
    Thanks for sharing this bit of code I was wondering if you might be able to lend me a hand to get it up and running. I’ve cut and pasted the the block of code to an function file in matlab which I plan on implementing in a script at a later point. However when I run it it only returns a list of dates the prices and everything else doesn’t show up in matlab. The url builder seems to function fine if I cut and past it into the address bar of my browser it triggers a download to excel. Is the function supposed to trigger a downloaded spreadsheet? If so is there settings I need to authorize on another program to allow that to happen? Well thanks for putting this out there I will continue to play around with it in hopes to get it working. Hopefully I will hear back from you but I understand if not seeing as this post is 3 years old.
    Zack

  13. hi zack – if memory serves, whenever anyone has run into this problem it’s because they’re not providing the multiple output variables on the left hand side of the = sign when calling the function. for example, they’re calling it like:

    get_hist_google_stock_data(‘AAPL’);

    instead of:

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

    could that be it?

Leave a Reply

Your email address will not be published.