Advertisement
binkleym

Untitled

Jun 16th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. ################################################################################
  2. ### Compare the relative performance of assets from a given start date
  3. ### Use the Yahoo ticker for the assets you wish to graph
  4. ################################################################################
  5.  
  6. library(tidyverse)
  7. library(RColorBrewer)
  8.  
  9. # Start/end dates
  10. date_start <- as.Date("2025-01-20")
  11. date_end <- as.Date(Sys.Date())
  12.  
  13. # Tickers of the assets to graph
  14. tickers <-
  15. tibble::tribble(
  16. ~symbol, ~label,
  17. "FSAGX", "Fidelity Select Gold Fund",
  18. "GC=F", "Price of Gold",
  19. "BRK-B", "Berkshire Hathaway",
  20. "FUAMX", "Fidelity Intermediate Treasury Fund",
  21. "DX-Y.NYB", "US Dollar Index",
  22. "^SPX", "S&P 500"
  23. ) %>%
  24. mutate(label = paste("[", symbol, "] - ", label, sep = ""))
  25.  
  26. # Fetch prices for the chosen tickers
  27. prices <-
  28. tidyquant::tq_get(tickers$symbol, get = "stock.prices", from = date_start, to = date_end) %>%
  29. select(symbol, date, close) %>%
  30. pivot_wider(id_cols = "date", names_from = "symbol", values_from = "close") %>%
  31. arrange(date) %>%
  32. tsibble::as_tsibble() %>%
  33. tsibble::fill_gaps() %>%
  34. mutate(across(!starts_with("date"), forecast::na.interp))
  35.  
  36. # Normalize stock price changes relative to date_start
  37. rel <-
  38. prices %>%
  39. mutate_if(is.numeric, ~ . / first(., order_by = date), na.rm = TRUE) %>%
  40. pivot_longer(-date, names_to = "symbol", values_to = "price")
  41.  
  42. # Make a tibble of final values to help us label them
  43. labels <-
  44. rel %>%
  45. pivot_wider(id_cols = "date", names_from = "symbol", values_from = "price") %>%
  46. filter(!if_all(-date, is.na)) %>%
  47. tail(n = 1) %>%
  48. pivot_longer(-date, names_to = "symbol", values_to = "price") %>%
  49. left_join(tickers, by = "symbol") %>%
  50. mutate(
  51. price_txt = ifelse(price > 1, paste("(+", round(100 * (price - 1), 1), "%)", sep = ""),
  52. paste("(-", round(100 * (1 - price), 1), "%)", sep = "")))
  53.  
  54. # Make shiny graph...
  55. ggplot() +
  56. theme_bw() +
  57. theme(legend.position = "none") +
  58. geom_line(data = rel, aes(x = date, y = price, color = symbol)) +
  59. geom_hline(yintercept = 1, linetype = "dotted") +
  60. scale_x_date(breaks = scales::pretty_breaks(10), limits = c(date_start, date_end + days(60))) +
  61. scale_y_continuous(breaks = scales::pretty_breaks(10), labels = scales::label_number(suffix = "x")) +
  62. scale_color_brewer(palette = "Dark2") +
  63. geom_label(data = labels, hjust = 0, aes(x = date + 0.03 * (date_end - date_start), y = price, label = paste(label, " ", price_txt, sep = ""), color = symbol)) +
  64. labs(x = "", y = "Performance", title = paste("Asset performance relative to", date_start))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement