使用ROI优化投资组合

时间:2019-09-26 17:06:28

标签: r nonlinear-optimization quantitative-finance r-portfolioanalytics

我想使用ROI求解器来优化股票组合。但是,当我尝试优化投资组合时,会出现一个错误,说初始权重中的资产数量小于收益。我留下了代码和错误。

library(quantmod)
library(PortfolioAnalytics)
library(PerformanceAnalytics)
library(ROI)

#vector of stocks in my portfolio  of 
tickers <- c("GS", "AAPL", "AMZN", "GM", "CBRL", "NVDA", "AMAT", "NKE","CSCO","SBUX","TLT")
#bind porfolio prices 
portfolioPrices <- NULL
for(ticker in tickers) {
  portfolioPrices <- cbind(portfolioPrices,
                           getSymbols.yahoo(ticker,  periodicity = 'daily', auto.assign=FALSE)[,4])
}


#portfolio returns
portfolioReturns <- na.omit(ROC(portfolioPrices))
print(portfolioReturns)
portf <- portfolio.spec(colnames(portfolioReturns.2))
portf <- add.constraint(portf, type="weight_sum", min_sum=.99, max_sum=1.01)
portf <- add.constraint(portf, type="box", min=.02, max=.60) 
portf<-add.constraint(portf,type="transation_cost", ptc=.001)
portf <- add.objective(portfolio = portf, type="return", name="mean")
portf <- add.objective(portfolio = portf, type="risk", name="StdDev")
portf<-add.objective(portfolio = p, type="risk", name="ES", 
                     arguments=list(p=0.925, clean="boudt"))


#optimize portfolio using the "DEoptim solver"
optPort <- optimize.portfolio(portfolioReturns, portf, optimize_method = "ROI", trace=TRUE)
extractWeights(optPort)

这是我得到的错误:

  

警告信息:   在Return.portfolio(portfolioReturns,权重= extractWeights(opt_rebal))中:     Begining_weights中的资产数量小于退货中的列数,因此子集退货。

0 个答案:

没有答案