How to Get a Free Real-time Stock Quote from Google Finance Into Matlab

A while back I posted a link to my Matlab script that retrieves a stock quote (last trade) from Yahoo! Finance. Problem is, as one reader pointed out, Yahoo’s quotes are delayed by 15 minutes, limiting their timeliness.

There is no delay in Google Finance’s quotes though so we’d probably want to retrieve data from their site instead.

Below you can find a link to a starter Matlab script that does this. I say “starter” just because I’ve only tested it with a couple tickers and haven’t added much in the way of error handling for things like typing in an incorrect ticker.

Final note, I probably wouldn’t put this function into any sort of while loop that pings google day and night. They might think you’re some sort of DoS attacker wannabe.

Just sayin’…


Amma and Ben

26 thoughts on “How to Get a Free Real-time Stock Quote from Google Finance Into Matlab”

  1. Nicely done! I was wondering, have you written a general back testing engine in Matlab? If you have, what does its structure look like? I am beginning to lay down the ground work for a back testing engine that accepts inputs like, tickers, time frame and strategy and then outputs P&L statistics. Sorry for leaving a long message in your comments section, I couldnt find a contact tab. Thanks!

  2. Hey thanks for writing. My contact info is under the “About” tab.

    I used to do a lot of backtesting sims in Matlab. My first ones were based on using common metrics to trigger buys / sells (like simple moving average, MACD, stochastic, Darvas boxes).

    My structure was similar to what you described, start time, stop time, etc. But I also had a lot of for- loops so that I could vary the algorithm parameters over a wide range to see what worked best. (e.g. 2-day moving average, 3-day, 4-day, …, 500-day)

    I’d keep track of profit and loss, including $9.99 trading commissions, and then compare the result to buy-and-hold for the same ticker. I’d also factor in taxes (my trading algorithm would usually incur short-term capital gains while buy-and-hold incurred long). Finally I’d compute mean and standard deviation of monthly returns just to get insight into whether a potentially profitable trading algorithm was taking a lot more risk.

    I’d also plot the P/L of the individual trades just to see if the gains happened to come from 1 or 2 homeruns, or just a bunch of tiny mediocre trades that added up nicely.

    To be honest I had a heck of a time finding any strategy that would always work, mostly work, or at least work more often than it didn’t work when compared to buy-and-hold. An efficient markets theory person would say “Of course!”.

    But it’s fun to keep looking so lately I’m pursuing time series forecasting methods to see if they might bear fruit. Happy hunting!


  3. Nice work, mate.
    I find yours code very helpful.

    I have a little question:
    How can I pull the Date & Time correspondence to the moment I checked the stock value ? The time must come from Google at the same we check the stock value.

    Thanks ๐Ÿ™‚

  4. Hey, nice function!
    You might also want to take a look at the matlab function that I made:

    I’m actually trying to do backtesting sims in matlab using several TA indicators in combination with genetic algorithms for optimization and forecasting.
    In case you’re interested and want to pull resources together just drop me a line.

  5. Napox – the date, time, and a whole host of other info is there for the picking. Just have to add some extra lines in the Matlab code similar to the ones that grab the last trade to extract whatever else you’re after.

    Alex – thanks for the link to your function. I was doing some GA and neural network myself a while back but had a heck of a time beating buy and hold no matter how many neurons I threw at the historical data! At best maybe I was just going after securities that were too “efficient” (a.k.a. SPY). At worst even the lightly traded small caps may be efficient!


  6. Hi SD – Periodically there are small changes to the page that breaks the script. Just fixed it so re-download and give it a try.

  7. Thanks for the scripts – they’re exactly what I was looking for. Do you know where to get historical data (for free) at relatively fine-grain (say every 10 min) for intra-day trades? There are many places that make open/close/high/low available, but I’d like more detail. Thanks!

  8. Hi! Thanks for this matlab script. It works very well…
    My request is the follow one… I try to get some values from european market. But the URL configuration is anothe one… What I doing wrong?
    I saw that the google URL have additional the market name. For example the Dax-Value…

    The old one:
    url_name = strcat(‘’, stock_symbol);


    Have you any ideas how I have to change the URL configuration???

    Thanks for your help! ๐Ÿ™‚

  9. Hello together!
    I solved my problem… It was the wrong html-script searching position and wrong convert. Anyway it is very good example for searching values on html sites!
    Now I can generate a chart with circle-buffer from all google-finance values that I need for testing of my own algorithm…
    Best regards!

  10. Never mind… I was able to modify your trailing_PE script to meet my needs. Thanks a lot!

  11. Great script. I have one suggestion that would improves the script. Currently when it parses numbers greater than 999 as a last trade it treats the commas a separate number so for 1,000 you get [1, 0] instead of the desired 1000.

    An easy fix is to replace this….
    last_trade = str2num(line_buff(ptr_gt(1)+1:ptr_lt(2)-1));
    with this…
    last_trade = str2num(strrep(line_buff(ptr_gt(2)+1:ptr_lt(2)-1), ‘,’, ”));

  12. Hi
    I tried running get_last_trade_google() below. I got these errors. I hate to bother you but maybe I am doing something stupid and it may be simple to fix if anyone knows how…….

    >> get_last_trade_google(‘AAPL’)
    Retrieving Google Finance quote (last trade) for AAPL…Error using get_last_trade_google (line 17)

    Java exception occurred: Server returned HTTP response code: 403 for URL:


    at Source)

  13. Hi Don – sorry this was 10 yrs ago so something on Google’s format must have changed in the mean time. These days I exclusively use R for such work so haven’t kept the old Matlab scripts up to date..

Leave a Reply

Your email address will not be published.