Here I have collected data of 5 different stocks from Rstdio using ‘getsymbol.yahoo’ function. I had to call different library such as ‘quantmod’ and ‘tidyverse’ in to get these data from Rstodio. The data is from 2009-0101 to 2018-12-31. Then data bwas exported to my computer through write.csv funtion in order create the data that is needed for my porfolio making.
library(quantmod)
library(tidyverse)
start<- '2009-01-01'
end<-'2018-12-31'
NVDA<- getSymbols.yahoo('NVDA', from = start, to=end, auto.assign=F)[,4]
MSFT<- getSymbols.yahoo('MSFT', from = start, to=end, auto.assign=F)[,4]
GGLE<- getSymbols.yahoo('GOOG', from = start, to=end, auto.assign=F)[,4]
JPM<- getSymbols.yahoo('JPM', from = start, to=end, auto.assign=F)[,4]
AMZN<- getSymbols.yahoo('AMZN', from = start, to=end, auto.assign=F)[,4]
write.csv(AMZN, file = "Amazon.csv")
write.csv(JPM, file = "Jpm.csv")
write.csv(GGLE, file = "Goog.csv")
write.csv(MSFT, file = "Msft.csv")
write.csv(NVDA, file = "Nvda.csv")
the data consist only the closing price of each stocks only. I ahve used the closing price as it shows the return from the priovious day. This data will help to understand in which I should put focus on while diving the weights in to get the maximum output from my investment.
n<-mean(yearlyReturn(NVDA))
ms<-mean(yearlyReturn(MSFT))
g<-mean(yearlyReturn(GGLE))
j<-mean(yearlyReturn(JPM))
a<-mean(yearlyReturn(AMZN))
Hera asset were given according to each stock name and created a vector according to the return that was calculated through excel by using ‘avg.return’ function. The covariance matrix’s data was calculated by excel using data solver and using transpose paste function.
asset.names<- c("NVDA","MSFT","GGLE","JPM","AMZN")
mu.vec= c(0.0007, 0.0005, 0.0006,0.0002, 0.0011)
names(mu.vec)= asset.names
sigma.mat= matrix(c(0.0007,0.0002,0.0002,0.0002,0.0002,
0.0002,0.0002,0.0001,0.0002,0.0002,
0.0002,0.0001,0.0002,0.0002,0.0002,
0.0002,0.0002,0.0002,0.0005,0.0002,
0.0002,0.0002,0.0002,0.0002,0.0005),
nrow = 5,ncol = 5)
dimnames(sigma.mat)= list(asset.names,asset.names)
sigma.mat
## NVDA MSFT GGLE JPM AMZN
## NVDA 7e-04 2e-04 2e-04 2e-04 2e-04
## MSFT 2e-04 2e-04 1e-04 2e-04 2e-04
## GGLE 2e-04 1e-04 2e-04 2e-04 2e-04
## JPM 2e-04 2e-04 2e-04 5e-04 2e-04
## AMZN 2e-04 2e-04 2e-04 2e-04 5e-04
this package called IntroComFinR is not included in R. That is why I used manual input to get the library.
install.packages("IntroCompFinR", repos="http://R-Forge.R-project.org" )
library(IntroCompFinR)
ew = rep(1,5)/5
equalWeighted.portfolio<- getPortfolio(er=mu.vec,cov.mat=sigma.mat,weights = ew)
class(equalWeighted.portfolio)
## [1] "portfolio"
ew
## [1] 0.2 0.2 0.2 0.2 0.2
names(equalWeighted.portfolio)
## [1] "call" "er" "sd" "weights"
plot(equalWeighted.portfolio, col="gray")
# Global Minimum Variance Calculation
min.var<- globalMin.portfolio(mu.vec, sigma.mat)
class(min.var)
## [1] "portfolio"
min.var
## Call:
## globalMin.portfolio(er = mu.vec, cov.mat = sigma.mat)
##
## Portfolio expected return: 0.0004647059
## Portfolio standard deviation: 0.01057188
## Portfolio weights:
## NVDA MSFT GGLE JPM AMZN
## -0.1765 0.8824 0.8824 -0.2941 -0.2941
plot(min.var,col= "black")
# Target Return
target.return<- mu.vec[1]
e.port.nvda<- efficient.portfolio(mu.vec, sigma.mat,target.return)
class(e.port.nvda)
## [1] "portfolio"
e.port.nvda
## Call:
## efficient.portfolio(er = mu.vec, cov.mat = sigma.mat, target.return = target.return)
##
## Portfolio expected return: 7e-04
## Portfolio standard deviation: 0.01210349
## Portfolio weights:
## NVDA MSFT GGLE JPM AMZN
## -0.1070 0.6827 0.8303 -0.4244 0.0185
r.f<- 0.0001
tang.port<- tangency.portfolio(mu.vec, sigma.mat,r.f)
class(tang.port)
## [1] "portfolio"
tang.port
## Call:
## tangency.portfolio(er = mu.vec, cov.mat = sigma.mat, risk.free = r.f)
##
## Portfolio expected return: 0.0009532258
## Portfolio standard deviation: 0.01617011
## Portfolio weights:
## NVDA MSFT GGLE JPM AMZN
## -0.0323 0.4677 0.7742 -0.5645 0.3548
ef= efficient.frontier(mu.vec, sigma.mat, alpha.min = 0.5, alpha.max = 2, nport = 30)
attributes(ef)
## $names
## [1] "call" "er" "sd" "weights"
##
## $class
## [1] "Markowitz"
ef
## Call:
## efficient.frontier(er = mu.vec, cov.mat = sigma.mat, nport = 30,
## alpha.min = 0.5, alpha.max = 2)
##
## Frontier portfolios' expected returns and standard deviations
## port 1 port 2 port 3 port 4 port 5 port 6 port 7 port 8 port 9 port 10
## ER 0.0008 0.0007 0.0007 0.0007 0.0007 0.0006 0.0006 0.0006 0.0005 0.0005
## SD 0.0132 0.0128 0.0123 0.0119 0.0116 0.0112 0.0110 0.0108 0.0107 0.0106
## port 11 port 12 port 13 port 14 port 15 port 16 port 17 port 18 port 19
## ER 0.0005 0.0004 0.0004 0.0004 0.0003 0.0003 0.0003 0.0002 0.0002
## SD 0.0106 0.0106 0.0107 0.0109 0.0112 0.0114 0.0118 0.0122 0.0126
## port 20 port 21 port 22 port 23 port 24 port 25 port 26 port 27 port 28
## ER 0.0002 0.0001 0.0001 0.0001 0.0000 0.0000 0.0000 -0.0001 -0.0001
## SD 0.0131 0.0136 0.0141 0.0147 0.0152 0.0158 0.0165 0.0171 0.0178
## port 29 port 30
## ER -0.0001 -0.0002
## SD 0.0184 0.0191
plot(ef)
# Ploting Efficient Frontier
plot(ef, plot.assets = T, col= 'blue', pch=15)
# Bining All the Stock Data
library(tidyquant)
cbind<-cbind(NVDA,MSFT,GGLE,JPM,AMZN)
msft_jpm<-MSFT/JPM
head(msft_jpm)
## MSFT.Close
## 2009-01-02 0.6484848
## 2009-01-05 0.7015385
## 2009-01-06 0.6947791
## 2009-01-07 0.6945532
## 2009-01-08 0.7391624
## 2009-01-09 0.7516365
plot.zoo(msft_jpm)
# Comparing Google stock with JP Morgan
goog_jpm<-GGLE/JPM
head(goog_jpm)
## GOOG.Close
## 2009-01-02 0.2552792
## 2009-01-05 0.2793376
## 2009-01-06 0.2784576
## 2009-01-07 0.2855176
## 2009-01-08 0.2975529
## 2009-01-09 0.3021693
plot.xts(goog_jpm)
nvda_amzn<-NVDA/AMZN
head(nvda_amzn)
## NVDA.Close
## 2009-01-02 0.8011406
## 2009-01-05 0.8203847
## 2009-01-06 0.7993375
## 2009-01-07 0.7669039
## 2009-01-08 0.7347795
## 2009-01-09 0.7142857
plot.ts(nvda_amzn)
# Binding All the return
library(PerformanceAnalytics)
nvda_ret<-Return.calculate(NVDA)[(-1),]
msft_ret<-Return.calculate(MSFT)[(-1),]
goog_ret<-Return.calculate(GGLE)[(-1),]
jpm_ret<-Return.calculate(JPM)[(-1),]
amzn_ret<-Return.calculate(AMZN)[(-1),]
rets<- head(cbind(nvda_ret,msft_ret,goog_ret,jpm_ret,amzn_ret))
rets
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 2009-01-05 0.01836967 0.009345821 0.020944720 -0.06698566 -0.005518715
## 2009-01-06 0.03382189 0.011695895 0.018320471 0.02153843 0.061043270
## 2009-01-07 -0.05997821 -0.060211945 -0.036071372 -0.05990626 -0.020223182
## 2009-01-08 -0.02552207 0.031266048 0.009875410 -0.03097191 0.017081885
## 2009-01-09 -0.05595236 -0.029821091 -0.031120262 -0.04592212 -0.028866333
## 2009-01-12 -0.04035305 -0.002561534 -0.007553903 -0.04081631 -0.064673068
eq_weights<- c(0.2,0.2,0.2,0.2,0.2)
pf_bh<- Return.portfolio(R=rets, weights = eq_weights, verbose = T)
pf_rebal<- Return.portfolio(R=rets, weights = eq_weights,rebalance_on = "months", verbose = T)
par(mfrow=c(2,1), mar=c(2,4,2,2))
plot.zoo(pf_bh$returns)
plot.zoo(pf_rebal$returns)
eop_weight_bh<- pf_bh$EOP.Weight
eop_weight_reb<- pf_rebal$EOP.Weight
par(mfrow=c(2,1),mar=c(2,4,2,2))
plot.zoo(eop_weight_bh)
plot.zoo(eop_weight_reb)
# Analysing S&P 500 Data
snp500<-getSymbols.yahoo("^GSPC", from=start, to=end,auto.assign=F )
sp_monthly<- to.monthly(snp500)[,(4)]
head(sp_monthly)
## snp500.Close
## Jan 2009 825.88
## Feb 2009 735.09
## Mar 2009 797.87
## Apr 2009 872.81
## May 2009 919.14
## Jun 2009 919.32
sp_monthly
## snp500.Close
## Jan 2009 825.88
## Feb 2009 735.09
## Mar 2009 797.87
## Apr 2009 872.81
## May 2009 919.14
## Jun 2009 919.32
## Jul 2009 987.48
## Aug 2009 1020.62
## Sep 2009 1057.08
## Oct 2009 1036.19
## ...
## Mar 2018 2640.87
## Apr 2018 2648.05
## May 2018 2705.27
## Jun 2018 2718.37
## Jul 2018 2816.29
## Aug 2018 2901.52
## Sep 2018 2913.98
## Oct 2018 2711.74
## Nov 2018 2760.17
## Dec 2018 2485.74
sp_500_returns<- Return.calculate(sp_monthly)[(-1),]
plot.ts(sp_500_returns)
# Calender return Calculation
x<-table.CalendarReturns(sp_500_returns)
mean(sp_500_returns)
## [1] 0.01004424
mean.geometric(sp_500_returns)
## snp500.Close
## Geometric Mean 0.009302465
sd(sp_500_returns)
## [1] 0.03862305
table.AnnualizedReturns(sp_500_returns)
## snp500.Close
## Annualized Return 0.1175
## Annualized Std Dev 0.1338
## Annualized Sharpe (Rf=0%) 0.8784
chart.RollingPerformance(R=sp_500_returns, width = 12,FUN = "Return.annualized")
chart.RollingPerformance(R=sp_500_returns, width = 12,FUN = "StdDev.annualized")
# Rolling performance Sharp ratio
chart.RollingPerformance(R=sp_500_returns, width = 12,FUN = "SharpeRatio.annualized")
par(mfrow=c(1,1))
chart.RollingPerformance(R=sp_500_returns, width = 12)
# Daily Return of S&P 500
sp500_ret_daily <- Return.calculate(Cl(snp500))
skewness(sp500_ret_daily)
## [1] -0.2190269
kurtosis(sp500_ret_daily)
## [1] 5.00786
SemiDeviation(sp_500_returns)
## snp500.Close
## Semi-Deviation 0.02874935
sd(sp_500_returns)
## [1] 0.03862305
VaR(sp_500_returns, p=0.05)
## snp500.Close
## VaR -0.05704423
ES(sp_500_returns,p=0.05)
## snp500.Close
## ES -0.08228496
table.Drawdowns(sp500_ret_daily)
## From Trough To Depth Length To Trough Recovery
## 1 2009-01-07 2009-03-09 2009-06-01 -0.2762 100 42 58
## 2 2018-09-21 2018-12-24 <NA> -0.1978 69 65 NA
## 3 2011-05-02 2011-10-03 2012-02-24 -0.1939 207 108 99
## 4 2010-04-26 2010-07-02 2010-11-04 -0.1599 136 49 87
## 5 2015-05-22 2016-02-11 2016-07-11 -0.1416 286 183 103
par(mfrow=c(1,1))
chart.Drawdown(sp500_ret_daily)
# Scatterplot of NVDIA and amazon
chart.Scatter(nvda_ret,amzn_ret, xlab = "nvda return", ylab="amzn return", main="nvda x amzn")
# Correlation between 5 stocks
chart.Correlation(cbind)
# Correlation through 252 days of Microsoft and JPM return
chart.RollingCorrelation(msft_ret,jpm_ret,width = 252)
means<- apply(rets,2,"mean")
means
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## -0.021602355 -0.006714468 -0.004267489 -0.037177302 -0.006859357
sds<- apply(rets,2,"sd")
sds
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 0.03921528 0.03302283 0.02485660 0.03156233 0.04287440
x<-plot(x= sds,y= means)
# Daigonal covariance
diag_cov<- diag(sds^2)
diag_cov
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.001537838 0.000000000 0.0000000000 0.0000000000 0.000000000
## [2,] 0.000000000 0.001090507 0.0000000000 0.0000000000 0.000000000
## [3,] 0.000000000 0.000000000 0.0006178504 0.0000000000 0.000000000
## [4,] 0.000000000 0.000000000 0.0000000000 0.0009961804 0.000000000
## [5,] 0.000000000 0.000000000 0.0000000000 0.0000000000 0.001838214
cov_matrix<- cov(rets)
cov_matrix
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## NVDA.Close 0.0015378384 0.0008564647 0.0008817568 0.0006558626 0.0012152065
## MSFT.Close 0.0008564647 0.0010905074 0.0007332357 0.0004481648 0.0006328570
## GOOG.Close 0.0008817568 0.0007332357 0.0006178504 0.0003264295 0.0006224301
## JPM.Close 0.0006558626 0.0004481648 0.0003264295 0.0009961804 0.0009604265
## AMZN.Close 0.0012152065 0.0006328570 0.0006224301 0.0009604265 0.0018382142
cor_matrix<- cor(rets)
cor_matrix
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## NVDA.Close 1.0000000 0.6613629 0.9045901 0.5298934 0.7227643
## MSFT.Close 0.6613629 1.0000000 0.8932801 0.4299861 0.4469853
## GOOG.Close 0.9045901 0.8932801 1.0000000 0.4160818 0.5840511
## JPM.Close 0.5298934 0.4299861 0.4160818 1.0000000 0.7097364
## AMZN.Close 0.7227643 0.4469853 0.5840511 0.7097364 1.0000000
I have put weights according to the return making capability of each stock
weights<- c(0.3,0.1,0.1,0.2,0.3)
vol_budget<- StdDev(rets, portfolio_method = "component", weights = weights)
vol_budget
## $StdDev
## [1] 0.03160263
##
## $contribution
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 0.010735575 0.002274506 0.002062014 0.004819739 0.011710799
##
## $pct_contrib_StdDev
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 0.33970510 0.07197204 0.06524816 0.15251069 0.37056402
weights_percrisk<- cbind(weights, vol_budget$pct_contrib_StdDev)
colnames(weights_percrisk)<- c("weights", "perc vol contrib")
weights_percrisk
## weights perc vol contrib
## NVDA.Close 0.3 0.33970510
## MSFT.Close 0.1 0.07197204
## GOOG.Close 0.1 0.06524816
## JPM.Close 0.2 0.15251069
## AMZN.Close 0.3 0.37056402
library(PortfolioAnalytics)
data(rets)
index_returns<- rets
index_returns<- xts:::.update_index_attributes(index_returns)
library(ROI)
library(ROI.plugin.quadprog)
library(ROI.plugin.glpk)
port_spec<- portfolio.spec(colnames(index_returns))
port_spec <- add.constraint(portfolio = port_spec, type = "full_investment")
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
port_spec <- add.objective(portfolio = port_spec, type = "risk", name = "StdDev")
opt <- optimize.portfolio(index_returns, portfolio = port_spec, optimize_method = "ROI")
opt
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
##
## Call:
## optimize.portfolio(R = index_returns, portfolio = port_spec,
## optimize_method = "ROI")
##
## Optimal Weights:
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 0.0000 0.0000 0.6968 0.3032 0.0000
##
## Objective Measure:
## StdDev
## 0.02301
extractWeights(opt)
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 2.672949e-17 -4.037369e-17 6.968067e-01 3.031933e-01 0.000000e+00
chart.Weights(opt)
port_spec <- portfolio.spec(assets = colnames(index_returns))
port_spec <- add.constraint(portfolio = port_spec, type = "full_investment")
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
port_spec <- add.objective(portfolio = port_spec, type = "return", name = "mean")
port_spec <- add.objective(portfolio = port_spec, type = "risk", name = "var", risk_aversion = 10)
opt1 <- optimize.portfolio(R = index_returns, portfolio = port_spec, optimize_method = "ROI")
extractWeights(opt1)
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 3.632312e-16 0.000000e+00 1.000000e+00 9.244464e-32 -4.649059e-16
chart.Weights(opt1)
extractWeights(opt)
## NVDA.Close MSFT.Close GOOG.Close JPM.Close AMZN.Close
## 2.672949e-17 -4.037369e-17 6.968067e-01 3.031933e-01 0.000000e+00
chart.Weights(opt)
port_spec <- portfolio.spec(colnames(index_returns))
port_spec <- add.constraint(portfolio = port_spec, type = "full_investment")
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
port_spec <- add.constraint(portfolio = port_spec, type = "box", min = c(0.1, 0.1, 0.1, 0.1,0.1), max = 0.4)
port_spec <- add.objective(portfolio = port_spec, type = "return", name = "mean")
port_spec <- add.objective(portfolio = port_spec, type = "risk", name = "StdDev")
port_spec <- add.objective(portfolio = port_spec, type = "risk_budget", name = "StdDev", min_prisk = 0.05, max_prisk = 0.1)
opt <- optimize.portfolio.rebalancing(index_returns, portfolio = port_spec,
optimize_method ="random",trace = TRUE,
search_size = 1000,
rebalance_on = "quarters")
optweight<- extractWeights(opt)
chart.Weights(opt)
ex.ob.msr<- extractObjectiveMeasures(opt)
ex.ob.msr
## mean StdDev StdDev.contribution.NVDA.Close
## 2009-01-12 -0.01219761 0.0269134 0.004801788
## StdDev.contribution.MSFT.Close StdDev.contribution.GOOG.Close
## 2009-01-12 0.00641256 0.009416219
## StdDev.contribution.JPM.Close StdDev.contribution.AMZN.Close
## 2009-01-12 0.003031285 0.00325155
## StdDev.pct_contrib_StdDev.NVDA.Close
## 2009-01-12 0.1784163
## StdDev.pct_contrib_StdDev.MSFT.Close
## 2009-01-12 0.2382664
## StdDev.pct_contrib_StdDev.GOOG.Close
## 2009-01-12 0.349871
## StdDev.pct_contrib_StdDev.JPM.Close
## 2009-01-12 0.1126311
## StdDev.pct_contrib_StdDev.AMZN.Close
## 2009-01-12 0.1208153