Financial Market Data for R/Rmetrics
Total Page:16
File Type:pdf, Size:1020Kb
Financial Market Data for R/Rmetrics Diethelm Würtz Andrew Ellis Yohan Chalabi Rmetrics Association & Finance Online R/Rmetrics eBook Series R/Rmetrics eBooks is a series of electronic books and user guides aimed at students and practitioner who use R/Rmetrics to analyze financial markets. A Discussion of Time Series Objects for R in Finance (2009) Diethelm Würtz, Yohan Chalabi, Andrew Ellis R/Rmetrics Meielisalp 2009 Proceedings of the Meielisalp Workshop 2011 Editor Diethelm Würtz Basic R for Finance (2010), Diethelm Würtz, Yohan Chalabi, Longhow Lam, Andrew Ellis Chronological Objects with Rmetrics (2010), Diethelm Würtz, Yohan Chalabi, Andrew Ellis Portfolio Optimization with R/Rmetrics (2010), Diethelm Würtz, William Chen, Yohan Chalabi, Andrew Ellis Financial Market Data for R/Rmetrics (2010) Diethelm W?rtz, Andrew Ellis, Yohan Chalabi Indian Financial Market Data for R/Rmetrics (2010) Diethelm Würtz, Mahendra Mehta, Andrew Ellis, Yohan Chalabi Asian Option Pricing with R/Rmetrics (2010) Diethelm Würtz R/Rmetrics Singapore 2010 Proceedings of the Singapore Workshop 2010 Editors Diethelm Würtz, Mahendra Mehta, David Scott, Juri Hinz R/Rmetrics Meielisalp 2011 Proceedings of the Meielisalp Summer School and Workshop 2011 Editor Diethelm Würtz III tinn-R Editor (2010) José Cláudio Faria, Philippe Grosjean, Enio Galinkin Jelihovschi and Ri- cardo Pietrobon R/Rmetrics Meielisalp 2011 Proceedings of the Meielisalp Summer Scholl and Workshop 2011 Editor Diethelm Würtz R/Rmetrics Meielisalp 2012 Proceedings of the Meielisalp Summer Scholl and Workshop 2012 Editor Diethelm Würtz Topics in Empirical Finance with R and Rmetrics (2013), Patrick Hénaff FINANCIAL MARKET DATA FOR R/RMETRICS DIETHELM WÜRTZ ANDREW ELLIS YOHAN CHALABI RMETRICS ASSOCIATION &FINANCE ONLINE Series Editors: Prof. Dr. Diethelm Würtz Dr. Martin Hanf Institute of Theoretical Physics and Finance Online GmbH Curriculum for Computational Science Zeltweg 7 Swiss Federal Institute of Technology 8032 Zurich Hönggerberg, HIT G 32.3 8093 Zurich Publisher: Contact Address: Finance Online GmbH Rmetrics Association Swiss Information Technologies Zeltweg 7 Zeltweg 7 8032 Zurich 8032 Zurich [email protected] Authors: Yohan Chalabi, ETH Zurich Andrew Ellis, Finance Online GmbH Zurich Diethelm Würtz, ETH Zurich ISBN:978-3-906041-04-9 © 2009, Finance Online GmbH, Zurich Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the con- ditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another lan- guage, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Rmetrics Association, Zurich. Limit of Liability/Disclaimer of Warranty: While the publisher and authors have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor authors shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. Trademark notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation, without intent to infringe. February 2010: 1st edition DEDICATION This book is dedicated to all students and academic researchers who work in empirical finance and cannot afford to buy data from commercial providers. PREFACE Are you working with R and Rmetrics in the field of teaching finance? Then you will often use financial market data from the Internet, most likely downloaded from an Internet portal like Yahoo Finance or the Fed- eral Reserve. You may have asked yourself whether there are also other data sources on the Internet that can be used for courses and lectures and how can the download functions be customized to your personal needs. This Rmetrics eBook tries to answer questions you might have about these issues. We will show you how to access and download financial market data from the Internet. We will also show you how to write convenient download and listing functions to make your life more comfortable. In this eBook you will find solutions for how to compose download URLs, how to download financial time series and how to generate listings for the financial instruments to make the search for individual time series easier. In all cases examples and exercises are given. This eBook is a Sweave document and we will keep it up-to-date with changes on the servers of the data Internet portals and with the most recent R and Rmetrics packages. This eBook is copyrighted by the Rmetrics Association and Finance Online in Zurich. It can be ordered from the Rmetrics website www.rmetrics.org. Enjoy it! Diethelm Würtz Zurich, May 2010 vii CONTENTS DEDICATION V PREFACE VII CONTENTS IX LIST OF FIGURES XIII LIST OF TABLES XIV I Managing Data from the Internet 1 1TIME SERIES FUNCTIONS 2 1.1 timeDate and timeSeries Objects .................... 2 1.2 Downloading timeSeries Data Sets .................. 3 1.3 Financial Time Series ............................ 5 1.4 Sorting and Reverting timeSeries Objects .............. 6 1.5 Alignment of Time Series Objects .................... 8 1.6 Binding and Merging timeSeries Objects .............. 9 1.7 Subsetting timeSeries Objects ...................... 12 1.8 Aggregating timeSeries Objects ..................... 15 2READING DATA FROM THE INTERNET 18 2.1 The Function scan() ............................ 18 2.2 The Function readLines() ........................ 19 2.3 The Function read.table() ....................... 20 2.4 The Function read.xls() ......................... 21 2.5 The Function read.lynx() ........................ 22 2.6 Cleaning a Downloaded File ....................... 22 3BASIC STATISTICS OF TIME SERIES 24 3.1 Sample Mean and Covariance Estimates . 27 3.2 Estimates for Higher Moments ..................... 28 ix CONTENTS X 3.3 Portfolio Risk Measures .......................... 29 3.4 Extreme Values and Outliers ....................... 30 3.5 Column Statistics ............................... 30 3.6 Cumulated Column Statistics ...................... 31 4PLOTTING FINANCIAL TIME SERIES 32 4.1 The Generic Plot Function plot() ................... 32 4.2 Rmetrics’ Tailored Plot Functions ................... 33 4.3 Box Plots ..................................... 38 4.4 Histogram and Density Plots ....................... 40 4.5 Quantile-Quantile Plots .......................... 42 4.6 Customization of Plots ........................... 44 4.7 Plot Labels .................................... 44 4.8 More About Plot Function Arguments . 45 4.9 Selecting Colours ............................... 48 4.10 Selecting Character Fonts ......................... 48 4.11 Selecting Plot Symbols ........................... 50 II Equity Markets 52 5YAHOO FINANCE PORTAL 53 5.1 The Download URL ............................. 53 5.2 Downloading a Time Series ........................ 54 5.3 The Function yahooDownload() .................... 56 5.4 Time Series Listings ............................. 57 5.5 US Equities ................................... 58 5.6 Non-US Equities ............................... 60 5.7 US Equity Indices ............................... 65 5.8 World Equity Indices ............................ 69 5.9 Index Components .............................. 76 5.10 WORLD Index Components ....................... 82 5.11 Function Summary ............................. 87 6ONVISTA FINANCE PORTAL 88 6.1 The Download URL ............................. 89 6.2 Downloading a Time Series ........................ 90 6.3 The Function onvistaDownload() . 92 6.4 Time Series Listings ............................. 94 6.5 Regional Indices ................................ 94 6.6 MSCI Indices .................................. 98 6.7 Stock Exchange Listings .......................... 99 6.8 Equity Index Components . 100 6.9 Function Summary .............................103 CONTENTS XI III Interest Rate and Bond Markets 105 7BOARD OF GOVERNORS H15 REPORT 106 7.1 The Download URL .............................107 7.2 Downloading a Time Series . 107 7.3 The Function h15Download() . 108 7.4 Time Series Listings .............................110 7.5 Interest Rate Swaps .............................111 7.6 Function Summary .............................112 8 US FEDERAL RESERVE BANK 114 8.1 The Download URL .............................115 8.2 Downloading a Time Series . 115 8.3 The Function fredDownload() . 116 8.4 Time Series Listings .............................117 8.5 Time Series Categories . 117 8.6 Time Series Selection ............................122 8.7 Sub Category Listings ............................123 8.8 Function Summary .............................127 9BUNDESBANK TIME SERIES DATABASE 128 9.1 The Download URL .............................129 9.2 Downloading