Close Navigation
Learn more about IBKR accounts
R Code Snippet: Read Historical Daily Exchange Rates

R Code Snippet: Read Historical Daily Exchange Rates

Posted September 22, 2022
Sang-Heon Lee
SHLee AI Financial Model

This post shows how to read daily exchange rates given symbols as a string.

Read historical exchange rates

Source: I collected the symbols of exchange rates at https://finance.yahoo.com/currencies.

R code

The following R code retrieves historical daily exchange rates given their symbols as of 2022-08-14.

#========================================================#
# Quantitative ALM, Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
#
# https://kiandlee.blogspot.com
#--------------------------------------------------------#
# read historical exchange rates
#========================================================#
 
graphics.off(); rm(list = ls())
 
library(quantmod)
library(stringr) # trim
 
#-------------------------------------------------
# Symbols of exchange rates, as of 2022-08-14
#-------------------------------------------------
vstr_symbol <- "
    Symbol  ,    Name
    EURUSD=X,    EUR/USD   
    JPY=X   ,    USD/JPY  
    GBPUSD=X,    GBP/USD
    AUDUSD=X,    AUD/USD
    NZDUSD=X,    NZD/USD
    EURJPY=X,    EUR/JPY
    GBPJPY=X,    GBP/JPY
    EURGBP=X,    EUR/GBP
    EURCAD=X,    EUR/CAD
    EURSEK=X,    EUR/SEK
    EURCHF=X,    EUR/CHF
    EURHUF=X,    EUR/HUF
    CNY=X   ,    USD/CNY
    HKD=X   ,    USD/HKD
    SGD=X   ,    USD/SGD
    INR=X   ,    USD/INR
    MXN=X   ,    USD/MXN
    PHP=X   ,    USD/PHP
    IDR=X   ,    USD/IDR
    THB=X   ,    USD/THB
    MYR=X   ,    USD/MYR
    ZAR=X   ,    USD/ZAR
    RUB=X   ,    USD/RUB
    "
 
#-------------------------------------------
# split symbols and make vector
#-------------------------------------------
df <- read.table(text = str_trim(vstr_symbol), 
                 sep = ",", header = TRUE)
df <- as.data.frame(df); df
 
df$Symbol <- str_trim(gsub("[\t\r\n,]", "", df$Symbol))
df$Name   <- str_trim(gsub("[\t\r\n,]", "", df$Name))
df
nc <- nrow(df) # number of exchange rate
 
#-------------------------------------------
# read price information
#-------------------------------------------
sdate <- as.Date("2004-01-01")
edate <- as.Date("2022-07-31")
getSymbols(df$Symbol, from=sdate, to=edate)
 
#-------------------------------------------
# collect only adjusted prices
#-------------------------------------------
price <- NULL
for(i in 1:nc) {
    eval(parse(text=paste0(
        "price <- cbind(price,`",
        gsub("\\^","",df$Symbol[i]),"`[,6])")))
}
 
# modify column Name as only symbol
colnames(price) <- gsub(".X.Adjusted", "", colnames(price))
 
# convert to data.frame with the first column as Date
df.price <- cbind(time=time(price), as.data.frame(price))
rownames(df.price) <- NULL
 
#-------------------------------------------
# print time series of daily prices
#-------------------------------------------
head(df.price,3)
tail(df.price,3)

Running the above R code displays the status of data reading process as follows.

Finally, we can get the collection of individual exchange rates.

For additional insight on this topic and to download the R script, visit https://kiandlee.blogspot.com/2022/08/r-code-snippet-read-historical-daily.html.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

Disclosure: Forex

There is a substantial risk of loss in foreign exchange trading. The settlement date of foreign exchange trades can vary due to time zone differences and bank holidays. When trading across foreign exchange markets, this may necessitate borrowing funds to settle foreign exchange trades. The interest rate on borrowed funds must be considered when computing the cost of trades across multiple markets.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.