2.7 Comparing Performance of Multiple Securities: Total Returns AABA replaced YHOO in the textbook. The shares of what used to be known as Yahoo under the ticker symbol YHOO started trading Monday as Altaba (think alternative Alibaba) and trade under the symbol AABA.Jun 19, 2017.

# To clean up the memory of your current R session run the following line
rm(list=ls(all=TRUE))

# Compare total returns of multiple securities - AMZN, IBM, AABA, ^GSPC
# Which of these investments performed better from 12/31/2010 to 12/31/2013
# Pay dividends: IBM
# Don't pay: AMZN, AABA, ^GSPC

#Step 1: Importing Price Data
library(quantmod)
data.AMZN <- getSymbols("AMZN", from = "2010-12-31", to = "2014-01-01", auto.assign = FALSE)
head(data.AMZN)
##            AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume
## 2010-12-31    181.96    182.30   179.51     180.00     3451900
## 2011-01-03    181.37    186.00   181.21     184.22     5331400
## 2011-01-04    186.15    187.70   183.78     185.01     5031800
## 2011-01-05    184.10    187.45   184.07     187.42     3418800
## 2011-01-06    186.50    187.41   185.25     185.86     3179700
## 2011-01-07    187.88    188.45   183.74     185.49     5221700
##            AMZN.Adjusted
## 2010-12-31        180.00
## 2011-01-03        184.22
## 2011-01-04        185.01
## 2011-01-05        187.42
## 2011-01-06        185.86
## 2011-01-07        185.49
data.AABA<- getSymbols("AABA", from = "2010-12-31", to = "2014-01-01", auto.assign = FALSE)
head(data.AABA)
##            AABA.Open AABA.High AABA.Low AABA.Close AABA.Volume
## 2010-12-31     16.74     16.76    16.47      16.63     7754500
## 2011-01-03     16.81     16.94    16.67      16.75    17684000
## 2011-01-04     16.71     16.83    16.57      16.59    11092800
## 2011-01-05     16.55     16.91    16.34      16.91    23447700
## 2011-01-06     16.90     17.34    16.77      17.06    30656800
## 2011-01-07     17.03     17.17    16.65      16.90    19869500
##            AABA.Adjusted
## 2010-12-31         16.63
## 2011-01-03         16.75
## 2011-01-04         16.59
## 2011-01-05         16.91
## 2011-01-06         17.06
## 2011-01-07         16.90
data.IBM <- getSymbols("IBM", from = "2010-12-31", to = "2014-01-01", auto.assign = FALSE)
head(data.IBM)
##            IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
## 2010-12-31  174.671  175.076 173.755    146.76    2969800     123.2834
## 2011-01-03  175.243  176.421 175.159    147.48    4603800     123.8883
## 2011-01-04  175.659  176.445 174.564    147.64    5060100     124.0227
## 2011-01-05  175.398  175.564 174.671    147.05    4657400     123.5271
## 2011-01-06  175.148  177.124 174.779    148.66    5029200     124.8795
## 2011-01-07  177.124  177.207 174.921    147.93    4135700     124.2663
data.GSPC <- getSymbols("^GSPC", from = "2010-12-31", to = "2014-01-01", auto.assign = FALSE)
head(data.GSPC)
##            GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume
## 2010-12-31   1256.76   1259.34  1254.19    1257.64  1799770000
## 2011-01-03   1257.62   1276.17  1257.62    1271.87  4286670000
## 2011-01-04   1272.95   1274.12  1262.66    1270.20  4796420000
## 2011-01-05   1268.78   1277.63  1265.36    1276.56  4764920000
## 2011-01-06   1276.29   1278.17  1270.43    1273.85  4844100000
## 2011-01-07   1274.41   1276.83  1261.70    1271.50  4963110000
##            GSPC.Adjusted
## 2010-12-31       1257.64
## 2011-01-03       1271.87
## 2011-01-04       1270.20
## 2011-01-05       1276.56
## 2011-01-06       1273.85
## 2011-01-07       1271.50
#to = "2013-12-31" in the book was replaced by to = "2014-01-01"
#B/c that code doesn't produce the output that includes stock price of 2013-12-31the date. 

# Step 2: Combine Data
multi <- data.AMZN[, 6] # Need to create a new data object before cbinding
head(multi)
##            AMZN.Adjusted
## 2010-12-31        180.00
## 2011-01-03        184.22
## 2011-01-04        185.01
## 2011-01-05        187.42
## 2011-01-06        185.86
## 2011-01-07        185.49
multi <- merge(multi, data.GSPC[, 6])
head(multi)
##            AMZN.Adjusted GSPC.Adjusted
## 2010-12-31        180.00       1257.64
## 2011-01-03        184.22       1271.87
## 2011-01-04        185.01       1270.20
## 2011-01-05        187.42       1276.56
## 2011-01-06        185.86       1273.85
## 2011-01-07        185.49       1271.50
multi <- merge(multi, data.AABA[, 6])
head(multi)
##            AMZN.Adjusted GSPC.Adjusted AABA.Adjusted
## 2010-12-31        180.00       1257.64         16.63
## 2011-01-03        184.22       1271.87         16.75
## 2011-01-04        185.01       1270.20         16.59
## 2011-01-05        187.42       1276.56         16.91
## 2011-01-06        185.86       1273.85         17.06
## 2011-01-07        185.49       1271.50         16.90
multi <- merge(multi, data.IBM[, 6])
head(multi)
##            AMZN.Adjusted GSPC.Adjusted AABA.Adjusted IBM.Adjusted
## 2010-12-31        180.00       1257.64         16.63     123.2834
## 2011-01-03        184.22       1271.87         16.75     123.8883
## 2011-01-04        185.01       1270.20         16.59     124.0227
## 2011-01-05        187.42       1276.56         16.91     123.5271
## 2011-01-06        185.86       1273.85         17.06     124.8795
## 2011-01-07        185.49       1271.50         16.90     124.2663
#multi <- cbind(multi, data.GSPC[, 6], data.AABA[, 6], data.IBM[, 6])
multi[c(1:3, nrow(multi)), ]
##            AMZN.Adjusted GSPC.Adjusted AABA.Adjusted IBM.Adjusted
## 2010-12-31        180.00       1257.64         16.63     123.2834
## 2011-01-03        184.22       1271.87         16.75     123.8883
## 2011-01-04        185.01       1270.20         16.59     124.0227
## 2013-12-31        398.79       1848.36         40.44     166.0890

# Step 3: Convert Data into a data.frame
multi.df <- cbind(index(multi), 
                  data.frame(multi))
names(multi.df) <- c("date", "AMZN", "GSPC", "AABA", "IBM")
multi.df[c(1:3, nrow(multi.df)), ]
##                  date   AMZN    GSPC  AABA      IBM
## 2010-12-31 2010-12-31 180.00 1257.64 16.63 123.2834
## 2011-01-03 2011-01-03 184.22 1271.87 16.75 123.8883
## 2011-01-04 2011-01-04 185.01 1270.20 16.59 124.0227
## 2013-12-31 2013-12-31 398.79 1848.36 40.44 166.0890

# Step 4: Calculate Normalized Values for Each Security
multi.df$AMZN.idx <- multi.df$AMZN / multi.df$AMZN[1]
multi.df$GSPC.idx <- multi.df$GSPC / multi.df$GSPC[1]
multi.df$AABA.idx <- multi.df$AABA/ multi.df$AABA[1]
multi.df$IBM.idx <- multi.df$IBM / multi.df$IBM[1]
multi.df[c(1:3, nrow(multi.df)), 6:9]
##            AMZN.idx GSPC.idx  AABA.idx  IBM.idx
## 2010-12-31 1.000000 1.000000 1.0000000 1.000000
## 2011-01-03 1.023444 1.011315 1.0072159 1.004906
## 2011-01-04 1.027833 1.009987 0.9975948 1.005996
## 2013-12-31 2.215500 1.469705 2.4317499 1.347213

# Step 5: Plot the Capital Appreciation of Each Security

# GSPC first
y.range = range(multi.df[, 6:9])
y.range
## [1] 0.6668671 2.4564041

plot(x = multi.df$date,
     y = multi.df$GSPC.idx,
     type = "l",
     xlab = "Date",
     ylab = "Value of Investment ($)",
     ylim = y.range,
     col = "black",
     lty = 1,
     lwd = 2,
     main = "Value of $1 Investment in AMZN, IBM, AABA, 
        And the S&P 500 Index Based on Total Returns
        December 31, 2010 - December 31, 2013")


# Add a line for AMZN, AABA, IBM

lines(x = multi.df$date,
      y = multi.df$AMZN.idx,
      col = "black",
      lty = 2,
      lwd = 1)
lines(x = multi.df$date,
      y = multi.df$IBM.idx,
      col = "gray40",
      lty = 1,
      lwd = 2)
lines(x = multi.df$date,
      y = multi.df$AABA.idx,
      col = "gray60",
      lty = 1,
      lwd = 1)
abline(h = 1, lty = 1, col = "black")
legend("topleft",
       c("S&P 500 Index", "AMZN", "IBM", "AABA"),
       col = c("black", "black", "gray40", "gray60"),
       lty = c(1, 2, 1, 1),
       lwd = c(2, 1, 2, 1))

Interpretation