Update! I finally passed all the CFA exams and wrote an eBook about the program. If you're interested, click here.

Matlab Stock Market Scripts

Below are links to various stock market related Matlab scripts.

Please read the disclaimer. The author is not providing professional investing advice. You are responsible for any investment decisions you make using the scripts and I do not guarantee that they are error-free.

A script used to gain further insight into the reliability of the so-called January Rule (as goes January, so goes the year). See the accompanying article about this here.

A starter Matlab script for building your own market barometer. Retrieves recent price and volume action of the Dow, NASDAQ, and S&P 500 to help you identify high volume buying, selling, or stalling to help predict where the market is headed. See the accompanying article about this here.

A Matlab function that retrieves historical stock data (high, low, open, close, volume) from Yahoo! Finance and also normalizes the raw prices for splits & dividends. See the accompanying article about this here.

A Matlab function that retrieves historical stock data (high, low, open, close, volume) from Google Finance. See the accompanying article about this here.

A Matlab function that estimates the current P/E’s of major US stock market indices. See the accompanying article about this here.

A Matlab function that retrieves the “Last Trade” price per share from Yahoo! Finance. See the accompanying article about this here.

Same as above except this script retrieves from Google Finance, which has real-time quotes. Accompanying article here.

The main purpose of this script is just to demonstrate how to use Matlab’s java interface to retrieve any bit of data from a web page. In this case we go and read a P/E statistic from the Yahoo! Finance site. See the accompanying article about this here.

A modified version of the above get_trailing_pe.m to retrieve the forward P/E instead.

A Matlab starter script for dabbling with Modern Portfolio Theory and mean-variance optimization. See the accompanying article about this here.

{ 22 comments… read them below or add one }

sjev September 26, 2010 at 4:00 pm

Thanks for the yahoo downloader code! After using it for a year I’ve cleaned it up and added exact period for data download. It’s posted on my blog.

Lumilog October 3, 2010 at 7:15 am

Thanks for writing Sjev, and glad it’s been useful. I added a link to your blog on the site. Looks like some interesting topics you’re exploring there.

sudhanshu November 30, 2010 at 2:20 am

can i get some matlab quoets and script for comparison between various diversity techniques..especially for space and frequency

Lumilog December 26, 2010 at 3:18 pm

Hi Sudhanshu – at the moment I’m not doing any research in that area, but if I do I might put up some scripts that are useful. Maybe try the Matlab File Exchange in the mean time.

– Lumi

alex January 21, 2011 at 11:47 pm

Are you aware of any reason why


is not working for the last three days? Thank you.

rep January 24, 2011 at 4:07 pm

alex: I noticed the same thing. Very frustrating. Just figured it out though…

Google must’ve recently (slightly) modified the format of their page.

Open the original get_last_trade_google.m script and edit the for loop on line 31.
It says “for i = 1:8″ .
Just change it to “for i = 1:10″ and it should work again.

Hope that helps. This is a very cool script. Thanks to the creator for posting it.

Lumilog February 13, 2011 at 5:00 pm

Alex – thanks for pointing out the problem.

Rep – thanks for fixing it for us.

I updated the file so that it should work going forward.


Russell December 26, 2011 at 1:32 am

Hi LumiLog,
Thanks for the excellent reference scripts to help me begin using Matlab to retrieve financial data online. My question is simple: your examples show how to easily retrieve historical stock price and volume data. But, from Yahoo! finance only CURRENT stock metrics, like P/E and Revenue can be retrieved. Is there any quick and easy source of HISTORICAL stock metrics, like P/E and Revenue. My idea to this point is to use similar commands to scan the EDGAR database at sec.gov. However, to open and parse financial statements and accurately caluclate stock metrics of interest seems like a very tedious and error-prone process.
The end-game is to backtest trading algorithms based on stock metrics (like the ‘Magic Formula’) before I apply these formulas to current metrics and put my own skin in the game.
Any ideas/suggestions are greatly appreciated. And thanks for posting this exellent website/blog/reference source. Best Regards, ~Russell

rp March 6, 2012 at 1:46 am

hi there, I am having some trouble using your historical stock data scripts. they are only returning a cell with the dates. Do you know why this is?

Lumilog March 6, 2012 at 5:36 am

Hi RP – I don’t know what script you’re using, but it sounds like you’re not calling the function with the correct # of return arguments on the left hand side of the equals sign. For example, if a function returns 2 arrays you need to call it with


It sounds like you may have had a single argument like A=some_function(some_argument) when it should have been [A, B, C, D] = some_function(some_argument).

That being said, the websites do sometimes make small changes that break the scripts and require me to update them. Give me a heads up if you find some that appear broken.

Bastien September 30, 2012 at 10:07 am


nice blog… Do you have, by any chance, a Matlab script for getting the income statement of publicly traded companies? These are available from the Nasdaq website for instance (see for example http://www.nasdaq.com/symbol/aapl/financials?query=cash-flow&data=quarterly), however the html parsing is probably a little tricky… I would love to use whatever exists as opposed to writing smtg from scratch!

Take care,


TA Developer February 10, 2013 at 12:23 am

Hi Lumi, I like your collection of MATLAB scripts. I have created a toolbox for backtesting trading strategies in MATLAB which might be interesting for you. The toolbox is currently in the beta testing phase so it is free to try.
Please have a look at http://www.tadeveloper.com/docs/80-matlab-algo-trading and let me know what you think.

Rodney February 15, 2013 at 12:53 pm

Like your scripts for downloading historical stock data. How about options prices? Neither Yahoo or Google provide links for downloading the options tables to a spreadsheet, so a different approach seems to be required.

Lumilog February 15, 2013 at 12:56 pm

Thanks Rodney. Unfortunately I’ve never found a source for historical options pricing. Paid sources probably exist though. Good luck!

Andy June 21, 2013 at 4:48 pm

while running your file to get indices data matlab is giving following errors

Error in ==> C:\MATLAB6p5\work\usindices.m
On line 29 ==> [hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] = get_hist_stock_data(indices_list{index});

Please look n solve asap.

Lumilog June 22, 2013 at 6:56 am

there is no usindices.m file so i assume that’s one you created by modifying examine_indices.m? if so, no problems on my end – make sure you also have get_hist_stock_data.m in the same directory.

Rasmus December 20, 2013 at 3:09 pm

Hey Luminous,

is there a way to plot only business days for financial data in Matlab? I’m using some of your scripts for downloading Yahoo stock data and want to plot the date of the day instead of the tick number.


Lumilog December 20, 2013 at 3:19 pm

hi rasmus,

if i understand you correctly, i think this is what you’re after:


Rasmus December 20, 2013 at 5:48 pm

Hey Luminous,

Thank you so much.. What would I only do without your blog??

Happy Christmas

Jasper September 21, 2014 at 8:41 pm

Thank you for your insightful website and the Matlab downloads. Not many make some of the subjects you address as clear as you. Not sure if you keep this web up anymore, but it is very interesting

OptiValue January 30, 2015 at 9:38 am

Hey Lumilog,

I’m currently trying to backtest the magic formular on the german stock market, do you have some insight how to code it in Matlab?

Kind regards


Lumilog February 4, 2015 at 1:40 pm

hi nils,

i can’t think of where you’d get the historical fundamental data from unfortunately. perhaps quantopian.com has historical info on german stocks?

good luck!

Leave a Comment

{ 2 trackbacks }