Files
strategy-lab/to_explore/pyquantnews/54_BlackLitterman.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

4.9 KiB

No description has been provided for this image

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.

In [ ]:
import numpy as np
In [ ]:
from pypfopt.black_litterman import BlackLittermanModel
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models, plotting
In [ ]:
from openbb_terminal.sdk import openbb
import seaborn as sns
sns.set_theme()

Fetch price data for selected assets from the OpenBB terminal

In [ ]:
prices = openbb.economy.index(["AAPL", "BBY", "BAC", "SBUX", "T"])

Define absolute views on the expected returns for specific assets

In [ ]:
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

In [ ]:
cov_matrix = risk_models.sample_cov(prices)

Initialize the Black-Litterman model with equal weight priors and absolute views

In [ ]:
bl = BlackLittermanModel(
    cov_matrix, 
    absolute_views=viewdict,
    pi="equal"
)

Compute the implied expected returns using the Black-Litterman model and initialize Efficient Frontier

In [ ]:
rets = bl.bl_returns()
ef = EfficientFrontier(rets, cov_matrix)

Plot the efficient frontier showing the possible portfolios

In [ ]:
plotting.plot_efficient_frontier(ef, show_tickers=True)

Calculate and display the optimal weights for the portfolio

In [ ]:
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.