# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

symbols <- c("XOM", "QQQ", "SPY", "TSLA","CGC")

prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2018-10-15",
                 to = "2024-8-31")

2 Convert prices to returns by quarterly

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

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

3 Make plot

3 Make plot

asset_returns_tbl %>%
    
 ggplot(aes(x = returns)) +
    geom_density(aes(color = asset), show.legend = FALSE, alpha = 1) +
    
geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 1.5, binwidth = 0.015) +
    facet_wrap(~asset, ncol = 1) +

#labeling
labs(title = "Distribution of Quarterly, Returns 2018-2024",
         x = "Rate of Returns",
         y = "Frequency", 
    )

4 Interpret the plot

I would invest my money in QQQ and SPY for a stable growth as they have consistanly gotten positive returns with little negative returns. CGC I believe is the worst stock to invest in as their most common return is in negative, although they have had some high returns overall you are going to loose money. Exxon Mobile is also a very solid stock with mostly positive returns. If you want to have some volatility in your portfolio tesla is a good choice as it has a split of some positive and negative days but more positive than negative.

5 Change the global chunck options

Hide the code, messages, and warnings