# # ########### Taking a look at Gold, IBM, and the DOW Jones Industrials. # ################################################################################ # # The 'quantmod' package is very handy if you're interested in financial modeling, # or simply curious about market indices. # The persistent CRAN link for the package is: # http://cran.r-project.org/web/packages/quantmod/index.html library(quantmod) # The 'getMetals' function is limited to 500 data points per query; an easy # way to overcome that is to simply import each year separately. gold.0 <- getMetals(Metals = "XAU", from = "2012-01-02", to = "2012-02-29", base.currency = "USD", auto.assign = FALSE) gold.1 <- getMetals(Metals = "XAU", from = "2011-01-02", to = "2012-01-01", base.currency = "USD", auto.assign = FALSE) gold.2 <- getMetals(Metals = "XAU", from = "2010-01-02", to = "2011-01-01", base.currency = "USD", auto.assign = FALSE) gold.3 <- getMetals(Metals = "XAU", from = "2009-01-02", to = "2010-01-01", base.currency = "USD", auto.assign = FALSE) gold.4 <- getMetals(Metals = "XAU", from = "2008-01-02", to = "2009-01-01", base.currency = "USD", auto.assign = FALSE) gold.5 <- getMetals(Metals = "XAU", from = "2007-01-02", to = "2008-01-01", base.currency = "USD", auto.assign = FALSE) gold.6 <- getMetals(Metals = "XAU", from = "2006-01-02", to = "2007-01-01", base.currency = "USD", auto.assign = FALSE) gold.7 <- getMetals(Metals = "XAU", from = "2005-01-02", to = "2006-01-01", base.currency = "USD", auto.assign = FALSE) gold.8 <- getMetals(Metals = "XAU", from = "2004-01-02", to = "2005-01-01", base.currency = "USD", auto.assign = FALSE) gold.9 <- getMetals(Metals = "XAU", from = "2003-01-02", to = "2004-01-01", base.currency = "USD", auto.assign = FALSE) gold.10 <- getMetals(Metals = "XAU", from = "2002-01-02", to = "2003-01-01", base.currency = "USD", auto.assign = FALSE) gold <- rbind(gold.0, gold.1, gold.2, gold.3, gold.4, gold.5, gold.6, gold.7, gold.8, gold.9, gold.10) rm(gold.0, gold.1, gold.2, gold.3, gold.4, gold.5, gold.6, gold.7, gold.8, gold.9, gold.10) nrow(gold) head(gold) tail(gold) plot(gold) # Starting January 01, 2011. plot(gold[3287:3702,]) # When the markets crashed, gold skyrocketed. plot(gold[2490:3550]) # Cleaning up the workspace. graphics.off() rm(gold) detach("package:quantmod") detach("package:TTR") detach("package:Defaults") detach("package:xts") detach("package:zoo") ################################################################################ # ############## IBM stock: library(tseries) ibm.ts <- get.hist.quote(instrument="ibm", start="2002-01-02", end="2012-02-29", quote=c("O","H","L","C","A","V"), provider="yahoo", retclass="zoo") nrow(ibm.ts) head(ibm.ts) tail(ibm.ts) ibm.close <- get.hist.quote(instrument="ibm", start="2002-01-02", end="2012-03-02", quote="C", provider="yahoo", retclass="zoo") nrow(ibm.close) head(ibm.close) tail(ibm.close) plot(ibm.close) # Larger time span. ibm.close <- get.hist.quote(instrument="ibm", start="1992-01-02", end="2012-02-29", quote="C", provider="yahoo", retclass="zoo") # The crash of 2008 - 2009. plot(ibm.close[4000:4335,]) # More specifically, Summer 2008 - Spring 2009. plot(ibm.close[4150:4330,]) # Cleaning up the workspace. rm(ibm.ts, ibm.close) graphics.off() detach("package:tseries") ################################################################################ # Yahoo no longer has a license to provide DOW data, although it # can be viewed on their website. For example purposes, the # DOW data was downloaded and hosted locally. # ############### DOW Jones Industrials. # # Data was downloaded from: # http://research.stlouisfed.org/fred2/series/DJIA/downloaddata # and then reposted to: # http://www.unt.edu/rss/class/Jon/R_SC/Module10/DJIA_20130408.txt # Import the data from the web. dji.df <- read.table("http://www.unt.edu/rss/class/Jon/R_SC/Module10/DJIA_20130408.txt", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE) head(dji.df, 15) nrow(dji.df) tail(dji.df, 15) my.dates <- as.Date(dji.df[, 1]) dji.mat <- matrix(dji.df[,2], ncol = 1, dimnames = list(dji.df[,1],"Close")) head(dji.mat) ################################################################################ library(zoo) dji.zoo <- zoo(dji.df[,2], order.by = my.dates) length(dji.zoo) head(dji.zoo) tail(dji.zoo) # help(zoo) detach("package:zoo") ################################################################################ dji.ts <- ts(dji.zoo, class = "zoo") head(dji.ts) library(tseries) plot(dji.ts) # Cleaning up the workspace. graphics.off() rm(dji.df, dji.mat, dji.ts, dji.zoo, my.dates) detach("package:tseries") ################################################################################ # # Recommended books: # # Ahamed, L. (2009). Lords of finance: The bankers who broke the world. New York: # Penguin Press. # # Patterson, S. (2010). The quants: How a new breed of math whizzes conquered # Wall Street and nearly destroyed it. New York: Crown Business. # # Taleb, N., N. (2007). The black swan: The impact of the highly improbable. # New York: Random House. # # End; Feb. 29, 2012.