INSTRUCTIONS (READ CAREFULLY)

Q1 (10 pts): Install and load required packages

# install.packages("quantmod")
# install.packages("xts")
# install.packages("ggplot2")
# install.packages("data.table")
# install.packages("reshape2")
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.5.2
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.5.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.5.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
library(data.table)
## Warning: package 'data.table' was built under R version 4.5.2
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:zoo':
## 
##     yearmon, yearqtr
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.5.2
## 
## Attaching package: 'reshape2'
## The following objects are masked from 'package:data.table':
## 
##     dcast, melt

Q2 (10 pts): Download AAPL stock price data (2008–2009): For Apple Stock prices

getSymbols("AAPL", from = "2008-01-01", to = "2009-12-31")
## [1] "AAPL"
head(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2008-01-02  7.116786  7.152143 6.876786   6.958571  1079178800      5.843455
## 2008-01-03  6.978929  7.049643 6.881786   6.961786   842066400      5.846155
## 2008-01-04  6.837500  6.892857 6.388929   6.430357  1455832000      5.399887
## 2008-01-07  6.473214  6.557143 6.079643   6.344286  2072193200      5.327610
## 2008-01-08  6.433571  6.516429 6.100000   6.116071  1523816000      5.135967
## 2008-01-09  6.117857  6.410714 6.010714   6.407143  1813882000      5.380394

Q3 (10 pts): Plot AAPL closing prices

chartSeries(Cl(AAPL))

Q4 (10 pts): Download WMT stock price data (2008-2009: For Walmart Stock prices

getSymbols("WMT", from = "2008-01-01", to = "2009-12-31")
## [1] "WMT"
head(WMT)
##            WMT.Open WMT.High  WMT.Low WMT.Close WMT.Volume WMT.Adjusted
## 2008-01-02 15.80333 15.87667 15.48667  15.63333   59755800     10.80396
## 2008-01-03 15.57333 15.63333 15.41000  15.46000   59403000     10.68417
## 2008-01-04 15.30333 15.45667 15.12333  15.24000   61053600     10.53213
## 2008-01-07 15.26667 15.60333 15.19667  15.52000   60979500     10.72564
## 2008-01-08 15.53000 15.69333 15.29333  15.32333   57051300     10.58973
## 2008-01-09 15.32333 15.66667 15.08000  15.63333   81592800     10.80396

Q5 (10 pts): Merge AAPL and WMT closing prices

aapl_close <- Cl(AAPL)
wmt_close <- Cl(WMT)

merged_prices <- merge(aapl_close, wmt_close)

colnames(merged_prices) <- c("AAPL", "WMT")

head(merged_prices, 10)
##                AAPL      WMT
## 2008-01-02 6.958571 15.63333
## 2008-01-03 6.961786 15.46000
## 2008-01-04 6.430357 15.24000
## 2008-01-07 6.344286 15.52000
## 2008-01-08 6.116071 15.32333
## 2008-01-09 6.407143 15.63333
## 2008-01-10 6.357857 16.13333
## 2008-01-11 6.167500 15.90667
## 2008-01-14 6.385000 15.89000
## 2008-01-15 6.037143 15.66333

Q6 (10 pts): Function to extract multiple stock prices

get_price_data <-function(symbols,start, end,freq="daily"){
  price_data <- NULL
  
  for(sym in symbols) {
    temp <- getSymbols(Symbols = sym, from = start, to = end, auto.assign = FALSE,periodicity=freq)
  
  names(temp) <- c("Open", "High", "Low","Close","Volume","Adjusted")
  
  temp <- temp[,c("Close")]
  
  names(temp) <- c(sym)
  
  if(is.null(price_data)){
    price_data <- temp
  } else {
    price_data <- merge(price_data,temp)
  }
}

  price_data <- as.data.table(price_data)
  setnames(price_data,"index","date")

return(price_data)
}
prices_multi <- get_price_data(symbols = c("AAPL", "WMT", "MSFT"), start="2008-01-01",end="2009-12-31",freq="daily")

head(prices_multi)
##          date     AAPL      WMT  MSFT
##        <Date>    <num>    <num> <num>
## 1: 2008-01-02 6.958571 15.63333 35.22
## 2: 2008-01-03 6.961786 15.46000 35.37
## 3: 2008-01-04 6.430357 15.24000 34.38
## 4: 2008-01-07 6.344286 15.52000 34.61
## 5: 2008-01-08 6.116071 15.32333 33.45
## 6: 2008-01-09 6.407143 15.63333 34.44

Q7 (10 pts): Convert merged prices to long format

dt <- data.table(date = index(merged_prices), coredata(merged_prices))

dt_long <- melt(dt, id.vars = "date",
                variable.name = "ticker",
                value.name = "price")

head(dt_long)
##         date ticker    price
## 1 2008-01-02   AAPL 6.958571
## 2 2008-01-03   AAPL 6.961786
## 3 2008-01-04   AAPL 6.430357
## 4 2008-01-07   AAPL 6.344286
## 5 2008-01-08   AAPL 6.116071
## 6 2008-01-09   AAPL 6.407143

Q8 (10 pts): Plot merged stock prices (Close Prices for Apple and Walmart) in the same plot

ggplot(dt_long, aes(x = date, y = price, color = ticker)) +
  geom_line() +
  labs(title = "AAPL and WMT Closing Prices (2008–2009)",
       x = "Date",
       y = "Price")

Q9 (10 pts): Compute log returns + long format

# YOUR CODE HERE
get_returns <- function(x) {
  ret <- diff(log(x))
  ret <- na.omit(ret)
  return(ret)
}

returns <- get_returns(merged_prices)

dt_ret <- data.table(date = index(returns), coredata(returns))

dt_ret_long <- melt(dt_ret,
                    id.vars = "date",
                    variable.name = "ticker",
                    value.name = "return")

head(dt_ret_long)
##         date ticker        return
## 1 2008-01-03   AAPL  0.0004618899
## 2 2008-01-04   AAPL -0.0794059664
## 3 2008-01-07   AAPL -0.0134754947
## 4 2008-01-08   AAPL -0.0366346262
## 5 2008-01-09   AAPL  0.0464935463
## 6 2008-01-10   AAPL -0.0077220732

Q10 (10 pts): Plot returns + density

# Time-series plot of returns
ggplot(dt_ret_long, aes(x = date, y = return, color = ticker)) +
  geom_line() +
  labs(title = "Log Returns of AAPL and WMT",
       x = "Date",
       y = "Log Return")

# Density plot of returns
ggplot(dt_ret_long, aes(x = return, fill = ticker)) +
  geom_density(alpha = 0.4) +
  labs(title = "Return Distributions: AAPL vs WMT",
       x = "Log Return",
       y = "Density")

END OF EXAM