Step 1: Download 10 years of trading data from Yahoo Finance

# Load the 'quantmod' package
suppressWarnings(library(quantmod))
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# Download TSLA data from Yahoo Finance
tsla <- getSymbols.yahoo("TSLA", from = "2012-01-01", to = "2022-12-31", auto.assign = FALSE)

# Display the first few rows of the TSLA data
head(tsla)
##            TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 2012-01-03  1.929333  1.966667 1.843333   1.872000    13921500      1.872000
## 2012-01-04  1.880667  1.911333 1.833333   1.847333     9451500      1.847333
## 2012-01-05  1.850667  1.862000 1.790000   1.808000    15082500      1.808000
## 2012-01-06  1.813333  1.852667 1.760667   1.794000    14794500      1.794000
## 2012-01-09  1.800000  1.832667 1.741333   1.816667    13455000      1.816667
## 2012-01-10  1.829333  1.850667 1.816667   1.841333    10077000      1.841333

Step 2: Calculate monthly, quarterly, and yearly returns

# Load necessary libraries
suppressPackageStartupMessages(library(quantmod))
suppressPackageStartupMessages(library(dplyr))

# Calculate arithmetic returns
tsla_monthly_arithmetic <- periodReturn(tsla, period = 'monthly', type = 'arithmetic')
tsla_quarterly_arithmetic <- periodReturn(tsla, period = 'quarterly', type = 'arithmetic')
tsla_yearly_arithmetic <- periodReturn(tsla, period = 'yearly', type = 'arithmetic')

# Calculate geometric returns
tsla_monthly_geometric <- periodReturn(tsla, period = 'monthly', type = 'log')
tsla_quarterly_geometric <- periodReturn(tsla, period = 'quarterly', type = 'log')
tsla_yearly_geometric <- periodReturn(tsla, period = 'yearly', type = 'log')

# Display the first few rows of each return dataset
head(tsla_monthly_arithmetic)
##            monthly.returns
## 2012-01-31     0.004492222
## 2012-02-29     0.149294691
## 2012-03-30     0.114636605
## 2012-04-30    -0.110365157
## 2012-05-31    -0.109568341
## 2012-06-29     0.060677739
head(tsla_quarterly_arithmetic)
##            quarterly.returns
## 2012-03-30        0.28680067
## 2012-06-29       -0.15977455
## 2012-09-28       -0.06423775
## 2012-12-31        0.15676223
## 2013-03-28        0.11868917
## 2013-06-28        1.83346510
head(tsla_yearly_arithmetic)
##            yearly.returns
## 2012-12-31     0.17035262
## 2013-12-31     3.44139411
## 2014-12-31     0.47849488
## 2015-12-31     0.07913319
## 2016-12-30    -0.10966213
## 2017-12-29     0.45701711
head(tsla_monthly_geometric)
##            monthly.returns
## 2012-01-31     0.004482162
## 2012-02-29     0.139148442
## 2012-03-30     0.108528437
## 2012-04-30    -0.116944189
## 2012-05-31    -0.116048924
## 2012-06-29     0.058908080
head(tsla_quarterly_geometric)
##            quarterly.returns
## 2012-03-30        0.25215904
## 2012-06-29       -0.17408503
## 2012-09-28       -0.06639384
## 2012-12-31        0.14562492
## 2013-03-28        0.11215761
## 2013-06-28        1.04150038
head(tsla_yearly_geometric)
##            yearly.returns
## 2012-12-31     0.15730509
## 2013-12-31     1.49096832
## 2014-12-31     0.39102460
## 2015-12-31     0.07615811
## 2016-12-30    -0.11615426
## 2017-12-29     0.37639127

Step 3: Plot the ten-year monthly discrete and continuous returns, compare the two

# Load necessary libraries
library(ggplot2)

# Plotting monthly returns
ggplot() +
  geom_line(aes(x = index(tsla_monthly_arithmetic), y = tsla_monthly_arithmetic, color = "Arithmetic"), size = 1) +
  geom_line(aes(x = index(tsla_monthly_geometric), y = tsla_monthly_geometric, color = "Geometric"), size = 1) +
  labs(x = "Date", y = "Returns", title = "Monthly Returns of TSLA") +
  scale_color_manual(values = c("Arithmetic" = "blue", "Geometric" = "red")) +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "lightgray"),
    axis.text = element_text(color = "black"),
    axis.title = element_text(color = "black", size = 12, face = "bold"),
    plot.title = element_text(color = "black", size = 16, face = "bold", hjust = 0.5),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

Step 4: Use chartSeries to plot 10 years discrete returns

# Load necessary libraries
library(quantmod)

# Plotting daily returns
chartSeries(tsla, theme = chartTheme("white"), name = "TSLA") +
  theme(
    chart.background = element_rect(fill = "lightblue"),
    panel.background = element_rect(fill = "lightgray"),
    axis.title = element_text(color = "darkblue", size = 12, face = "bold"),
    axis.text = element_text(color = "darkblue"),
    major.grid.line = element_line(color = "darkblue"),
    minor.grid.line = element_line(color = "darkblue")
  )

## NULL

Step 5: Plot 3 technical analysis indicators with explanation

# Load necessary libraries
library(quantmod)

# Plotting technical analysis indicators
plot(tsla, theme = chartTheme("white"), name = "TSLA")

addBBands()

addRSI()

addMACD()

# Customize the theme
theme_update(
  panel.background = element_rect(fill = "lightgray"),
  axis.title = element_text(color = "darkgreen", size = 12, face = "bold"),
  axis.text = element_text(color = "darkgreen"),
  major.grid.line = element_line(color = "darkgreen"),
  minor.grid.line = element_line(color = "darkgreen")
)