Optimising weights in R (Portfolio)

440 views Asked by At

I tried several packages in R and I am really lost in which one I should be using. I just need help in general direction and I can find my way myself for the exact code.

I am trying portfolio optimization in R. I need weights vector to be calculated where each weight in the vector represents percentage of that stock.

Given the weights, I calculate total return, variance and sharpe ratio (function of return and variance).

There could be constraints like total weights should be equal to 1 (100%) and may be some others on case by case basis.

I am trying to get my code to be flexible that I can optimize with different objectives (one at a time though). For example, I could want minimum variance in one simulation or maximum return in other and even max. sharpe ration in other.

This is pretty straight forward in excel with solver package. Once I have formulas entered, whichever cell I pick for objective function, it will calculate weights based on that and then calculate other parameters based on those weights. (Eg, if I optimize based on min variance, then it calculate weights for min variance and then calculate return and sharpe based on those weights).

I am wondering how to go about it in R? I am lost in reading documetation of several R packages or functions (Lpsolve, Optim, constrOptim, portfoiloAnalytics, etc) but not able to find the starting point. My specific questions are

  1. Which would be the right R package for this kind of analysis?
  2. Do I need to define separate functions for each possible objective, like variance, return and sharpe and optimize those functions? This is little tricky because sharpe depends on variance and returns. So if I want to optimize sharpe functions, then do I need to nest it within the variance and return functions?

I just need some ideas on how to start and I can give it a try. If I at least get the right package and right example to use, it would be great. I searched a lot on the web but I am really lost.

0

There are 0 answers