R Code Snippet: Read Historical Daily Exchange Rates

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(stringr) # trim
# Symbols of exchange rates, as of 2022-08-14
vstr_symbol <- "
    Symbol  ,    Name
    EURUSD=X,    EUR/USD   
    JPY=X   ,    USD/JPY  
    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))
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) {
        "price <- cbind(price,`",
# 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

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.