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.

28 thoughts on “Matlab Stock Market Scripts”

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

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

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

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

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


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

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

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

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

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

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

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


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

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


  15. hi nils,

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

    good luck!

  16. Lumi,

    Hopefully this finds you well, I’ve been playing with your historical data function a bit over the last few months. I’m currently calling the function in a script looking up multiple symbols at a time in hopes to get on with further calculations later. I’m using matlab to pull the symbols for the sp500 and attempting to use your function to pull the info and package it into a single array however certain symbols cause it to hang up (COV is a example that comes to mind). I believe this happens for various reasons but I was wondering if you might have an idea about how to have the function skip the symbol that causes the error instead of crashing? I’m hoping that once I work out these bugs I’ll be able to use some parallel computing techniques to crunch large volumes of data and hopefully obtain some useful information. Any help would be greatly appreciated and I would like to thank you for publishing this site it is full of great information.

  17. hi zack – apologies but i know of no easy way to do this. what you might try is to put a dummy ticker into the script and see what yahoo finance gives you back. if it gives you back nothing can you test that the length of the return variable == 0?

    i would play around with it in Matlab myself to find the answer for you but i defected to R a couple years ago and don’t have Matlab on my current machine.

    one other thing i used to use is a while loop to proceed through the tickers so if something caused a failure i could then restart the script (without clearing the workspace) and it would continue at the pointer where it left off

    hope that helps!

  18. Thanks for the reply I will attempt to see what I can figure out the while loop within the script sounds promising and probably the easiest course of action.

Leave a Reply

Your email address will not be published.