Constructing Minimum Variance Portfolio with Asset Number Constraint using PortfolioAnalytics

24 Views Asked by At

For instance, I have a pool of 76 available stocks, but I want to limit my portfolio to only 6 of them. In the code below that I've attempted, it runs without errors, but it doesn't enforce the asset number constraint and ends up assigning weights to all available assets:

posi <- portfolio.spec(assets = colnames(data_rets))

posi <- add.constraint(posi, 
                       type = "position_limit",
                       max_pos = 6)

posi <- add.objective(posi, 
                      type = "risk", 
                      name = "var")

optimize.portfolio(R = data_rets,
                   portfolio = posi, 
                   optimize_method = "ROI")
0

There are 0 best solutions below