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
# Define stock symbol and date range
stock_symbol <- "TSLA"
start_date <- "2022-01-01"
end_date <- "2022-12-31"
# Get Tesla stock data from Yahoo Finance
getSymbols(stock_symbol, src = "yahoo", from = start_date, to = end_date)
## [1] "TSLA"
# Extract adjusted closing prices
tsla_data <- TSLA[, "TSLA.Adjusted"]
# Plot the adjusted closing price
chartSeries(tsla_data, name = "Tesla Stock Price (2022)", theme = chartTheme("white"))

# Calculate daily returns
tsla_returns <- dailyReturn(tsla_data)
# Plot daily returns
plot(tsla_returns, main = "Tesla Daily Returns (2022)", col = "blue", type = "l")

# Summary statistics
summary(tsla_returns)
## Index daily.returns
## Min. :2022-01-03 Min. :-0.121841
## 1st Qu.:2022-04-02 1st Qu.:-0.028227
## Median :2022-07-05 Median :-0.001100
## Mean :2022-07-02 Mean :-0.003832
## 3rd Qu.:2022-10-01 3rd Qu.: 0.019591
## Max. :2022-12-30 Max. : 0.106776