require(IKTrading)
require(quantmod)
require(PerformanceAnalytics)
options("getSymbols.warning4.0"=FALSE)
symbols <- c("NSA", #National Storage
"EXR", #Extra Space Storage
"PSA", # Public Storage
"SELF", #Global Self Storage
"CUBE", #Cube Smart
"LSI" #Life Storage
)
from="2003-01-01"
#Home ETFs first, iShares ETFs afterwards
if(!"XLB" %in% ls()) {
suppressMessages(getSymbols(symbols, from="2003-01-01", src="yahoo", adjust=TRUE))
}
Self <- list()
for(i in 1:length(symbols)) {
Self[[i]] <- Cl(get(symbols[i]))
}
Self <- do.call(cbind, Self)
colnames(Self) <- gsub("\\.[A-z]*", "", colnames(Self))
cor(Self)
Storage<-Self/lag(Self)-1
Storage[1,] <- 0
write.table(Storage, file='Storage.xls')
tail(Storage)
Home_2020=window(Storage,start=as.Date("2020-01-01"), end=as.Date("2020-12-31"))
Home_2021=window(Storage,start=as.Date("2021-01-01"), end=as.Date("2021-12-31"))
charts.PerformanceSummary(Home_2021,main='Self Storage REITs Sectors',wealth.index = TRUE)
#calculate the Sharpe ratio
# Sharpe ratio = ((Expected_Return - Risk Free Return) / SD)
Home_Returns<-table.AnnualizedReturns(Home_2021, scale=252, Rf=0.005/252)
Home_Returns
Cumm_Returns=Return.cumulative(Home_2021)*100
Cumm_Returns
chart.CumReturns(Home_2021, main='Home for 2021', begin=c("first", "axis"))
barplot(Cumm_Returns, main='Cummulative Returns for Self REITs')
barplot(Cumm_Returns)
write.csv(Home_Returns, file='return.xls')
cor.distance <- cor(Storage)
corrplot::corrplot(cor.distance)
write.table(cor.distance, file='correlation matrix.xls')
library(igraph)
g1 <- graph.adjacency(cor.distance, weighted = T, mode = "undirected", add.colnames = "label")
mst <- minimum.spanning.tree(g1)
plot(mst)
library(visNetwork)
mst_df <- get.data.frame( mst, what = "both" )
visNetwork(
data.frame(
id = 1:nrow(mst_df$vertices)
,label = mst_df$vertices
)
, mst_df$edges
) %>%
visOptions( highlightNearest = TRUE)
No comments:
Post a Comment