Creating a moment function for PortfolioAnalytics

Hey everyone,

this is my first topic creation in this community so I appologize for every misstake in advance.

For my master studies I want to create a minimum variance portfolio out of the stocks of the S&P 500 with the help of the package "PortfolioAnalytics". Generally everything works fine up to the point where I want to introduce a function which computes the mean and the covariance matrix for the optimization. Here is my code:


#insert stock prices
prices_total_SP                              <- read.xlsx(...)

#delete all stocks without a value for the whole time horizon
prices_total_SP                              <- prices_total_SP[apply(prices_total_SP,2,function(x) all(!]

#calculating Returns
returns_total_SP                             <- ROC(prices_total_SP,type = "discrete", na.pad = FALSE)

#create portfolio
portfolio_SP                                 <- portfolio.spec(colnames(prices_total_SP))

#introduce constraints for the portfolio
portfolio_SP                                 <- add.constraint(portfolio_SP, type = "weight_sum", min_sum = 0.99, 
                                                               max_sum = 1.01)
portfolio_SP                                 <- add.constraint(portfolio_SP, type = "box", min = -0.2, max = 0.3)

#introduce objectives for the portfolio
portfolio_SP                                 <- add.objective(portfolio_SP, type = "risk", name = "StdDev")

#set up moment function for portfolio optimization with shrinked covariance matrix
port_moments_shrink                          <- function(rets){
                                                               moments <- list()
                                                               moments$mu <- meanEstimation(returns_total_SP)
                                                               moments$sigma <- covEstimation(returns_total_SP, 
                                                                                              control = list(type = "lw"))

#optimize the portfolio every quarter with a training period of two years
opt_portfolios_shrink                       <- optimize.portfolio.rebalancing(returns_total_SP,
                                                                               optimize_method = "ROI",
                                                                               momentFUN = "port_moments_shrink",
                                                                               trace = TRUE,
                                                                               rebalance_on = "quarters",
                                                                               training_period = 500)

#weights of every optimization
weights_shrink                               <- extractWeights(opt_portfolios_shrink)

The problem is that with this method the weights for all optimizations are the same. They all equal the weights of the last optimization if I use the default method of moments calculation.

If somebody could help me it would be really cool and I thank you in advance.

I found the solution by myself. I just have to name the returns R and the portfolio portfolio. And set them as arguements for the function then it works fine.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.