Site icon R-bloggers

R code snippet : Read Historical Daily Exchange Rates

[This article was first published on SH Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< !--shlee --> This post shows how to read daily historical exchange rates given symbols as a string.


Read historical exchange rates


I collected the symbols of exchnage 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.

< !--shlee -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#========================================================#
# 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)
 
cs


< !--shlee --> Running the above R code displays the status of data reading process as follows.

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



< !--shlee -->
To leave a comment for the author, please follow the link and comment on their blog: SH Fintech Modeling.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.