0% found this document useful (0 votes)
167 views3 pages

Portfolio Analysis

The document analyzes a portfolio of stocks over 252 days. It retrieves stock price data for 9 companies from Yahoo Finance and calculates the monthly return for each stock. It then calculates the fraction of each stock, expectation, and risk of the portfolio. It plots the risk and return of the individual stocks and portfolio on an efficient frontier graph.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views3 pages

Portfolio Analysis

The document analyzes a portfolio of stocks over 252 days. It retrieves stock price data for 9 companies from Yahoo Finance and calculates the monthly return for each stock. It then calculates the fraction of each stock, expectation, and risk of the portfolio. It plots the risk and return of the individual stocks and portfolio on an efficient frontier graph.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Portfolio Analysis

YIK LUN, KEI


[email protected]
library("quantmod")
##
##
##
##
##
##
##
##
##
##
##

Loading required package: xts


Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.

symbols <-c("BABA","IBM","VIPS","GOOG","MSFT","GE","WFC","AMZN","JD")
days<-252
stock <- list();daily.return<-list()
fraction<-matrix(NA,nrow=length(symbols),ncol=1)
for (i in 1:length(symbols)) {
stock[[i]] <- tail(getSymbols(symbols[i],src="yahoo",return.class="xts",auto.assign=FALSE),days)
daily.return[[i]] <- periodReturn(stock[[i]],period='monthly')
}
##
##
##
##
##
##
##
##
##

As of 0.4-0, 'getSymbols' uses env=parent.frame() and


auto.assign=TRUE by default.
This behavior will be phased out in 0.5-0 when the call will
default to use auto.assign=FALSE. getOption("getSymbols.env") and
getOptions("getSymbols.auto.assign") are now checked for alternate defaults
This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.

daily.return<-as.data.frame(daily.return)
colnames(daily.return)<-symbols
for (i in 1:length(symbols)) {
fraction[i,1]<-sum(solve(cov(daily.return))[i,])/sum(solve(cov(daily.return)))
}
rownames(fraction)<-symbols
expectation<-colMeans(daily.return)%*%fraction
risk<-sd(as.matrix(daily.return) %*% fraction)

fraction
##
##
##
##
##
##
##
##
##
##

BABA
IBM
VIPS
GOOG
MSFT
GE
WFC
AMZN
JD

[,1]
0.022436456
1.009465335
-0.007964852
0.428176360
-0.537134017
0.653694881
-0.288443107
-0.144980627
-0.135250429

expectation
##
[,1]
## [1,] -0.01249279
risk
## [1] 0.02195758
plot(risk,expectation,col="red",pch=19,xlab="Risk",ylab="Return",
ylim=c(min(colMeans(daily.return),expectation),max(colMeans(daily.return),expectation)),
xlim=c(min(sqrt(diag(var(daily.return))),risk),max(sqrt(diag(var(daily.return))),risk)))
abline(h=0,col="red")
for (i in 1:length(symbols)){
points(sd(daily.return[,i]),mean(daily.return[,i]),col="blue",pch=17)
text(sd(daily.return[,i]),mean(daily.return[,i]),colnames(daily.return)[i])
}

0.02

GOOG

GE

MSFT
JD

0.00

WFC

VIPS

IBM
0.02

Return

0.04

0.06

AMZN

BABA
0.02

0.04

0.06

0.08
Risk

0.10

0.12

0.14

You might also like