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:


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