How to Download Historical Stock Data from Google Into Matlab

by Lumilog on April 15, 2009

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

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…


{ 11 comments… read them below or add one }

Rigby July 15, 2009 at 7:00 pm

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.

Lumilog July 17, 2009 at 10:37 am

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

Alec Jeffery November 5, 2010 at 5:58 pm

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.

Lumilog November 12, 2010 at 2:23 pm

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

velappan periasamy November 28, 2010 at 12:36 pm

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

Lumilog December 26, 2010 at 3:17 pm

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

Baixiao June 30, 2011 at 1:23 am

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 :D

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

Lumilog July 1, 2011 at 7:22 am

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

terry dylan July 30, 2011 at 9:52 pm

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

mike October 12, 2011 at 12:22 am

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

Lumilog October 12, 2011 at 6:58 am

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

Leave a Comment

Previous post:

Next post: