## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----setup--------------------------------------------------------------------
# # Install required packages if not already installed:
# # install.packages(c("yahoofinancer", "dplyr", "ggplot2"))
# 
# library(yahoofinancer)
# library(dplyr)
# library(ggplot2)

## ----get-currencies-----------------------------------------------------------
# currencies <- get_currencies()
# 
# head(currencies)
# #>   short_name         long_name symbol   local_long_name
# #> 1        USD         US Dollar    USD         US Dollar
# #> 2        EUR              Euro    EUR              Euro
# #> 3        GBP    Pound Sterling    GBP    Pound Sterling
# #> 4        JPY      Japanese Yen    JPY      Japanese Yen
# #> 5        CHF       Swiss Franc    CHF       Swiss Franc
# #> 6        AUD Australian Dollar    AUD Australian Dollar

## ----convert-dates------------------------------------------------------------
# gbp_usd <- currency_converter(
#   from  = "GBP",
#   to    = "USD",
#   start = "2024-01-01",
#   end   = "2024-12-31"
# )
# 
# head(gbp_usd)
# #>                  date   high    low   open  close volume adj_close
# #> 1 2024-01-02 00:00:00 1.2734 1.2577 1.2732 1.2602      0    1.2602
# #> 2 2024-01-03 00:00:00 1.2716 1.2595 1.2678 1.2631      0    1.2631
# #> 3 2024-01-04 00:00:00 1.2709 1.2602 1.2689 1.2677      0    1.2677
# #> 4 2024-01-05 00:00:00 1.2721 1.2614 1.2705 1.2717      0    1.2717
# #> 5 2024-01-08 00:00:00 1.2741 1.2698 1.2718 1.2720      0    1.2720
# #> 6 2024-01-09 00:00:00 1.2743 1.2688 1.2694 1.2694      0    1.2694

## ----rolling-forex------------------------------------------------------------
# # Example: Rolling 1-month window with weekly candle intervals
# weekly_rates <- currency_converter(
#   from     = "GBP",
#   to       = "USD",
#   period   = "1mo",
#   interval = "1wk"
# )

## ----fetch-two-pairs----------------------------------------------------------
# eur_usd <- currency_converter(
#   from  = "EUR",
#   to    = "USD",
#   start = "2024-01-01",
#   end   = "2024-12-31"
# )
# 
# fx <- bind_rows(
#   gbp_usd |> mutate(pair = "GBP/USD"),
#   eur_usd |> mutate(pair = "EUR/USD")
# )

## ----normalize----------------------------------------------------------------
# fx_performance <- fx |>
#   group_by(pair) |>
#   arrange(date, .by_group = TRUE) |>
#   mutate(
#     date = as.Date(date),
#     normalized_rate = (close / close[1]) * 100
#   ) |>
#   ungroup()
# 
# # Where did each pair end the year?
# fx_performance |>
#   group_by(pair) |>
#   slice_tail(n = 1) |>
#   select(pair, date, close, normalized_rate)
# #> # A tibble: 2 × 4
# #>   pair    date       close normalized_rate
# #>   <chr>   <date>     <dbl>           <dbl>
# #> 1 EUR/USD 2024-12-31 1.038            96.2
# #> 2 GBP/USD 2024-12-31 1.252            98.6

## ----plot---------------------------------------------------------------------
# ggplot(fx_performance, aes(x = date, y = normalized_rate, color = pair)) +
#   geom_line(linewidth = 0.8) +
#   geom_hline(yintercept = 100, linetype = "dashed", color = "grey50") +
#   theme_minimal() +
#   labs(
#     title = "Pound vs Euro: Performance Against the US Dollar",
#     subtitle = "Daily closing rates in 2024, normalized to 100 at the start of the year",
#     x = "Date",
#     y = "Normalized Rate (Base = 100)",
#     color = "Pair"
#   ) +
#   theme(legend.position = "bottom")

## ----full-workflow------------------------------------------------------------
# library(yahoofinancer)
# library(dplyr)
# library(ggplot2)
# 
# # 1. Fetch historical exchange rates for two currency pairs
# gbp_usd <- currency_converter(
#   from  = "GBP",
#   to    = "USD",
#   start = "2024-01-01",
#   end   = "2024-12-31"
# )
# 
# eur_usd <- currency_converter(
#   from  = "EUR",
#   to    = "USD",
#   start = "2024-01-01",
#   end   = "2024-12-31"
# )
# 
# # 2. Combine and normalize to base = 100
# fx_performance <- bind_rows(
#   gbp_usd |> mutate(pair = "GBP/USD"),
#   eur_usd |> mutate(pair = "EUR/USD")
# ) |>
#   group_by(pair) |>
#   arrange(date, .by_group = TRUE) |>
#   mutate(
#     date = as.Date(date),
#     normalized_rate = (close / close[1]) * 100
#   ) |>
#   ungroup()
# 
# # 3. Plot normalized exchange rates side-by-side
# ggplot(fx_performance, aes(x = date, y = normalized_rate, color = pair)) +
#   geom_line(linewidth = 0.8) +
#   geom_hline(yintercept = 100, linetype = "dashed", color = "grey50") +
#   theme_minimal() +
#   labs(
#     title = "Pound vs Euro: Performance Against the US Dollar",
#     subtitle = "Daily closing rates in 2024, normalized to 100 at the start of the year",
#     x = "Date",
#     y = "Normalized Rate (Base = 100)",
#     color = "Pair"
#   ) +
#   theme(legend.position = "bottom")

