knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4 )
This 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
package# Install the package if necessary # remotes::install_github("szigony/xchangeR") library(xchangeR)
Other packages used for this demo:
binancer
dplyr
data.table
ggplot2
scales
library(binancer) library(dplyr) library(data.table) library(ggplot2) library(scales)
Let'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
).
btc_prices <- as.data.table( binance_klines("BTCUSDT", interval = "1d", limit = 45) %>% mutate(date = as.Date(open_time, format = "%Y-%m-%d"), price = open) %>% select(date, price) )
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"))
As 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)
Now 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) # E.g. the price for the first day in Euro format: btc_prices$price_in_eur[1]
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.