R-script for Fixer.io – get FX rates in R for 31 currencies

Even if you are not a Forex trader, it is often necessarily to get currency exchange rates, e.g. if you trade [the options on] foreign stocks.  Fixer.io provides daily FX-rates from European Central Bank for 31 currencies via JSON API. We present a script to get data in R.


library(jsonlite)

#fetches currency rates w.r.t. EUR for a given date
getFxRatesFromFixerIo = function(refDate)
{
   avUrl = paste0("http://api.fixer.io/",refDate)
   dat = fromJSON(avUrl)
   FX_RATES = array(0.0, dim=c(1,31))
   if(!is.null( dat$rates$AUD )) FX_RATES[1] = dat$rates$AUD
   if(!is.null( dat$rates$BGN )) FX_RATES[2] = dat$rates$BGN
   if(!is.null( dat$rates$BRL )) FX_RATES[3] = dat$rates$BRL
   if(!is.null( dat$rates$CAD )) FX_RATES[4] = dat$rates$CAD
   if(!is.null( dat$rates$CHF )) FX_RATES[5] = dat$rates$CHF
   if(!is.null( dat$rates$CNY )) FX_RATES[6] = dat$rates$CNY
   if(!is.null( dat$rates$CZK )) FX_RATES[7] = dat$rates$CZK
   if(!is.null( dat$rates$DKK )) FX_RATES[8] = dat$rates$DKK
   if(!is.null( dat$rates$GBP )) FX_RATES[9] = dat$rates$GBP
   if(!is.null( dat$rates$HKD )) FX_RATES[10] = dat$rates$HKD
   if(!is.null( dat$rates$HRK )) FX_RATES[11] = dat$rates$HRK
   if(!is.null( dat$rates$HUF )) FX_RATES[12] = dat$rates$HUF
   if(!is.null( dat$rates$IDR )) FX_RATES[13] = dat$rates$IDR
   if(!is.null( dat$rates$ILS )) FX_RATES[14] = dat$rates$ILS
   if(!is.null( dat$rates$INR )) FX_RATES[15] = dat$rates$INR
   if(!is.null( dat$rates$JPY )) FX_RATES[16] = dat$rates$JPY
   if(!is.null( dat$rates$KRW )) FX_RATES[17] = dat$rates$KRW
   if(!is.null( dat$rates$MXN )) FX_RATES[18] = dat$rates$MXN
   if(!is.null( dat$rates$MYR )) FX_RATES[19] = dat$rates$MYR
   if(!is.null( dat$rates$NOK )) FX_RATES[20] = dat$rates$NOK
   if(!is.null( dat$rates$NZD )) FX_RATES[21] = dat$rates$NZD
   if(!is.null( dat$rates$PHP )) FX_RATES[22] = dat$rates$PHP
   if(!is.null( dat$rates$PLN )) FX_RATES[23] = dat$rates$PLN
   if(!is.null( dat$rates$RON )) FX_RATES[24] = dat$rates$RON
   if(!is.null( dat$rates$RUB )) FX_RATES[25] = dat$rates$RUB
   if(!is.null( dat$rates$SEK )) FX_RATES[26] = dat$rates$SEK
   if(!is.null( dat$rates$SGD )) FX_RATES[27] = dat$rates$SGD
   if(!is.null( dat$rates$THB )) FX_RATES[28] = dat$rates$THB
   if(!is.null( dat$rates$TRY )) FX_RATES[29] = dat$rates$TRY
   if(!is.null( dat$rates$USD )) FX_RATES[30] = dat$rates$USD
   if(!is.null( dat$rates$ZAR )) FX_RATES[31] = dat$rates$ZAR

   return (FX_RATES)
}

attempt = 1
refDate = as.Date('2016-01-03') #min date is 2000-01-03
endDate = Sys.Date()
res = list()
while(refDate <= endDate)
{
   possibleError <- tryCatch({ 
      res[[as.character(refDate)]] =   getFxRatesFromFixerIo(refDate) 
      refDate = refDate + 1 }, 
    error = function(err) { 
      if(attempt > 5) { 
        print(paste0("Problems with rates on ", as.character(refDate)))
        refDate = refDate + 1
        attempt = 1
    } else {
        attempt = attempt + 1 
   }
  }) 
}

nms = names(res)
N_DAYS = length(nms) 
USDts = array(0.0, dim=N_DAYS)
GBPts = array(0.0, dim=N_DAYS)
for(d in 1:N_DAYS)
{
    USDts[d] = res[[nms[d]]][30]
}
plot(USDts ~ as.Date(nms), type="l",
xlab="Date",ylab="USD/EUR")


The data are available from 2000-01-03, however, not for all currencies. That's why we have to check with is.null(). According to Fixer.io their availability is 99.98%. However, sometimes technical problems do happen. So we need to call getFxRatesFromFixerIo() inside of tryCatch(...). If an error is occurred, we re-send the JSON request upto 5 times.

Like this post and wanna learn more? Have a look at Knowledge rather than Hope: A Book for Retail Investors and Mathematical Finance Students

FinViz - an advanced stock screener (both for technical and fundamental traders)