Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created August 17, 2012 21:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/3382974 to your computer and use it in GitHub Desktop.
Save timelyportfolio/3382974 to your computer and use it in GitHub Desktop.
plot.xts and rugarch
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org")
require(quantmod)
require(rugarch)
require(xtsExtra) #if you get an error, see first line and install from r-forge
getSymbols("DEXJPUS",src="FRED")
DEXJPUS<-1/to.weekly(DEXJPUS)
ugarch.panel <- function(index,x,type,cex,col,pch,...){
spec = ugarchspec(
variance.model=list(garchOrder=c(1,1)),
mean.model=list(armaOrder=c(1,1), include.mean=T))
#get ugarchroll; I cannot say I completely understand what I'm doing here
ugr <- ugarchroll(spec,
data = na.omit(x),
forecast.length = floor(NROW(na.omit(x)) / 1000) * 1000,
n.ahead = 1,
refit.every = 50,
refit.window = "moving")
#get garch coefficients
ugr.var <- merge(x,as.data.frame(ugr,which="VaR"))
ugr.var <- as.xts(apply(ugr.var,MARGIN=2,na.fill,fill=c(0,"extend")),order.by=index(x))[,2:4]
print(tail(ugr.var))
default.panel(index,ugr.var[,3],type="h",cex,pch,col,...)
default.panel(index,ugr.var[,1],type="l",cex,pch,col="red",...)
default.panel(index,ugr.var[,2],type="l",cex,pch,col="gray50",...)
text(x=index[1],y=par("usr")[4],"VaR from rugarch ugarchroll",adj=c(0,1),cex=0.8)
}
plot.xts(na.omit(merge(DEXJPUS[,4],ROC(DEXJPUS[,4],n=1,type="discrete"))),
screens=c(1,2),
minor.ticks=FALSE, major.format="%Y",
panel=c(default.panel,ugarch.panel),
main="Japanese Yen (source: St. Louis Federal Reserve)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment