There are three simple steps in stockPortfolio to identifying an optimal portfolio
stockData <- getReturns(vectoroftickers,start=‘2016-03-15’,end=‘2017-03-15’)
model1 <- stockModel(stockData) model2 <- stockModel(storkData, model=‘CCM’)
optPort <- optimalPort(model1)
see https://cran.r-project.org/web/packages/stockPortfolio/stockPortfolio.pdf.
library(stockPortfolio)
ticker <- c('C','BAC','JPM','WFC')
gR <- getReturns(ticker, start='2016-03-15',end='2017-03-15')
byDay <- getReturns(ticker,'d',start='2016-03-15')
byWk <- getReturns(ticker,'w',start='2016-03-15')
byMth <- getReturns(ticker,'m',start='2016-03-15')
getReturns(ticker,freq=c("month","week","day"),get=c("overlapOnly","all"),start ="2016-03-15",end=NULL)
## Time Scale: month
##
## Average Return
## C BAC JPM WFC
## 0.03365373 0.05497465 0.03934434 0.01896777
theData <- getReturns(ticker, 'w',start='2016-03-15',end='2017-03-15')
plot(theData)
pairs(theData)
By randomly construct many portfolios, a general view of the Risk-Return possibilities becomes apparent.
data(stock94)
sm <- stockModel(stock94, model='SIM', index=25)
portCloud(sm, add=FALSE)
portPossCurve(sm, 2.5, add=TRUE)
The top half of this curve yields the best possible investments: The efficicient forntier
data(stock94Info)
ind=stock94Info$industry[1:4]
sim <- stockModel(theData, model='SIM',index=4,industry=ind, shortSelling='y')
opSim <- optimalPort(sim)
tpSim <- testPort(theData, opSim)
## Warning in testPort(theData, opSim): Allocation X was standardized
plot(opSim)
portPossCurve(sim,10,add=TRUE)
opSim
## Model: single index model
## Expected return: 0.01117691
## Risk estimate: 0.03191488
##
## Portfolio allocation:
## C BAC JPM
## -0.0485952 0.5712331 0.4773621
plot(tpSim)