PortfolioAnalytics icon indicating copy to clipboard operation
PortfolioAnalytics copied to clipboard

Error in `[.data.frame`(R, ep.i) : undefined columns selected, when running `optimize.portfolio.rebalancing`

Open emjay032 opened this issue 4 years ago • 0 comments

I always get this error when trying to rebalance, but i dont understand why?

This is my portfolio:

smallcap_tickers <- c( "AAON",'BCPC', "CEVA", "JJSF", "UNF") 
midcap_tickers <-c("CTXS", "DRE", "NDAQ", "O", "SWKS") 
largecap_tickers <- c("MCD", "KO", "JPM", "WM", "AFL", "AAPL", "NVDA", "GOOG", "HD", "ATVI" ) 



  our_data <- tq_get(smallcap_tickers, from = "2018-06-30", to = "2020-06-30")
  df <- data.table(our_data)
  df_wide <- dcast(df, date ~ symbol, value.var = 'close')
  log_returns <- data.table(apply(log(df_wide[, -c(1)]), 2, diff))
  log_returns$date <- df_wide$date[-c(1)]

  small_cap_ret <-log_returns %>% 
    column_to_rownames(., var = "date")
  
  our_data <- tq_get(midcap_tickers, from = "2018-06-30", to = "2020-06-30")
  df <- data.table(our_data)
  df_wide <- dcast(df, date ~ symbol, value.var = 'close')
  log_returns <- data.table(apply(log(df_wide[, -c(1)]), 2, diff))
  log_returns$date <- df_wide$date[-c(1)]

  mid_cap_ret <-log_returns %>% 
    column_to_rownames(., var = "date")
  
  our_data <- tq_get(largecap_tickers, from = "2018-06-30", to = "2020-06-30")
  df <- data.table(our_data)
  df_wide <- dcast(df, date ~ symbol, value.var = 'close')
  log_returns <- data.table(apply(log(df_wide[, -c(1)]), 2, diff))
  log_returns$date <- df_wide$date[-c(1)]

  large_cap_ret <-log_returns %>% 
    column_to_rownames(., var = "date")
  
equity_data <- cbind(small_cap_ret, mid_cap_ret, large_cap_ret)

And i want to evaluate different portfolios with Expected Shortfall

portf.init <- portfolio.spec(colnames(equity_data))

portf.init <- add.constraint(portf.init, type = "weight_sum", min_sum = 0.99, max_sum = 1.01)

portf.init <- add.constraint(portf.init, type = "box", min=0.02, max = 0.25)

portf.init<- add.objective(portf.init, type = "return", name = "mean", multiplier = 0)

portf.minES <- add.objective(portf.init, type = "risk", name = "ES", arguments = list(p=0.95))

portf.minES.RB <- add.objective(portf.minES, type = "risk_budget", name = "ES", max_prisk = 0.25, arguments = list(p=0.95))

portf.minES.RB$constraints[[2]]$max <- rep(1,ncol(equity_data))

portf.minES.eqRB <- add.objective(portf.minES, type = "risk_budget", name = "ES", min_concentration = TRUE ,arguments = list(p=0.95))

portf.minES.eqRB <- add.constraint(portf.minES.eqRB, type = "box", min = 0.02, max = 0.25, indexnum = 2)


portf <- combine.portfolios(list(minES = portf.minES, minES.RB = portf.minES.RB, minES.eqRB = portf.minES.eqRB))

opt.minES <- optimize.portfolio(equity_data, portf ,optimize_method = "DEoptim", search_size = 2000, trace = TRUE, traceDE = 0)

The optimization via optimize.portfolio works fine, but when i want to rebalance the 3 portfolios i get the error [.data.frame(R, ep.i) : undefined columns selected

At first i guessed the error occurs, because there is a comma missing behind ep.iin the Source Code for optimize.protfolio.rebalancing in Line 1637: names(out_list)<-index(R[ep.i])

After adding the comma i tried to rebalance again with the modified function called optimize.portfolio.rebalancing_help

bt.opt.minES <- optimize.portfolio.rebalancing_help(equity_data, portf,optimize_method = "DEoptim", itermax = 10, search_size = 2000,trace = TRUE, rebalance_on = "months",K=3, training_period = 125, rolling_window = 125 )

But again the same error message occured. In Contrast after modifiing the function and testing it on a simpler Portfolio i did not get an error message as before when trying to balance just one portfolio and i finally got results

tickers <- c( "AAPL",'MCD', "KO", "NKE", "GOOG")

our_data <- tq_get(tickers, from = "2018-06-30", to = "2020-06-30")
  df <- data.table(our_data)
  df_wide <- dcast(df, date ~ symbol, value.var = 'close')
  log_returns <- data.table(apply(log(df_wide[, -c(1)]), 2, diff))
  log_returns$date <- df_wide$date[-c(1)]

  t <-log_returns %>% 
    column_to_rownames(., var = "date")

hilfe <- portfolio.spec(assets = colnames(t))
#Add constraint such that the weights sum to 1 (andere typen "leveraged")
hilfe <- add.constraint(portfolio = hilfe, type = "weight_sum", min_sum=0.99, max_sum = 1.01)

# Add box constraint such that no asset can have a weight of greater than 25% or less than 2%
hilfe <- add.constraint(portfolio = hilfe, type = "box", min = 0.02, max = 0.25)

#Add return objective with multiplier 0 such that portfolio mean ret is caLCULATED but does not impact optimization (for later)
hilfe <- add.objective(portfolio = hilfe, type ="return", name = "mean", multiplier = 0)

hilfe_minvar <- add.objective(portfolio = hilfe, type = "risk", name = "StdDev", multiplier = 0)

rp <- random_portfolios(hilfe_minvar, 1000, "sample")

opt_test <- optimize.portfolio.rebalancing_help(R=t, portfolio = hilfe_minvar
                                          , optimize_method = "DEoptim",itermax = 10, search_size = 2000,trace = TRUE,rp=rp, rebalance_on = "months",K=3, training_period = 125, rolling_window = 125)

Thanks in advance

emjay032 avatar Aug 03 '20 13:08 emjay032