Exchange rates plots

    Will the dollar get back to 1.50 euro ? Will the british pound continue to decrease ? How linked is the yuan to the dollar ? Here, you won't get the answers, but you'll get the tools to design your own answer !

    It is shown here how you can easily get exchange rates and manipulate them into the R system.
For instance, the program hereafter produces this plot : Pdf file (update : december 16, 2008).

Back to home

R Code

### Graphique automatique de taux de change

library(fImport) #
library(facts) # there'a a nice shading function in this package : ombrage (means shade in french)
library(zoo) # -> zoo time series

#============================
# Step 1 : get Data from fred
#============================


USvsEUR0<-fredImport("DEXUSEU", file = "tempfile",
    source = "http://research.stlouisfed.org/fred2/series/",
    frequency = "daily", save = FALSE, sep = ";", try = TRUE)
USvsUK0<-fredImport("DEXUSUK", file = "tempfile",
    source = "http://research.stlouisfed.org/fred2/series/",
    frequency = "daily", save = FALSE, sep = ";", try = TRUE)
JPvsUS0<-fredImport("DEXJPUS", file = "tempfile",
    source = "http://research.stlouisfed.org/fred2/series/",
    frequency = "daily", save = FALSE, sep = ";", try = TRUE)
CHvsUS0<-fredImport("DEXCHUS", file = "tempfile",
    source = "http://research.stlouisfed.org/fred2/series/",
    frequency = "daily", save = FALSE, sep = ";", try = TRUE)
INvsUS0<-fredImport("DEXINUS", file = "tempfile",
    source = "http://research.stlouisfed.org/fred2/series/",
    frequency = "daily", save = FALSE, sep = ";", try = TRUE)
BZvsUS0<-fredImport("DEXBZUS", file = "tempfile",
    source = "http://research.stlouisfed.org/fred2/series/",
    frequency = "daily", save = FALSE, sep = ";", try = TRUE)

#==========================
# Step2 : create zoo objets
#==========================

transforme<-function(xx)
{
        aaa<-xx
        date<-as.Date(rownames(aaa@data),format="%Y-%m-%d")
        res<-zoo(as.vector(aaa@data[,1]),date)
        return(res)
}

USDvsEUR<-transforme(USvsEUR0)
USDvsUK<-transforme(USvsUK0)
JPvsUSD<-transforme(JPvsUS0)
CHvsUSD<-transforme(CHvsUS0)
INvsUSD<-transforme(INvsUS0)
BZvsUSD<-transforme(BZvsUS0)
USDvsUSD<-1

# I need all exchange rates such as : xxx=1 USD.
EURvsUSD<-1/USDvsEUR
UKvsUSD<-1/USDvsUK

FOREX<-function (cur1, cur2,curref="USD")
{
    cur1aux <- get(paste(cur1, "vs",curref, sep = ""))
    cur2aux <- get(paste(cur2, "vs",curref, sep = ""))
    return(cur1aux/cur2aux)
}


#==============
# Step 3 : plot
#==============

# Parameters : here I am interested in Euro
monnaie<-"EUR"
#
ombr<-"quarter" # or "month"

res0<-cbind(FOREX("USD",monnaie),
        FOREX("UK",monnaie),
        FOREX("JP",monnaie),
        FOREX("CH",monnaie),
        FOREX("IN",monnaie),
        FOREX("BZ",monnaie)
)
colnames(res0)<-c("US Dollar",    "GB Pound",
            "Japanese Yen",    "Chinese Yuan",
            "Indian Rupee","Bresilian Real")

# Time scale starts in 2005
res<-window(res0,start=as.Date("2005-01-01"))


pdf(file=paste("FOREX ",format(Sys.time(),"%Y-%m-%d"),".pdf",sep=""),width=21/2.54,height=29.7/2.54,paper="a4")

# Set graphical parameters
par(mar=c(2,3.5,2,1),oma=c(0,0,3,0),las=1,mgp=c(2,0.5,0),cex.main=2,cex.axis=1.5)
mat<-matrix(1:6,ncol=2,byrow=TRUE)
layout(mat,height=c(5,4,4))

temps<-time(res)
ans0<-as.integer(unique(format(temps,"%Y"))) # all years : integer vector
xrange<-as.Date(paste(c(min(ans0),max(ans0)+1),"01","01",sep="-"))
ans<-unique(as.Date(paste(ans0,"01","01",sep="-")))
# Get quarters
trim<-unique(as.Date(paste( format(temps,"%Y"),(as.integer(format(temps,"%m"))-1)%%4*3+1,"01",sep="-")))
# Get month
mois<-as.Date(paste(rep(ans0,each=12),1:12,"1",sep="-"))
# Builds time stamps to shade the ploting region
ddd<-if (ombr=="month") mois else trim

# Plots exchange rates
for (i in 1:6)
{
    plot(x=xrange,y=range(res[,i]),type="n",main=paste("x ",colnames(res)[i],"=1",monnaie,sep=""),xlab="",ylab="",xaxt="n",xaxs="i")
    axis(side=1,at=ans+365.25/2,labels=format(ans,"%Y"),tick=FALSE)
    ombrage(ddd)
    abline(v=ans)
    lines(res[,i],col=2)
}
mtext(side=3,outer=TRUE,text=paste("Exchange rate of",monnaie),cex=2)

dev.off()