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.*

as_goes_january.m

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.

examine_indices.m

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.

get_hist_stock_data.m

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.

get_hist_google_stock_data.m

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

get_index_pes.m

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

get_last_trade.m

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

get_last_trade_google.m

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

get_trailing_pe.m

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.

get_fwd_pe.m

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

simple_mvo.m

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

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.

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.

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

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

Are you aware of any reason why

get_last_trade_google.m

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

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.

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.

-lumi

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

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?

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

[A,B]=some_function(some_argument);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.

Hey,

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,

Bastien

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.

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.

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

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.

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.

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.

Thanks

Rasmus

hi rasmus,

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

http://www.mathworks.com/help/matlab/ref/datetick.html

Hey Luminous,

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

Happy Christmas

Rasmus

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

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

Nils

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!

lumi