I am using SLSQP function in NLOPTR for portfolio construction using 34 low volatility stocks. The stocks are not important. What I am trying to do is being able to impose a minimum weight and a maximum weight to each stock.
The hin function, imposes the minimum weight constraint - as can be seen here - that each weight must be at least 1% in the construction of the portfolio; this is seen by the '(x - 0.01)'
hin <- function(x){
return(x - 0.01)
}
I don't know how to now add a maximum constraint (of say 15% max weight per stock) to the function as well as having the minimum constraint. Thus will have a weighting constraint of minimum 1% and maximum 15%.
Can someone help me to create a minimum and a maximum constraint.
My code for running the optimisation is as follows:
modvol <- subset(matret, select=modvolreturns)
covmodvol <- cov(modvol)
var(modvol[,1])
size <- 34
fn <- function(x){
return(t(x)%*%covmodvol%*%x)
}
hin <- function(x){
return(x - 0.01)
}
heq <- function(x){
return(sum(x)-1)
}
startx <- vector("numeric",size)
startx[1:size] <- 1/size
mvportmodvol <- slsqp(startx,fn,hin = hin,heq = heq)