# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

# Choose stocks

symbols <- c("AAL", "SBUX", "BBY", "LOW", "SPY")

# Using tq_get() ----
prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2025-12-10")

2 Convert prices to returns by quarterly

asset_returns_tbl <- prices %>%

    # Calculate monthly returns
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "quarterly",
                 type = "log") %>%
    slice(-1) %>%
    ungroup() %>%

    # remane
    set_names(c("asset", "date", "returns"))

# period_returns = c("yearly", "quarterly", "monthly", "weekly")

3 Make plot

asset_returns_tbl %>%

    ggplot(aes(x = returns)) +
    geom_density(aes(col = asset), alpha = 1, show.legend = FALSE) +
    geom_histogram(aes(fill = asset), alpha = 0.45, binwidth = 0.01) +
    facet_wrap(~asset, ncol = 1, scales = "free_y") +
    guides(fill = "none") +

    labs(title = "Monthly Returns since 2013",
         x = "distribution",
         y = "monthly returns") +
    theme_update(plot.title = element_text(hjust = 0.5))

## 4 Interpret the plot BBY seems to have the best expected returns, as the stock shows a grouping of returns greater than that of any other stock. AAL looks to have the lowest expected returns. The largest possible gain would also come from BBY and the largest possible loss would come from AAL.