How to Import Stock Statistics into Matlab

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

There is a matlab script that goes with this article: get_trailing_pe.m

Other posts have dealt with how to download historical stock prices into Matlab and how to adjust for splits and dividends so that you can try your luck optimizing neural networks and other homebrewed trading algorithms that are going to bring dumptrucks, DUMPTRUCKS! full of money.

But perhaps you felt the article about Matlab data mining from Yahoo! Finance was just a special case because they provide special CSV files just for this application. You want more flexibility – you’d like to be able to tell Matlab to go and get any stock statistic you’re after from a web page, not a special file provided by the host.

Well the good news is it can be done (fairly) easily in Matlab. You just have to write a script to parse the source code of the web page.

If you’re like me, you learn best from example rather than theory. So here’s a link to my script that retrieves the trailing P/E for the stock of your choice from Yahoo! Finance. Invoke this function in the following way from the command line in Matlab using the stock’s ticker symbol, for example, for Google:

>>get_trailing_pe(‘goog’)

This is the same information you’d get if you entered a stock ticker on the Yahoo! Finance website – then clicked on Key Statistics and read it visually out of the table.

Feel free to modify this scripts to get any other statistic you’re after.

There is a matlab script that goes with this article: get_trailing_pe.m

9 thoughts on “How to Import Stock Statistics into Matlab”

  1. Hey man, I have been reading all the posts in your blog.please help me, i have few queries.
    This matlab thing is interesting to me since, i am an engineer myself.
    So can you tell me how to make use of these codes with matlab?
    should i enter the same code in matlab?
    where and how should i use this code?
    your response will be greatly valuable to me, and enhance my learning.

    Thanking you in advance……………

  2. Hi, I am trying to use the code to extract “forward pe’ data. But the thing is, on the source code the forward pe value is located on the next line. please help

  3. To get Forward P/E I had to make 3 changes to the file (if you’re interested search the code for comments “Change #1”, “Change #2”, and “Change #3”).

    Here’s a link to it.
    get_fwd_pe.m

  4. @lumilog
    Hey thanks for helping out earlier. I need some help again.
    I have modified your code to get various statistical values from yahoo finance. But, the problem is when I try to write that value in excel(for further equity report update).

    for eg: to get avg_vol
    function avg_vol = get_avg_vol(stock_symbol)

    stock_symbol = upper(stock_symbol);
    % Open connection to Yahoo! Finance statistics page
    fprintf(1,’Retrieving Average Volume(3 month) for %s…’, stock_symbol);
    url_name = strcat(‘http://finance.yahoo.com/q/ks?s=’, stock_symbol);
    url = java.net.URL(url_name); % Construct a URL object
    is = openStream(url); % Open a connection to the URL
    isr = java.io.InputStreamReader(is);
    br = java.io.BufferedReader(isr);

    % Cycle through the source code until we find Average Volume…
    while 1
    line_buff = char(readLine(br));
    ptr = strfind(line_buff, ‘Average Volume’);

    % …And break when we find it
    if length(ptr) > 0,break; end

    % Handle the case where either data not available or no Average Volume
    no_data = strfind(line_buff, ‘There is no’);
    if length(no_data) > 0,
    fprintf(1,’Yahoo! Finance does not currently have Average Volume for %s.\n’,stock_symbol);
    avg_vol = NaN;
    return;
    end
    end

    % Just to make it easier, strip off all the preceeding stuff we don’t want
    line_buff = line_buff(ptr:end);

    % Extract the Average Volume(3 month)
    ptr_gt = strfind(line_buff,’>’);
    ptr_lt = strfind(line_buff,'<‘);
    avg_vol = (line_buff(ptr_gt(6)+1:ptr_lt(7)-1));

    if length(avg_vol) == 0
    fprintf(1,’N/A\n’);
    else
    fprintf(1,’ = %s\n’, avg_vol);
    end

    ——————————–
    by doing this, avg_vol is stored as a character with commas. How can I get the vol amount directly so that I can write it directly to excel in matlab.
    This holds true even for market cap which is represented in ‘M’

    thanks for helping out earlier.

  5. I am having problem when I try to get data for a group of equities (say 20 or more). I have a list with stock quotes and I run a loop but the program stops corresponding after some quotes. Any idea how to get data for more names at the same time? I dont know if it has to do with YahooFinance if the later does not allow getting data for more names.

  6. hi theo – yes, i have run into similar problems. i usually do slow down the loop a bit by using the pause function. for example, pause(5); makes the loop just wait for 5 seconds before continuing.

    that doesn’t fix it in all cases. occasionally when matlab tries to get the data, it doesn’t succeed (and i don’t know exactly why). for that reason i’ve also had to add additional logic in the past to check if it got the data it was trying to get, and if not, pause a bit and re-try the same ticker. i usually set up a max # of re-tries before just stopping with a keyboard function to tell me that it can’t get the data despite re-trying. this helps you identify cases where the ticker may have changed overnight, or you typed it in incorrectly.

Leave a Reply

Your email address will not be published.