knitr::opts_chunk$set(warning = FALSE)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
pricedata<-read.csv("F:/R Files/Stock Returns.csv")
ticker<- c("EBL","ORIONPHARM","BSRM","HEDLBCEM")
weights <- c(.25, .25, .25, .25)

stockprice <- pricedata[,c("EBL","ORIONPHARM","BSRM","HEDLBCEM")]
rownames(stockprice) <- pricedata$date
  
indexprice1 <- pricedata[,c("date","INDEX")]
rownames(indexprice1) <- pricedata$date
  
#Converting a dataframe into xts time series file  
indexprice1$date <- as.Date(indexprice1$date)
indexprice <- xts(indexprice1$INDEX,indexprice1$date)

#Portfolio Performance

library(TTR)
library("PerformanceAnalytics")#Calculate Returns For DF
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked _by_ '.GlobalEnv':
## 
##     weights
## The following object is masked from 'package:graphics':
## 
##     legend
dailyReturns <- na.omit(ROC(stockprice, type="discrete"))

#Return for Index
benchmarkReturns <- na.omit(ROC(indexprice, type="discrete"))

portfolioReturn<-Return.portfolio(dailyReturns,weights=weights)#CalculatePrtflioRet

chart.CumReturns(portfolioReturn)

charts.PerformanceSummary(portfolioReturn)

CAPM.beta(portfolioReturn, benchmarkReturns, 0.0002365)
## [1] NA
#Calculate Beta and Jensen's Alpha 
rfrate <- as.numeric(0.0002365) # 10 year Tbill adjusted for daily frequency
#CAPM.alpha(portfolioReturn, benchmarkReturns, .035/252)
CAPM.jensenAlpha(portfolioReturn, benchmarkReturns, rfrate)
## [1] 0.002107396
SharpeRatio(portfolioReturn, Rf = rfrate, p = 0.95, FUN = "StdDev",
            weights = NULL, annualize = FALSE)
##                               portfolio.returns
## StdDev Sharpe (Rf=0%, p=95%):      0.0003786345

Correlation matrix

# Correlation matrix
library(ggcorrplot)
## Loading required package: ggplot2
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data(stockprice)
corr <- round(cor(stockprice), 1)

# Plot
a <- ggcorrplot(corr, hc.order = TRUE, 
           type = "full", 
           lab = TRUE, 
           lab_size = 4, 
           method="circle", 
           colors = c("tomato2", "white", "springgreen3","black"),
           outline.color = "gray",
           title="Stock Correlation Matrix", 
           ggtheme=theme_bw)
ggplotly(a)

#Efficient Frontier Plot

#Efficient Frontier
library(ggcorrplot)
library(ggplot2)
library(plotly)
front <- read.csv("efront.csv")


b <-ggplot(front, aes(x=dev, y=ret)) + 
  geom_point(
    color="#0063B2FF",
    fill="#0063B2FF",
    shape=22,
    alpha=0.1,
    size=1,
    stroke = 2
  )
ggplotly(b)

#Stock Price Animations

library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(gganimate)
animation <- read.csv("stockanimation.csv")

animation %>%
  ggplot( aes(x=year, y=price, group=stock, color=stock)) +
  geom_line() +
  geom_point(shape=21, color="black", fill="#69b3a2", size=3) +
  #scale_color_viridis(discrete = TRUE) +
  ggtitle("Stock Price By Year") +
  theme_ipsum() +
  ylab("Stocks") +
  transition_reveal(year)
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?