## ----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) ## ----fetch-prices------------------------------------------------------------- # # Define your portfolio of tickers # symbols <- c("AAPL", "MSFT", "GOOGL", "AMZN") # # # Download 1 year of daily historical prices (period = "1y" is the default) # portfolio_prices <- yf_download_prices( # tickers = symbols, # interval = "1d" # ) # # head(portfolio_prices) # #> # A tibble: 6 × 8 # #> symbol date open high low close adj_close volume # #> # #> 1 AAPL 2023-08-18 13:30:00 232. 233. 230. 231. 230. 41235600 # #> 2 AAPL 2023-08-19 13:30:00 231. 233. 229. 231. 230. 38945200 # #> 3 AAPL 2023-08-20 13:30:00 230. 230. 226. 226. 225. 45120300 # #> 4 AAPL 2023-08-21 13:30:00 226. 227. 224. 225. 224. 39870100 # #> 5 AAPL 2023-08-22 13:30:00 226. 229. 225. 228. 227. 42319800 # #> 6 AAPL 2023-08-25 13:30:00 226. 229. 226. 227. 226. 37651000 ## ----normalize---------------------------------------------------------------- # portfolio_performance <- portfolio_prices |> # # Group calculations by ticker and ensure chronological sorting within group # group_by(symbol) |> # arrange(date, .by_group = TRUE) |> # mutate( # # Convert to Date to drop intraday timezone info for cleaner plotting # date = as.Date(date), # # Normalize price to 100 on the first day # normalized_price = (close / close[1]) * 100 # ) |> # ungroup() # # # View the latest normalized performance # portfolio_performance |> # group_by(symbol) |> # slice_tail(n = 1) |> # select(symbol, date, close, normalized_price) # #> # A tibble: 4 × 4 # #> symbol date close normalized_price # #> # #> 1 AAPL 2024-08-21 225. 118. # #> 2 AMZN 2024-08-21 182. 135. # #> 3 GOOGL 2024-08-21 163. 122. # #> 4 MSFT 2024-08-21 421. 128. ## ----plot--------------------------------------------------------------------- # ggplot(portfolio_performance, aes(x = date, y = normalized_price, color = symbol)) + # geom_line(linewidth = 0.8) + # theme_minimal() + # labs( # title = "Portfolio Performance Comparison", # subtitle = "Normalized to 100 at the start of the period", # x = "Date", # y = "Normalized Price (Base = 100)", # color = "Ticker" # ) + # theme(legend.position = "bottom") ## ----full-workflow------------------------------------------------------------ # library(yahoofinancer) # library(dplyr) # library(ggplot2) # # # 1. Fetch prices for a portfolio of symbols # symbols <- c("AAPL", "MSFT", "GOOGL", "AMZN") # portfolio_prices <- yf_download_prices(tickers = symbols, interval = "1d") # # # 2. Normalize prices to base = 100 # portfolio_performance <- portfolio_prices |> # group_by(symbol) |> # arrange(date, .by_group = TRUE) |> # mutate( # date = as.Date(date), # normalized_price = (close / close[1]) * 100 # ) |> # ungroup() # # # 3. Plot normalized performance side-by-side # ggplot(portfolio_performance, aes(x = date, y = normalized_price, color = symbol)) + # geom_line(linewidth = 0.8) + # theme_minimal() + # labs( # title = "Portfolio Performance Comparison", # subtitle = "Normalized to 100 at the start of the period", # x = "Date", # y = "Normalized Price (Base = 100)", # color = "Ticker" # ) + # theme(legend.position = "bottom") ## ----validate-example--------------------------------------------------------- # # Keep only symbols that Yahoo recognizes # clean_symbols <- validate(c("AAPL", "NOTREAL", "MSFT")) # clean_symbols # #> [1] "AAPL" "MSFT"