Hi Jing,
Thank you for sharing this. I have a question about the long-short strategy, if you are doing long-short portfolio, is that the sum weight should equall to 0 every time you reblance your portfolio?
def opt_portfolio(self):
''' maximize the sharpe ratio to find the optimal weights '''
cons = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
bnds = tuple((.01, 1) for x in range(self.n))
opt = minimize(self.min_func,
np.array(self.n * [1. / self.n]),
method='SLSQP',
bounds=bnds,
constraints=cons)
opt_sharpe = - opt['fun']
opt_weights = opt['x']
opt_return = self.annual_port_return(opt_weights)
opt_volatility = self.annual_port_vol(opt_weights)
return opt_weights, opt_return, opt_volatility, opt_sharpe
So in the optimization function, the constraint lambda x: np.sum(x)-1 should that just be np.sum(x) with the bnds tuple(-1,1)?
And one last question, can you explain to me how do you realized the monthly rebalance in this code? Which part exactly realized this? Thank you in advance!