Skip to main content

Portfolio Optimization

The Portfolio Optimization menu allows the user to apply advanced optimization techniques to a portfolio of any type and of any size. It does so by introducing a multitude of optimization techniques ranging from mean-variance optimization to risk parity models and hierarchical clustering models. By providing Excel templates, the user can make sense of the vast array of parameters that each command has. E.g. think of the historic period you wish to use or which of the more than 10 risk measures and covariance methods should be used? These are questions the templates make easier to answer.

The capabilities of the Portfolio Optiminzation menu from the OpenBB Terminal are wrapped into a powerful SDK, enabling users to work with the data in a flexible environment that can be fully customized to meet the needs of any user. These functionalities allow the user to apply advanced optimization techniques to a portfolio of any type and of any size. It does so by introducing a multitude of optimization techniques ranging from mean-variance optimization to risk parity models and hierarchical clustering models. By providing Excel templates, the user can make sense of the vast array of parameters that each command has. E.g. think of the historic period you wish to use or which of the more than 10 risk measures and covariance methods should be used? These are questions the templates make easier to answer.

How to use

Start a Python script or Notebook file by importing the module:

from openbb_terminal.sdk import openbb

This menu requires the usage of the Excel templates to work properly. As there is a lot of complexity involved around these techniques, these templates allow the user to understand what values for each parameter are actually used and allow for an easy way to define the allocation.

If you wish to load in your own Excel allocation file, please follow the following steps:
  1. Download the Excel file that can be used as a template here.
  2. Move the file inside the portfolio/allocation folder within the OpenBBUserData folder and, optionally, adjust the name to your liking.
  3. Open the Excel file and remove, edit or add to the values as you desire (e.g. your own allocation). This is the default template that is also loaded in with load --example.
  4. The file can now be used by following the guide.

Furthermore, given the amount of options you can choose from in each command and giving the complexity of the topic, we also provide a parameter file (both .xlsx and .ini to adjust parameters in a user-friendly way).

If you wish to load in your own Excel or ini parameter file, please follow the following steps:
  1. Download the file that can be used as a template: xlsx (recommended) or ini (advanced).
  2. Move the file inside the portfolio/optimization folder within the OpenBBUserData folder and, optionally, adjust the name to your liking.
  3. Open the file and set parameters as you wish.
  4. Open up the OpenBB Terminal, go to portfolio/po and type file --file. The file should then be one of the options.

OpenBB Parameters Template

This template provides the user with the ability to set define values for each parameter based on the optimization technique that is deployed. E.g. if you select riskparity for the technique parameter, you will notice that some parameters turn grey. This means that the parameter is irrelevant for the selected method.

The OpenBB Terminal does, however, allow the user to run any model despite the technique you selected. Therefore, if you are interested in running multiple models, consider removing the value for technique. Do note that this makes it more difficult to understand which values are used for which model.

OpenBB Parameters Template

OpenBB Portfolio Template

This template hands the user a format to work with it to define the portfolio. Here, categorization is applied based on asset class, sector, industry, country and currency. By using the dropdown menus within this Excel, you are able to apply the proper categorization. This is based on the same methodology as found in other areas of the terminal.

OpenBB Portfolio Template

You can load in the portfolio template with the following code:

import pandas as pd

# Define your own orderbook path here, current value won't work
order_book_path = "allocation_example.xlsx"

# Read in the file
order_book = pd.read_excel(order_book_path)

# Adjust the columns accordingly
order_book_cols = ['Ticker', 'Asset Class', 'Sector', 'Industry', 'Country',
'Current Invested Amount', 'Currency']

order_book = order_book[order_book_cols]

# Load in the portfolio
p = openbb.portfolio.po.load(symbols_file_path=order_book_path)
weights, performance = openbb.portfolio.po.equal(portfolio_engine=p)

Performing optimization

Based on the parameters and allocation the user has set, the optimization process begins. What optimization technique is ideal depends entirely on the user's risk profile and objectives. As an illustration, Risk Parity is presented below:

# Perform calculations
weights_riskparity, data_returns_riskparity = openbb.portfolio.po.riskparity(p)

weights_riskparity

Which returns:

Risk Parity
AAPL0.0462
AMZN0.04545
APTV0.03177
ASML0.03328
BABA0.04192
GOOGL0.04758
HYG0.13093
NKE0.04909
TIP0.30826
TLT0.21927
TSM0.04626

It is possible to use the commands without loading in the parameters template or by using the parameters template but changing some arguments directly into the terminal. For example, using the same method as described above, the risk measure is changed to Conditional Value at Risk (CVaR) and the used historic period is increased to 10 years (keeping all other parameters unchanged):

# Perform calculations
weights_riskparity, data_returns_riskparity = openbb.portfolio.po.riskparity(p, interval="5y", risk_measure="CVaR")

weights_riskparity

Which returns:

Risk Parity
AAPL0.04843
AMZN0.04302
APTV0.03907
ASML0.03717
GOOGL0.04877
HYG0.14643
NKE0.05275
TIP0.33264
TLT0.20371
TSM0.04802

Examples

Starting by loading in the same dataset again with the following:

import pandas as pd

# Define your own orderbook path here, current value won't work
order_book_path = "allocation_example.xlsx"

# Read in the file
order_book = pd.read_excel(order_book_path)

# Adjust the columns accordingly
order_book_cols = ['Ticker', 'Asset Class', 'Sector', 'Industry', 'Country',
'Current Invested Amount', 'Currency']

order_book = order_book[order_book_cols]

# Load in the portfolio
P = openbb.portfolio.po.load(symbols_file_path=order_book_path)

Then, the Hierarchical Risk Parity technique is applied by using the following:

riskparity, data_returns_riskparity = openbb.portfolio.po.hrp(p, interval="5y",
risk_measure='SLPM',
risk_aversion=0.8)

print(riskparity)
print(data_returns_riskparity)

This results in a similar result as the following:

Hierarchical Risk Parity
A0.00199
AAL0.00104
AAP0.00185
AAPL0.00184
ABBV0.0028
......
NOC0.00228
XOM0.00167
ZBRA0.00156
ZION0.0014
ZTS0.00225

It is possible to delve further into these findings with the plot functionality for example done by looking at the portfolio's returns histogram which also includes a variety of risk measures as well as the portfolio's drawdowns.

openbb.portfolio.po.plot(p, category='SECTOR', chart_type="hist")
Portfokio Returns Histogram
openbb.portfolio.po.plot(p, category='SECTOR', chart_type="dd")
Portfolio Drawdowns