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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 SH Fintech Modeling and is being posted with permission from SH Fintech Modeling. The views expressed in this material are solely those of the author and/or SH Fintech Modeling and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

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.