def hrp(my_portfolio, perf=True) -> list: # changed to take in desired timeline, the problem is that it would use all historical data ohlc = yf.download( my_portfolio.portfolio, start=my_portfolio.start_date, end=my_portfolio.end_date, progress=False, ) prices = ohlc["Adj Close"].dropna(how="all") prices = prices.filter(my_portfolio.portfolio) # sometimes we will pick a date range where company isn't public we can't set price to 0 so it has to go to 1 prices = prices.fillna(1) rets = expected_returns.returns_from_prices(prices) hrp = HRPOpt(rets) hrp.optimize() weights = hrp.clean_weights() wts = weights.items() result = [] for val in wts: a, b = map(list, zip(*[val])) result.append(b) if perf is True: hrp.portfolio_performance(verbose=True) return flatten(result)
def test_portfolio_performance(): df = get_data() returns = df.pct_change().dropna(how="all") hrp = HRPOpt(returns) with pytest.raises(ValueError): hrp.portfolio_performance() hrp.optimize() assert hrp.portfolio_performance()
def test_portfolio_performance(): df = get_data() returns = df.pct_change().dropna(how="all") hrp = HRPOpt(returns) with pytest.raises(ValueError): hrp.portfolio_performance() hrp.optimize() np.testing.assert_allclose( hrp.portfolio_performance(), (0.21353402380950973, 0.17844159743748936, 1.084579081272277), )
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'
def test_pass_cov_matrix(): df = get_data() S = CovarianceShrinkage(df).ledoit_wolf() hrp = HRPOpt(cov_matrix=S) hrp.optimize() perf = hrp.portfolio_performance() assert perf[0] is None and perf[2] is None np.testing.assert_almost_equal(perf[1], 0.10002783894982334)
'RRC': 0.0, 'BBY': 0.0, 'MA': 0.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) plotting.plot_dendrogram(hrp) # 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,
df = df.set_index(df['t']) df = df.drop(['t'], axis=1) dataset = df dataset = dataset.dropna() data_[i] = dataset.c data_.dropna(axis=1, inplace=True) returns = risk_models.returns_from_prices(data_, log_returns=True) S = CovarianceShrinkage(data_, frequency=180).ledoit_wolf() hrp = HRPOpt(returns, cov_matrix=S) # weights = hrp.optimize() # hrp.portfolio_performance(verbose=True); weights = {w: 1 / len(pair) for w in pair} hrp.set_weights(weights) w = hrp.portfolio_performance(verbose=True, frequency=180) st.write("Expected annual return: {:.2f}%".format(w[0] * 100)) st.write("Annual volatility: {:.2f}%".format(w[1] * 100)) st.write("Sharpe Ratio: {:.2f}".format(w[2])) returns = risk_models.returns_from_prices(data_, log_returns=True) returns["sum"] = returns.sum(axis=1) returns["cum"] = returns['sum'].cumsum(axis=0) returns = returns.reset_index() plt.figure(figsize=(12, 6)) plt.plot(returns.cum) st.pyplot() shift_d = shift_d Prop = returns
st.write(f"## Moroccan Assets Allocation using Efficient Frontier") st.pyplot() ################################## Herarchical Risk Parity ############################################################### if model_name == 'HRP': hrp_ETF = HRPOpt(returns) hrp_Mor = HRPOpt(moroccan_stocks_returns) weights = hrp_ETF.optimize(linkage_method='single') wei = hrp_Mor.optimize(linkage_method='single') hrp_ETF.portfolio_performance(verbose=True) hrp_Mor.portfolio_performance(verbose=True) dfhrpETF = pd.DataFrame([weights]) dfhrpMor = pd.DataFrame([wei]) if len(ETF_name) == 0 & len(moroccan_stocks) == 0: st.write("Please select stocks!!!") if len(ETF_name) != 0: result_pct = dfhrpETF.div(dfhrpETF.sum(1), axis=0) ax = result_pct.plot(kind='bar', figsize=(20, 6), width=0.8, color=colors_list, edgecolor=None) plt.legend(labels=dfhrpETF.columns, fontsize=20) plt.xticks(fontsize=20) for spine in plt.gca().spines.values(): spine.set_visible(False) plt.yticks([])