library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8      ✔ TTR                  0.24.4
## ✔ quantmod             0.4.26     ✔ xts                  0.14.1
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date()                 masks base::as.Date()
## ✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary()            masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(dplyr)
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:xts':
## 
##     first, last
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
symbol <- "TSLA"
start_date <- "2022-01-01"
end_date <- "2022-12-31"

tsla_data <- tq_get(symbol, from = start_date, to = end_date)

head(tsla_data)
## # A tibble: 6 × 8
##   symbol date        open  high   low close    volume adjusted
##   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
## 1 TSLA   2022-01-03  383.  400.  379.  400. 103931400     400.
## 2 TSLA   2022-01-04  397.  403.  374.  383. 100248300     383.
## 3 TSLA   2022-01-05  382.  390.  360.  363.  80119800     363.
## 4 TSLA   2022-01-06  359   363.  340.  355.  90336600     355.
## 5 TSLA   2022-01-07  360.  360.  337.  342.  84164700     342.
## 6 TSLA   2022-01-10  333.  353.  327.  353.  91815000     353.
ggplot(tsla_data, aes(x = date, y = adjusted)) +
  geom_line(color = "blue") +
  labs(title = "Tesla Stock Price in 2022",
       x = "Date",
       y = "Adjusted Closing Price") +
  theme_minimal()

tsla_monthly_returns <- tsla_data %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "monthly",
               type = "log")

ggplot(tsla_monthly_returns, aes(x = date, y = monthly.returns)) +
  geom_col(fill = "red") +
  labs(title = "Tesla Monthly Returns in 2022",
       x = "Date",
       y = "Log Return") +
  theme_minimal()