4.9 KiB
This code utilizes the Black-Litterman model to incorporate subjective views into the portfolio optimization process. It fetches price data for selected assets, constructs a covariance matrix, and sets up absolute views on asset returns. The Black-Litterman model is then used to compute new expected returns, followed by the construction of an efficient frontier. This approach helps in creating a more informed and optimized portfolio by blending market equilibrium with investor views.
import numpy as np
from pypfopt.black_litterman import BlackLittermanModel from pypfopt.efficient_frontier import EfficientFrontier from pypfopt import risk_models, plotting
from openbb_terminal.sdk import openbb import seaborn as sns sns.set_theme()
Fetch price data for selected assets from the OpenBB terminal
prices = openbb.economy.index(["AAPL", "BBY", "BAC", "SBUX", "T"])
Define absolute views on the expected returns for specific assets
viewdict = { "AAPL": 0.20, "BBY": 0.30, "BAC": 0.10, "SBUX": 0.2, "T": 0.15 }
Construct the sample covariance matrix using historical price data
cov_matrix = risk_models.sample_cov(prices)
Initialize the Black-Litterman model with equal weight priors and absolute views
bl = BlackLittermanModel( cov_matrix, absolute_views=viewdict, pi="equal" )
Compute the implied expected returns using the Black-Litterman model and initialize Efficient Frontier
rets = bl.bl_returns() ef = EfficientFrontier(rets, cov_matrix)
Plot the efficient frontier showing the possible portfolios
plotting.plot_efficient_frontier(ef, show_tickers=True)
Calculate and display the optimal weights for the portfolio
bl.bl_weights()
PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.
