This is a very important and usual task. Historical data is mainly required for risk management purposes, model validation and back-testing. For these tasks software systems like Theta Suite, Matlab, Java or Excel are the main work space. We need to import the data here.
Other Posts in this Series
Starting with MS Excel and usual market data
Free historical data is available at several resources, depending on your market preferences, they might be more or less relevant:
- Yahoo! Finance: Has an excellent coverage of many markets and allows the direct download of user defined Excel sheets. E.g. for IBM http://finance.yahoo.com/q/hp?s=IBM+Historical+Prices
- Google Finance: Again, an excellent coverage and newly added direct download of Excel sheets, too. E.g. for IBM http://www.google.com/finance/historical?q=NYSE:IBM
A comprehensive list of free data sources
Besides the above usual data sources, there are numerous other sources. A pretty cool list is available here:
http://www.quantnet.com/forum/threads/master-list-of-free-financial-data.1036/
and another list is in my other post: List of Free Historical Market Data Sources.
Read Customized data from Matlab
If market data is available on the web, and you cannot download it easily, there are convenient ways for importing the data:
- A great option for reading data from the web is getTableFromWeb_mod.m which allows you to choose a data table of an HTML and import it to Matlab. To get table data, it is necessary to know from which url you want to read in the data and from which table. If you have an url but no idea which table with the specified table number has the data – besides trial and error – you can use the getTableFromWeb and just click on the table you need.
- If your data is not in a table, it is more challenging. A good help for your required custom code could be JSoup: A simple Java HTML reader. With JSoup, you can do things like
javaaddpath('jsoup-1.6.1.jar') url_string = 'http://finance.yahoo.com'; doc = org.jsoup.Jsoup.connect(url_string); doc.get(); parser = org.jsoup.Jsoup.parse(char(doc.get())); doc = parser.getElementsByTag('td'); text = {}; for i=1:doc.size text{i,1} = char(doc.get(i-1).text()); end
Now, text{} contains a Matlab cell with e.g. USD/EUR.
Push historical data from Yahoo! into Matlab
If you just want go grab some historical data from yahoo into Matlab you also might just try: http://luminouslogic.com/matlab-stock-market-scripts and get “get_hist_stock_data.m”. This works great for most cases and you have no hassle programming.
I hope, this helps. If you know of other free and convenient methods, feel free and drop me a line in the comment!
Donglin Wang
Andreas. Long time no see. I really like this blog!