eur and eurusds from the xchangeR packageeur-eurusds.RmdThis vignette demos the usage of two functions from the xchangeR package:
eur converts a number to a string by adding the Euro sign (€), rounding up to 2 digits and using the comma as the separator.eurusds looks up the daily USD/EUR exchange rate via an API call for the specified date range.In this demo we’re going to:
ggplot.xchangeR packageOther packages used for this demo:
binancerdplyrdata.tableggplot2scalesLet’s look at the daily Bitcoin prices in USD for the past 45 days. We’re only interested in the day (open_time) and the opening price (open).
At this point we have the daily prices for Bitcoin, but only in USD. In order to see the prices in EUR, we have to look up the daily exchange rates by utilizing the eurusds function and join them to btc_prices.
exchange_rates <- eurusds(last_x_days = 45)
head(left_join(btc_prices, exchange_rates, by = "date"))
#> date price exchange_rate
#> 1 2019-04-18 5202.41 0.8888889
#> 2 2019-04-19 5258.44 NA
#> 3 2019-04-20 5258.68 NA
#> 4 2019-04-21 5292.91 NA
#> 5 2019-04-22 5257.41 NA
#> 6 2019-04-23 5357.14 0.8892841As we can see, there are some NA values in the exchange_rate column. The reason for this is that the exchange rates are only available from the API for weekdays. To overcome this obstacle, we have to apply a rolling join, rather than a left join.
# Set the keys for the join
setkey(btc_prices, date)
setkey(exchange_rates, date)
btc_prices <- exchange_rates[btc_prices, roll = TRUE] %>%
na.omit()
head(btc_prices)
#> date exchange_rate price
#> 1: 2019-04-18 0.8888889 5202.41
#> 2: 2019-04-19 0.8888889 5258.44
#> 3: 2019-04-20 0.8888889 5258.68
#> 4: 2019-04-21 0.8888889 5292.91
#> 5: 2019-04-22 0.8888889 5257.41
#> 6: 2019-04-23 0.8892841 5357.14Now we can calculate the price in EUR, and format it as such with the eur function.
btc_prices <- btc_prices %>%
mutate(price_in_eur = eur(exchange_rate * price)) %>%
select(date, price_in_eur)
head(btc_prices)
#> date price_in_eur
#> 1 2019-04-18 \2004,624.36
#> 2 2019-04-19 \2004,674.17
#> 3 2019-04-20 \2004,674.38
#> 4 2019-04-21 \2004,704.81
#> 5 2019-04-22 \2004,673.25
#> 6 2019-04-23 \2004,764.02
# E.g. the price for the first day in Euro format:
btc_prices$price_in_eur[1]
#> [1] "\2004,624.36"As we converted the price to Euro format in the previous example, in order to visualize it, we’ll utilize the uneur function from the xchangeR package that transforms the string to a number.
ggplot(btc_prices, aes(date, uneur(price_in_eur))) +
geom_col(fill = "skyblue2") +
ggtitle("Bitcoin prices in Euro for the past 45 days") +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 7500, 500), labels = eur) +
scale_x_date(labels = date_format("%m-%d"), date_breaks = "5 days") +
geom_hline(yintercept = mean(uneur(btc_prices$price_in_eur)), color = "indianred2", size = 1) +
annotate(geom = "text", x = min(btc_prices$date) + 6,
y = mean(uneur(btc_prices$price_in_eur)) + 250, size = 3.5,
label = paste("Mean price in Euro:", eur(mean(uneur(btc_prices$price_in_eur))))) +
theme(axis.text.x = element_text(angle = 45),
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"))