Here\'s the code (I\'m sorry if it\'s so long, but it was the first example I had); I\'m using the CVaR example from CreditMetrics package by A. Wittmann and
I think you should add a penalty for any deviation from one.
Add to your minimizing problem the term +(sum(weights) - 1)^2 * 1e10. You should see that this huge penalty will force the weights to sum to 1!