Return analysis

Libraries installation:

install.packages(“quantmod”) install.packages(“PerformanceAnalytics”) install.packages(“dygraphs”) install.packages(“corrplot”)

#install.packages("quantmod")
#install.packages("PerformanceAnalytics")
#install.packages("dygraphs")
#install.packages("corrplot")

Load packages:

library(quantmod)
## Ładowanie wymaganego pakietu: xts
## Ładowanie wymaganego pakietu: zoo
## 
## Dołączanie pakietu: 'zoo'
## Następujące obiekty zostały zakryte z 'package:base':
## 
##     as.Date, as.Date.numeric
## Ładowanie wymaganego pakietu: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(PerformanceAnalytics)
## 
## Dołączanie pakietu: 'PerformanceAnalytics'
## Następujący obiekt został zakryty z 'package:graphics':
## 
##     legend
library(dygraphs)
library(corrplot)
## corrplot 0.95 loaded

Let us create a function

## funciton
monthly_returns <- function(ticker, base_year)
{
  # Obtain stock price data from Yahoo! Finance
  stock <- getSymbols(ticker, src = "yahoo", auto.assign = FALSE) 
  # Remove missing values
  stock <- na.omit(stock)
  # Keep only adjusted closing stock prices
  stock <- stock[, 6]
  
  # Confine our observations to begin at the base year and end at the last available trading day
  horizon <- paste0(as.character(base_year), "/", as.character(Sys.Date()))
  stock <- stock[horizon]
  
  # Calculate monthly arithmetic returns
  data <- periodReturn(stock, period = "monthly", type = "arithmetic")
  
  # Assign to the global environment to be accessible
  assign(ticker, data, envir = .GlobalEnv)
}

Using our function and visualizing returns

Call our function for each stock

# Call our function for each stock
monthly_returns("SBUX", 2015)
monthly_returns("CCL", 2015)
monthly_returns("AAPL", 2015)

Get S&P 500 Data

# Get S&P 500 Data
monthly_returns("SPY", 2015)

Merge all the data and rename columns

# Merge all the data and rename columns
returns <- merge.xts(SBUX, CCL, AAPL, SPY)
colnames(returns) <- c("SBUX", "CCL", "AAPL", "SP500")

Produce interactive chart of stock returns

# Produce interactive chart of stock returns
dygraph(returns, main = "Starbucks vs. Carnival vs. Apple vs. S&P 500") %>%
  dyAxis("y", label = "Return", valueRange = c(-1,0.5)) %>%
  dyRangeSelector(dateWindow = c("2015-01-01", "2020-07-01")) %>%
  dyOptions(colors = RColorBrewer::brewer.pal(4, "Set2")) 

Analyzing portfolio composition

##Analyzing portfolio composition
corrplot::corrplot(cor(returns), method = "number")

Building our portfolio and assessing performance

Assign weights

# Assign weights
wts <- c(1/3, 1/3, 1/3)

Construct a portfolio using our returns object and weights

Only select first three columns to isolate our individual stock data

# Only select first three columns to isolate our individual stock data
portfolio_returns <- Return.portfolio(R = returns[,1:3], weights = wts, wealth.index = TRUE)

Then isolate our S&P 500 data

# Then isolate our S&P 500 data
benchmark_returns <- Return.portfolio(R = returns[,4], wealth.index = TRUE)

Merge the two

# Merge the two
comp <- merge.xts(portfolio_returns, benchmark_returns)
colnames(comp) <- c("Portfolio", "Benchmark")

Build an interactive graph to compare performance

# Build an interactive graph to compare performance
dygraph(comp, main = "Portfolio Performance vs. Benchmark") %>%
  dyAxis("y", label = "Amount ($)")