knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(lubridate)

library(tidyverse)

library(readxl)

devtools::load_all()
fx_df = read_rds(paste0(
  file.path(Sys.getenv("USERPROFILE"), fsep = "\\"),
  "\\OneDrive - Bank Of Israel\\Data\\BoI\\FX\\fx_df.rds")) %>% 
  filter(complete.cases(.))

temp_fx = import_fx_rates()
fx_fame_data = read_xlsx(paste0("C:\\Users\\internet\\OneDrive - Bank Of Israel\\",
                                "Data\\BoI\\FX\\fx_data_frm_fame.xlsx"),
                         col_types = c("date", rep("numeric",11))) %>% 
  mutate(date =dmy(date))

fx_df = import_fx_rates() %>% 
  filter(!is.na(date))


fx_balance = read_csv(paste0(
  file.path(Sys.getenv("USERPROFILE"), fsep = "\\"),
  "\\OneDrive - Bank Of Israel\\Data\\BoI\\FX\\Balance\\FX_Balance.csv"),
  col_names = c("date","fx_balance")) %>% 
  mutate(date = dmy(date))

fx_purchases = read_csv(paste0(
  file.path(Sys.getenv("USERPROFILE"), fsep = "\\"),
  "\\OneDrive - Bank Of Israel\\Data\\BoI\\FX\\Balance\\FX_Purchases.csv")) %>% 
  mutate(date = dmy(date))
volume_data_list = import_fx_trade_volume()
band_df = import_band_data()
fx_df %>% 
  filter(currency == "us_dollar") %>% 
  filter(year(date) > 2000) %>%
  ggplot(aes(x = date, y = value)) + 
  geom_line() + 
  xlab(NULL) + ylab(NULL) + ggtitle("USD-ILS exchange rate") + 
  theme_bw()
fx_balance %>% 
  filter(year(date) > 2000) %>%
  ggplot(aes(x = date, y = fx_balance)) + 
  geom_line() + 
  scale_y_continuous(labels = scales::comma_format(scale = 1000 ^ (-1))) + 
  xlab(NULL) + ylab(NULL) + ggtitle("FX reserves (billions USD)")
fx_balance %>% 
  filter(year(date) > 2000) %>%
  mutate(fx_balance = c(NA,diff(fx_balance))) %>% 
  filter(complete.cases(.)) %>% 
  ggplot(aes(x = date, y = fx_balance,
             fill = if_else(fx_balance > 0, "up", "down"))) + 
  geom_col() + 
  scale_fill_manual(values = c("up" = "darkgreen", "down" = "red")) + 
  scale_y_continuous(labels = scales::comma_format(scale = 1000 ^ (-1))) + 
  xlab(NULL) + ylab(NULL) + ggtitle("FX reserves changes (billions USD)") + 
  theme(legend.position = "none")
volume_data_list$old_format_data %>% 
  filter(type == "inc_swaps") %>% 
  ggplot(aes(x = date, y = value, fill = category)) + 
  xlab(NULL) + ylab("Millions USD") + ggtitle("FX trading volume") + 
  scale_y_continuous(labels = scales::comma_format()) + 
  geom_area()
volume_data_list$daily_df %>% 
  pivot_longer(-date) %>% 
  filter(date > as.Date("2011-01-01")) %>% 
  ggplot(aes(x = date, y = value, fill = name)) + 
  xlab(NULL) + ylab("Millions USD") + ggtitle("FX trading volume") + 
  scale_y_continuous(labels = scales::comma_format()) + 
  geom_area()
fx_purchases %>% 
  pivot_longer(-date, names_to = "type") %>% 
  filter(!type == "total_buy") %>% 
  ggplot(aes(x = date, y = value, fill = type)) + 
  geom_col() + 
  ylab(NULL) + xlab(NULL) + ggtitle("FX purchases (USD billions)")
band_df %>% 
  left_join(fx_df %>% 
              filter(currency == "us_dollar") %>% 
              select(-currency), by = "date") %>% 
  rename(fx_rate = value) %>% 
  pivot_longer(-date) %>% 
  ggplot(aes(x = date, y = value, color = name)) + 
  geom_line() + 
  scale_color_manual(values = c("fx_rate" = "black",
                                "lower_bound" = "cyan4",
                                "upper_bound" = "cyan4")) + 
  xlab(NULL) + ylab(NULL) + ggtitle("FX band")

Purchase analysis

int_analysis_df = fx_df %>%
  filter(currency == "us_dollar") %>%
  filter(year(date) >= 2008) %>% 
  group_by(year_mon = zoo::as.yearmon(date)) %>%
  summarise(avg_rate = mean(value, na.rm = TRUE),
            sd_rate = sd(value, na.rm = TRUE)) %>%
  mutate(rate_change = avg_rate / lag(avg_rate) - 1) %>%
  left_join(
    fx_purchases %>%
      group_by(year_mon = zoo::as.yearmon(date)) %>%
      summarise(monthly_buy = sum(total_buy, na.rm = TRUE)) %>%
      mutate(purchase_change = monthly_buy / lag(monthly_buy) - 1)
  )
int_analysis_df %>% 
  ggplot(aes(x = year_mon, y = avg_rate)) + 
  geom_line() + 
  scale_y_continuous(labels = scales::percent_format()) + 
  xlab(NULL) + ylab(NULL) + 
  ggtitle("USD rate (monthy)")
int_analysis_df %>% 
  ggplot(aes(x = year_mon, y = sd_rate)) + 
  geom_line() + 
  scale_y_continuous(labels = scales::percent_format()) + 
  xlab(NULL) + ylab(NULL) + 
  ggtitle("USD rate volatility (monthy)")

There is no unusual volatility after the Coronavirus (2020 March) crisis

int_analysis_df %>% 
  filter(year(year_mon) >= 2016) %>% 
  ggplot(aes(x = year_mon,y = avg_rate - 3, fill = monthly_buy > 0)) + 
  geom_col()


MichaelGurkov/LearningMaterials documentation built on July 9, 2022, 5:17 p.m.