install.packages(“quantmod”) install.packages(“PerformanceAnalytics”) install.packages(“dygraphs”) install.packages(“corrplot”)
#install.packages("quantmod")
#install.packages("PerformanceAnalytics")
#install.packages("dygraphs")
#install.packages("corrplot")
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)
}
# Call our function for each stock
monthly_returns("SBUX", 2015)
monthly_returns("CCL", 2015)
monthly_returns("AAPL", 2015)
# Get S&P 500 Data
monthly_returns("SPY", 2015)
# 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
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"))
round(tail(returns, n = 5), 4)
## SBUX CCL AAPL SP500
## 2024-07-31 0.0013 -0.1100 0.0544 0.0121
## 2024-08-30 0.2205 -0.0096 0.0324 0.0234
## 2024-09-30 0.0309 0.1200 0.0175 0.0210
## 2024-10-31 0.0022 0.1905 -0.0304 -0.0089
## 2024-11-14 0.0157 0.1050 0.0113 0.0435
##Analyzing portfolio composition
corrplot::corrplot(cor(returns), method = "number")
# Assign weights
wts <- c(1/3, 1/3, 1/3)
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
dygraph(comp, main = "Portfolio Performance vs. Benchmark") %>%
dyAxis("y", label = "Amount ($)")