Example #1
0
def HRP():

    sht = xw.Book.caller().sheets['Optim']
    sht.range('N17').value = 'Optimizing...'

    listticker = sht.range('B3').expand().value
    startdate = sht.range('F3').value
    enddate = sht.range('F6').value
    traintestdate = sht.range(
        'F4'
    ).value  #Dataset is divided in two sub: train (optimization) and test for backtest
    train, test = initialize(startdate, enddate, traintestdate, listticker)
    train, test = train.pct_change().dropna(), test.pct_change().dropna()
    hrp = HRPOpt(train)
    weights = hrp.optimize()
    perf = hrp.portfolio_performance(verbose=False)
    fig = plt.figure(figsize=(8, 8))
    ax = hrp.plot_dendrogram(showfig=False)  # to plot dendrogram
    fig = ax.get_figure()
    sht.range('P23').value = weights
    sht.range('P21').value = perf

    #Visualisation
    sht.pictures.add(fig, name="HRPCluster", update=True)
    sht.charts['HRPweights'].set_source_data(
        sht.range((23, 16), (22 + len(listticker), 17)))

    #Done
    sht.range('N17').value = 'Optimization Done'
Example #2
0
 'PFE': 0.0,
 'JPM': 0.14379,
 'SBUX': 0.0}

Expected annual return: 15.3%
Annual volatility: 28.7%
Sharpe Ratio: 0.46
"""


# Hierarchical risk parity
hrp = HRPOpt(returns)
weights = hrp.optimize()
hrp.portfolio_performance(verbose=True)
print(weights)
hrp.plot_dendrogram()  # to plot dendrogram

"""
Expected annual return: 10.8%
Annual volatility: 13.2%
Sharpe Ratio: 0.66

{'AAPL': 0.022258941278778397,
 'AMD': 0.02229402179669211,
 'AMZN': 0.016086842079875,
 'BABA': 0.07963382071794091,
 'BAC': 0.014409222455552262,
 'BBY': 0.0340641943824504,
 'FB': 0.06272994714663534,
 'GE': 0.05519063444162849,
 'GM': 0.05557666024185722,