Example #1
0
def effrontier(mu, S, sht, nameplot):
    cla = CLA(mu, S)
    fig = plt.figure(figsize=(8, 8))
    cla.max_sharpe()
    ax = cla.plot_efficient_frontier(showfig=False)
    fig = ax.get_figure()
    sht.pictures.add(fig, name=nameplot, update=True)
Example #2
0
def cla_max_sharpe_weights(rets_bl, covar_bl, config):
    cla = CLA(rets_bl, covar_bl, weight_bounds= \
            (config['min_position_size'] ,config['max_position_size']))
    cla.max_sharpe()
    weights = cla.clean_weights()
    weights = pd.DataFrame.from_dict(weights, orient='index')
    weights.columns = ['CLA Max Sharpe']
    return weights, cla
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)
Example #4
0
 'GOOG': 0.049560084289929286,
 'JPM': 0.017675709092515708,
 'MA': 0.03812737349732021,
 'PFE': 0.07786528342813454,
 'RRC': 0.03161528695094597,
 'SBUX': 0.039844436656239136,
 'SHLD': 0.027113184241298865,
 'T': 0.11138956508836476,
 'UAA': 0.02711590957075009,
 'WMT': 0.10569551148587905,
 'XOM': 0.11175337115721229}
"""

# Crticial Line Algorithm
cla = CLA(mu, S)
print(cla.max_sharpe())
cla.portfolio_performance(verbose=True)
plotting.plot_efficient_frontier(cla)  # to plot
"""
{'GOOG': 0.020889868669945022,
 'AAPL': 0.08867994115132602,
 'FB': 0.19417572932251745,
 'BABA': 0.10492386821217001,
 'AMZN': 0.0644908140418782,
 'GE': 0.0,
 'AMD': 0.0,
 'WMT': 0.0034898157701416382,
 'BAC': 0.0,
 'GM': 0.0,
 'T': 2.4138966206946562e-19,
 'UAA': 0.0,
barchart = pd.Series(
    tickers['Weight']).sort_values(ascending=True).plot.barh(figsize=(10, 6))
plt.show(barchart)

#covariance heatmap
#to get a grasp how our chosen portfolio correlates with each other asset in the portfolio we state a cov heatmap
plotting.plot_covariance(cov_matrix_tickers, plot_correlation=True)

##create the Efficient frontier line and visualize it
#in order to visualize the efficient frontier from the optimized portfolio, we need to initiate a new built-in method
#we use the CLA method, because it is more robust than the default option
cla = CLA(mu_target_tickers, cov_matrix_tickers)
if risk_tolerance == 'Low':
    cla.min_volatility()
else:
    cla.max_sharpe()

#plot the efficient frontier line
ax_portfolio = plotting.plot_efficient_frontier(cla,
                                                ef_param='risk',
                                                ef_param_range=np.linspace(
                                                    0.2, 0.6, 100),
                                                points=1000)

#initialize Discrete Allocation to get a full picture what you could buy with a given amount
#the Discrete Allocation gives you the discrete amount of shares you have to allocate given your available funds
from pypfopt import DiscreteAllocation
from datetime import datetime
from datetime import timedelta

#create list out of sliced tickers dataframe