Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can i put constraint on weight so that i can get sharp ratio and optimum return and volatility based on it.

I have written a code to get the sharp ratio. now i need to constrain the weight based on
The minimum - maximum permissible weight in any single asset class
ii. The minimum - maximum weight to an asset type/ product within the asset class. How can i do it. Also is there a way to get sharp ratio using cvxopt. I got lot of examples on effective frontier but how to get the same result as my code is a problem.
my full code is here
https://github.com/SouravRoy1989/portfolio-analysis/blob/master/Final%20Optimization.ipynb

def get_ret_vol_sr(weights):  
    """  
    Takes in weights, returns array or return,volatility, sharpe ratio  
    """  
    weights = np.array(weights)  
    ret = np.sum(stocks.mean() * weights)  
    vol = np.sqrt(np.dot(weights.T, np.dot(stocks.cov(), weights)))  
    sr = ret/vol  
    return np.array([ret,vol,sr])


from scipy.optimize import minimize  
def neg_sharpe(weights):  
    return  get_ret_vol_sr(weights)[2] * -1

def check_sum(weights):  
    '''  
    Returns 0 if sum of weights is 1.0  
    '''  
    return np.sum(weights) - 1  
cons = ({'type':'eq','fun': check_sum})

initial=np.random.rand(n)  
initial = initial / np.sum(initial)  
opt_results = minimize(neg_sharpe,initial,method='SLSQP',bounds=bounds,constraints=cons)  
print ('The Optimum weights',opt_results.x)  
#opt_results.x.sum() # Checking sum is 1 or not

get_ret_vol_sr(opt_results.x)  
get_ret_vol_sr(opt_results.x)[0].max()