Skip to content

Instantly share code, notes, and snippets.

@jkr216
Created November 13, 2022 14:05
Show Gist options
  • Save jkr216/be0093d26c80fc3967605b0dc10ffb99 to your computer and use it in GitHub Desktop.
Save jkr216/be0093d26c80fc3967605b0dc10ffb99 to your computer and use it in GitHub Desktop.
```{r, setup}
library(tidyverse)
library(timetk)
library(tidyquant)
library(readxl)
library(janitor)
library(gt)
library(gtExtras)
library(priceR)
library(scales)
```
For more background on how the DXY is constructed, please see this methodology piece from the ICE [here](https://www.theice.com/publicdocs/data/ICE_FX_Indexes_Methodology.pdf) .
The DXY measures the value of the dollar compared against a weighted basket of foreign currencies.[^1] When pundits say "the value of the dollar is rising", they often mean the DXY is increasing.
[^1]: For a good history of the dollar and what can cause it's value to rise, see [this esssay](https://www.lynalden.com/global-dollar-short-squeeze/) by Lyn Alden and [this essay](https://www.lynalden.com/fraying-petrodollar-system/) by Lyn Alden and [this essay](https://jameslavish.substack.com/p/-the-dollar-index-dxy-and-dollar) by James Lavish.
We will use a new package called `priceR` to source this data. For an introduction to this package and how it sources exchange rate data, see [here](https://stevecondylios.github.io/priceR/).
Let's first create a tibble to hold our currency pair symbols and names.
```{r}
exchange_rate_symbols <-
tibble(
symbol = c("EUR",
"JPY",
"GBP",
"CAD",
"SEK",
"CHF"),
currency = as_factor(c("euro",
"jap_yen",
"brit_pound",
"can_dollar",
"swed_krona",
"swiss_franc"))
)
```
```{r}
# create symbol function for exchange rate retrieval
exchange_get <- function(symbol,
starting_date = "2010-01-01"){
historical_exchange_rates( from = "USD",
to = symbol,
start_date = starting_date,
end_date = today()) %>%
rename(rate = 2) %>%
mutate(currency_name = symbol)
}
```
Next we use `map_dfr()` to pull in exchange rate data on all the symbols in our tibble.
```{r}
exchange_rates_tbl <-
map_dfr(exchange_rate_symbols %>%
pull(symbol),
exchange_get,
starting_date = "2019-01-01")
#
exchange_rates_tbl %>%
group_by(currency_name) %>%
slice(1, n())
```
Let's visualize these exchange rates over time. The y-axis is the number of currency units needed to buy one USD. As the number goes up, the base currency's value is going down and the USD is getting "stronger" relative to that currency.
```{r}
exchange_rates_tbl %>%
filter(date >= "2022-01-01") %>%
mutate(currency_name = as_factor(currency_name)) %>%
arrange(currency_name) %>%
ggplot(aes(x = date, y = rate, color = currency_name)) +
geom_line(show.legend = F) +
scale_x_date(
date_breaks = "3 months",
date_labels = ("%b %y")
)+
theme_minimal(
) +
facet_wrap(~currency_name, scales = "free_y",
ncol = 2) +
labs(x = "", y = "", title = "DXY Components")
```
Let's create a tibble to align weights and currency symbols.
```{r}
dxy_components_weights <-
tribble(
~"currency_name", ~"weight",
"EUR", "57.60",
"JPY", "13.60",
"GBP", "11.90",
"CAD", " 9.10",
"SEK", " 4.20",
"CHF", " 3.60",
) %>%
mutate(weight = as.numeric(weight)/100)
```
We can use `gt()` again to display the weights and current exchange rates for each of these DXY components.
`gt()` has a built-in method for displaying currency symbols in its `fmt_currency()` function.
```{r}
exchange_rates_tbl %>%
filter(date == max(date)) %>%
left_join(dxy_components_weights, by = "currency_name") %>%
select( currency_name, units_per_dollar = rate, weight) %>%
gt(rowname_col = "currency_name") %>%
cols_label(
units_per_dollar = "Units per Dollar"
) %>%
fmt_currency(
units_per_dollar,
rows = 1,
currency = "EUR"
) %>%
fmt_currency(
units_per_dollar,
rows = 2,
currency = "JPY"
) %>%
fmt_currency(
units_per_dollar,
rows = 3,
currency = "GBP"
) %>%
fmt_currency(
units_per_dollar,
rows = 4,
currency = "CAD"
) %>%
fmt_currency(
units_per_dollar,
rows = 5,
currency = "SEK"
) %>%
fmt_currency(
units_per_dollar,
rows = 6,
currency = "CHF"
) %>%
fmt_percent(
weight
) %>%
tab_header(
title = "DXY Component Weights and Latest Value"
) %>%
tab_source_note(
source_note = "Source: Weights from ICE and Values from priceR package"
) %>%
gt_theme_538()
```
```{r}
reconstructed_dxy <-
exchange_rates_tbl %>%
group_by(date) %>%
left_join(dxy_components_weights, by = "currency_name") %>%
mutate(
dxy_contribution = rate^weight,
total = prod(dxy_contribution),
dxy = total * 50.14348112,
currency_name = as_factor(currency_name)) %>%
group_by(date) %>%
summarise(dxy = mean(dxy))
reconstructed_dxy %>%
ggplot(aes(x = date, y = dxy)) +
geom_line(color = "steelblue") +
theme_minimal() +
scale_y_continuous(labels = number,
breaks = pretty_breaks(10)) +
labs(x = "", y = "", title = "DXY History Reconstructed",
caption = "Source: Fed Reserve Data, ICE methodology, Author Chart")
```
By way of comparison, here is the DXY chart from Yahoo! Finance that shows us the most recent few months and is current up to the most recent day.
```{r}
library(XML)
url <-
read_lines("https://finance.yahoo.com/quote/DX-Y.NYB/history?p=DX-Y.NYB")
html <- htmlTreeParse(url,
useInternalNodes = TRUE,
asText = TRUE)
tableNodes <- getNodeSet(html, "//table")
yahoo_dxy <-
readHTMLTable(tableNodes[[1]]) %>%
clean_names() %>%
select(date, close) %>%
mutate(date =
parse_date_time(date, orders = "%b%d,%Y")
%>% ymd(),
close = as.numeric(close)) %>%
fill(close, .direction = "down") %>%
rename(yahoo = close)
yahoo_dxy %>%
ggplot(aes(x = date, y = yahoo)) +
geom_line(color = "darkgreen") +
theme_minimal() +
scale_y_continuous(labels = number,
breaks = pretty_breaks(10)) +
labs(x = "", y = "", title = "DXY Prev 4 Months",
caption = "Source: Yahoo! Finance")
```
Let's compare the yahoo! results to our reconstruction.
```{r}
yahoo_dxy %>%
left_join(
reconstructed_dxy %>%
rename(reconstructed = dxy)
) %>%
pivot_longer(
-date
) %>%
ggplot(aes(x = date, y = value, color = name)) +
geom_line() +
theme_minimal() +
scale_y_continuous(labels = number,
breaks = pretty_breaks(10)) +
labs(x = "", y = "", title = "Yahoo DXY v. Our Reconstruction",
color= NULL,
caption = "Source: Fed Reserve Data, ICE methodology, Author Chart")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment