-
Notifications
You must be signed in to change notification settings - Fork 31
/
port_create_sample.R
75 lines (65 loc) · 3.85 KB
/
port_create_sample.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Ref video: http://youtu.be/O33dF532pRo
# R script to read prices from Yahoo and construct a portfolio
# updated 08/19/2012
# get libraries of routines – these packages need to be installed
library(timeSeries)
library(fPortfolio) # may also require installing the package require(slam)
library(quantmod)
library(caTools)
# create list of stock tickers – replace the tickers here with those you want to use in your portfolio
TickerList <- c("DELL", "GOOG","CSCO", "MSFT", "JNPR")
# read closing prices from Yahoo keeping only the closing prices
ClosingPricesRead <- NULL
for (Ticker in TickerList)
ClosingPricesRead <- cbind(ClosingPricesRead,
getSymbols.yahoo(Ticker, from="1950-01-01", verbose=FALSE, auto.assign=FALSE)[,6]) # [,6] = keep the adjusted prices
# keep only the dates that have closing prices for all tickers
ClosingPrices <- ClosingPricesRead[apply(ClosingPricesRead,1,function(x) all(!is.na(x))),]
# convert prices to daily returns
returns <- as.timeSeries(tail(ClosingPrices,-1) / as.numeric(head(ClosingPrices,-1)) - 1)
# calculate the efficient frontier
Frontier <- portfolioFrontier(returns)
# plot frontier
plot(Frontier,1) # can also call the plot routine so it only plots the frontier: plot(Frontier,1)
###########################################################################################
####### addtional code to get a better look at the portfolios – annualize the returns and risk
# get the means and covariance matrix of the price returns
getStatistics(Frontier)$mean # data input into the efficient frontier calculator
cor(returns)
# execute the next commands to plot annualized returns and risk
# convert from daily to annual returns and risk for points on the efficient frontier
# plot efficient frontier using annualized return and risk
riskReturnPoints <- frontierPoints(Frontier) # get risk and return values for points on the efficient frontier
annualizedPoints <- data.frame(targetRisk=riskReturnPoints[, "targetRisk"] * sqrt(252),
targetReturn=riskReturnPoints[,"targetReturn"] * 252)
plot(annualizedPoints)
# plot Sharpe ratios for each point on the efficient frontier
riskFreeRate <- 0
plot((annualizedPoints[,"targetReturn"] – riskFreeRate) / annualizedPoints[,"targetRisk"], xlab="point on efficient frontier", ylab="Sharpe ratio")
# plot the allocation to each stock for each point on the efficient frontier
# weightsPlot(Frontier)
allocations <- getWeights(Frontier@portfolio) # get allocations for each instrument for each point on the efficient frontier
colnames(allocations) <- TickerList
barplot(t(allocations), col=rainbow(ncol(allocations)+2), legend=colnames(allocations))
allocations
############################################################################################
# examine the efficient frontier for portfolios with different constraints
constraints <- "minW[1:length(TickerList)]=-1"
Frontier <- portfolioFrontier(returns, constraints = constraints)
Frontier.LongOnly <- portfolioFrontier(returns)
riskReturnPoints <- frontierPoints(Frontier)
annualizedPoints <- data.frame(targetRisk=riskReturnPoints[, "targetRisk"] * sqrt(252),
targetReturn=riskReturnPoints[,"targetReturn"] * 252)
riskReturnPoints.LongOnly <- frontierPoints(Frontier.LongOnly)
annualizedPoints.LongOnly <- data.frame(targetRisk=riskReturnPoints.LongOnly[, "targetRisk"] * sqrt(252),
targetReturn=riskReturnPoints.LongOnly[,"targetReturn"] * 252)
xlimit <- range(annualizedPoints[,1], annualizedPoints.LongOnly[,1])
ylimit <- range(annualizedPoints[,2], annualizedPoints.LongOnly[,2])
plot(annualizedPoints.LongOnly, xlim=xlimit, ylim=ylimit, pch=16, col="blue")
points(annualizedPoints, col="red", pch=16)
legend("right", legend=c("long only","constrained"), col=c("blue","red"), pch=16)
######
# other constraints
constraints <- c("minW[1:length(TickerList)]=.10","maxW[1:length(TickerList)]=.60")
# write data to csv file to import into excel
write.csv(allocations, "allocations.csv")