我正在对一系列股票进行投资组合优化,并试图提取重新平衡的投资组合的权重。
我遇到的问题:我没有得到重新平衡的投资组合的权重,而是得到 3 个日期。该项目的代码在下面。
library(ROI)
install.packages("DEoptim")
library(ggplot2)
install.packages("quantmod")
library(quantmod)
library(quantmod)
install.packages("PerfomanceAnalytics")
library(PerformanceAnalytics)
library(PortfolioAnalytics)
library(random)
install.packages("random")
library(random)
library(DEoptim)
install.packages("fPortfolio")
library(fPortfolio)
install.packages("foreach")
install.packages("doParallel")
library(PortfolioAnalytics)
#vector of stocks in my portfolio of
tickers <- c("FB", "AAPL", "AMZN", "GM", "GOOGL", "SQ", "NVDA","RYAM", "AMAT", "IMMR","SOI","PETS")
#bind porfolio prices
portfolioPrices <- NULL
for(ticker in tickers) {
portfolioPrices <- cbind(portfolioPrices,
getSymbols.yahoo(ticker, from='2003-01-03', periodicity = 'daily', auto.assign=FALSE)[,4])
}
#portfolio returns
portfolioReturns <- na.omit(ROC(portfolioPrices))
print(portfolioReturns)
portf <- portfolio.spec(colnames(portfolioReturns))
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(portf, type="return", name="mean")
portf <- add.objective(portf, type="risk", name="StdDev",target=.005)
rp<-random_portfolios(portf, 10000, "sample")
#optimize portfolio using the "DEoptim solver"
optPort <- optimize.portfolio(portfolioReturns, portf, optimize_method = "DEoptim", trace=TRUE)
#chart weights of optimized portfolio
chart.Weights(optPort)
summary(optPort)
chart.RiskReward(optPort, risk.col = "StDev", return.col = "mean", chart.assets = TRUE)
rp<-random_portfolios(portf, 10000, "sample")
#rebalance portfolo
opt_rebal <- optimize.portfolio.rebalancing(portfolioReturns,
portf,
optimize_method="ROI",
rp=rp,
rebalance_on="years",
training_period=60,
rolling_window=60)
extractWeights(optPort)
chart.Weights(optPort)
#extract weights of rebalanced portfolio
extractWeights(opt_rebal))
我怎样才能解决这个问题?
对你的帮助表示感谢。
谢谢你。