Every single step shows AMZN until the melt is used (look at the previous post). I hope I did this correctly...
library("quantmod")
library("PerformanceAnalytics")
library("ggplot2")
library("reshape2")
maxDate <-"2000-01-01"
MSFT.prices<-Ad(getSymbols("MSFT",auto.assign = FALSE, from=maxDate))
MSFT.rets<-dailyReturn(MSFT.prices)
VaR(MSFT.rets,p=0.95,method = "historical")
VaR(MSFT.rets, p=0.99, method="historical")
ES(MSFT.rets, p=0.99, method = "historical")
tickers <-c("MSFT","AAPL","AMZN")
weights <- c(.50,.10,.40)
getSymbols(tickers,from=maxDate)
Port.prices <- na.omit(merge(Ad(MSFT), Ad(AAPL),Ad(AMZN)))
Port.returns <- ROC(Port.prices, type = "discrete")[-1,]
colnames(Port.returns)<-tickers
Port.prices <- na.omit(merge(Ad(MSFT), Ad(AAPL),Ad(AMZN)))
Port.returns <- ROC(Port.prices, type = "discrete")[-1,]
colnames(Port.returns)<-tickers
VAR.Hist <- VaR(Port.returns, p=0.95, weights = NULL, portfolio_method = "single", method = "historical")
VAR.Gaus <- VaR(Port.returns, p=0.95, weights = NULL, portfolio_method = "single", method = "gaussian")
VAR.Mod <- VaR(Port.returns, p= 0.95, weights = NULL, portfolio_method = "single", method ="modified")
ALL.VARS <- data.frame(rbind(VAR.Hist, VAR.Gaus, VAR.MOD))
rownames(ALL.VARS)<-c("Hist","Gaus","Mod")
PortVAR.Hist <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "historical")$hVaR
PortVar.Gaus <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "gaussian")$VaR[1]
PortVar.Mod <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "modified")$MVaR[1]
ALL.VARS$Portfolio <- c(PortVAR.Hist,PortVar.Gaus,PortVar.Mod)
head(ALL.VARS$Portfolio)
ALL.VARS$Type <-c("Hist","Gaus","Mod")
head(ALL.VARS)
plotVar <-melt(ALL.VARS, variable.name = "Ticker", value.name = "VaR")
ggplot(plotVar, aes(x=Type,y=VaR, fill= Ticker))+geom_bar(stat="identity", position = "dodge")
`type or paste code here`