0

我想使用 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, weights = extractWeights(opt_rebal)) 中: begin_weights 中的资产数量少于回报中的列数,因此子集回报。

4

0 回答 0