def omptimizePortCLA(port, weights, start, plot=False, short=False, printBasicStats=True, how='Sharpe'): #Getting Data df = bpf.getData(port, start) #Plotting the portfolio if plot: bpf.plotPort(df, port) if printBasicStats: bpf.basicStats(df, weights) #Optimization for Sharpe using Efficient Frontier if short: bounds = (-1, 1) else: bounds = (0, 1) mu = df.pct_change().mean() * 252 S = risk_models.sample_cov(df) if how == 'Sharpe': # Maximized on Sharpe Ratio cla = CLA( mu, S ) #Here the weight bounds are being used to allow short positions as well weights = cla.max_sharpe() cleaned_weights = dict(cla.clean_weights()) print("Weights of an optimal portfolio maximised on Sharpe Ratio:") print(cleaned_weights) cla.portfolio_performance(verbose=True) bpf.getDiscreteAllocations(df, weights) plot_ef(cla) plotting.plot_weights(weights) elif how == "Vol": # Minimized on Volatility cla = CLA(mu, S) cla.min_volatility() w = dict(cla.clean_weights()) print("Weights of an optimal portfolio minimized on Volatilty (Risk):") print(w) cla.portfolio_performance(verbose=True) bpf.getDiscreteAllocations(df, w) plot_ef(cla) plotting.plot_weights(w)
def cla_min_vol_weights(rets_bl, covar_bl, config): cla = CLA(rets_bl, covar_bl, weight_bounds= \ (config['min_position_size'] ,config['max_position_size'])) cla.min_volatility() weights = cla.clean_weights() weights = pd.DataFrame.from_dict(weights, orient='index') weights.columns = ['CLA Min Vol'] return weights, cla