How to get currency exchange rates in R

You could use historical_exchange_rates() from the priceR library.

E.g. to get the daily AUD to USD exchange rate from 2010 to 2020:

# install.packages("priceR")
library(priceR)

cur <- historical_exchange_rates("AUD", to = "USD",
                          start_date = "2010-01-01", end_date = "2020-06-30")
tail(cur)

       date one_AUD_equivalent_to_x_USD
 2020-06-25                    0.688899
 2020-06-26                    0.686340
 2020-06-27                    0.686340
 2020-06-28                    0.685910
 2020-06-29                    0.687335
 2020-06-30                    0.690166

dim(cur)
[1] 3834    2


# Plot USD vs AUD last 10 years
library(ggplot2)
library(tidyverse)

cur %>% 
  tail(365 * 10) %>% 
  rename(aud_to_usd = one_AUD_equivalent_to_x_USD) %>%  
  mutate(date = as.Date(date)) %>% 
  ggplot(aes(x = date, y = aud_to_usd, group = 1)) +
  geom_line() +
  geom_smooth(method = 'loess') + 
  theme(axis.title.x=element_blank(),
        axis.ticks.x=element_blank()) + 
  scale_x_date(date_labels = "%Y", date_breaks = "1 year")

enter image description here

Some more examples can be found here, and here.


You can use quantmod to get yahoo quotes. (I'm not sure how delayed yahoo FX quotes are, or how often they're updated.)

library(quantmod)
from <- c("CAD", "JPY", "USD")
to <- c("USD", "USD", "EUR")
getQuote(paste0(from, to, "=X"))
#                  Trade Time   Last Change % Change Open High Low Volume
#CADUSD=X 2014-11-01 08:23:00 0.8875    N/A      N/A  N/A  N/A N/A    N/A
#JPYUSD=X 2014-11-01 08:23:00 0.0089    N/A      N/A  N/A  N/A N/A    N/A
#USDEUR=X 2014-11-01 08:23:00 0.7985    N/A      N/A  N/A  N/A N/A    N/A

Or TFX for real-time, millisecond timestamped quotes if you sign up for a free account. (note you have to use market convention; i.e. USD/JPY instead of JPY/USD)

library(TFX)
pairs <- paste(to, from, sep="/")
QueryTrueFX(ConnectTrueFX(pairs, "validUser", "anytext"))
#   Symbol Bid.Price Ask.Price      High       Low               TimeStamp
#1 USD/CAD   1.12651   1.12665   1.12665   1.12651 2014-10-31 20:45:00.559
#2 USD/JPY 112.34600 112.35900 112.35900 112.34600 2014-10-31 20:45:00.134
#3 EUR/USD   1.25234   1.25253   1.25253   1.25234 2014-10-31 20:45:00.598

Or if you have an Interactive Brokers account, you can use the IBrokers package, or my twsInstrument package (which is basically just wrappers for IBrokers functions)

library(twsInstrument)
getQuote(paste0(to, from), src="IB") # only works when market is open.

Looks like TFX and quantmod have functions for this (thanks to @RStudent and @KFB for the tips). I preferred quantmod since it didn't require setting up an account, but AFAICT there's no vectorized current-snapshot function like what I'm seeking. This function GetExchangeRates does this:

GetExchangeRates <- function(from, to, dt=Sys.Date()) {
  require(quantmod)
  obj.names <- getFX(paste0(from, "/", to), from=dt, to=dt)
  result <- numeric(length(obj.names))
  names(result) <- obj.names
  for (obj.name in obj.names) {
    result[obj.name] <- as.numeric(get(obj.name))[1]
    # Clean up    
    rm(obj.name)
  }
  return(result)
}

TestExchangeRates <- function() {
  from <- c("CAD", "JPY", "USD")
  to <- c("USD", "USD", "EUR")
  GetExchangeRates(from, to)
}

Tags:

Currency

R