Skip to main content

A research-focused portfolio optimization and backtesting engine.

Project description

OPES

An Open-source Portfolio Estimation System for advanced portfolio optimization and backtesting.


Overview

OPES is a comprehensive Python library for advanced portfolio optimization and backtesting. It is designed for quantitative finance enthusiasts who wants to explore cutting-edge methods for portfolio research or understanding. OPES aims to be research first, trying to prioritize sheer breadth over depth for each paradigm. OPES aims provide a wide range of portfolio strategies, risk measures and robust evaluation tools for users to always stay updated with current portfolio optimization techniques. OPES's inspiration grew from my own project, MOP, along with the giants of portfolio optimization: PyPortfolioOpt and Riskfolio-Lib.

Visit the documentation for detailed insights on OPES.


Disclaimer

The information provided by OPES is for educational, research and informational purposes only. It is not intended as financial, investment or legal advice. Users should conduct their own due diligence and consult with licensed financial professionals before making any investment decisions. OPES and its contributors are not liable for any financial losses or decisions made based on this content. Past performance is not indicative of future results.


Portfolio Objectives

Classification Name
Utility Theory Quadratic Utility
Constant Relative Risk Aversion (CRRA)
Constant Absolute Risk Aversion (CARA)
Hyperbolic Absolute Risk Aversion (HARA)
Kelly Criterion & Fractional Kelly
Markowitz Paradigm Maximum Mean Return
Minimum Variance
Mean-Variance
Maximum Sharpe Ratio
Principled Heuristics Uniform (1/N)
Risk Parity
Inverse Volatility
Softmax Mean
Maximum Diversification
Return Entropy Portfolio Optimization
Risk Measures Value at Risk (VaR)
Conditional Value at Risk (CVaR)
Mean-CVaR
Entropic Value at Risk (EVaR)
Mean-EVaR
Entropic Risk Measure
Worst-Case Loss
Online Learning Cover's Universal Portfolios
Best Constant Rebalanced Portfolio (BCRP)
Exponential Gradient
Distributionally Robust Optimization KL-Ambiguity Robust Maximum Mean Return
KL-Ambiguity Robust Kelly
KL-Ambiguity Robust Fractional Kelly
Wasserstein-Ambiguity Robust Maximum Mean Return
Wasserstein-Ambiguity Robust Minimum Variance
Wasserstein-Ambiguity Robust Mean-Variance

Metrics

Portfolio Metrics Backtest Metrics
Tickers Sharpe
Weights Sortino
Portfolio Entropy Volatility
Herfindahl Index Average Return
Gini Coefficient Total Return
Absolute Maximum Weight Mean Drawdown
Maximum Drawdown
Geometric Growth Rate
Value-at-Risk 95
Conditional-Value-at-Risk 95
Skew
Kurtosis
Omega Ratio
Ulcer Index
Hit Ratio

Other Features

Slippage Models Regularization Schemes
Constant L1 Regularization
Gamma L2 Regularization
Lognormal L-infinity Regularization
Inverse Gaussian Entropy
Compound Poisson-Lognormal Weight Variance
Mean Pairwise Absolute Deviation
KL-Divergence from Uniform (Experimental)
JS-Divergence from Uniform (Experimental)

Installation

Using pip

If you have pip, it is very convenient to install opes.

pip install opes

From the Source

Alternatively, you are also welcome to install directly from the GitHub repository.

git clone https://github.com/opes-core/opes.git
cd opes
pip install .

You can also install in editable mode if you plan on making any changes to the source code.

# After cloning
pip install -e .

Verification

Verify your installation by using pip.

pip show opes

You can also verify by using python.

>>> import opes
>>> opes.__version__

Getting Started

opes is designed to be minimalistic and easy to use and learn for any user. Here is an example script which implements my favorite portfolio, the Kelly Criterion.

# I recommend you use yfinance for testing.
# However for serious research, using an external, faster API would be more fruitful.
import yfinance as yf

# Importing our Kelly class
from opes.objectives.utility_theory import Kelly

# Obtaining ticker data
# Basic yfinance stuff
TICKERS = ["AAPL", "NVDA", "PFE", "TSLA", "BRK-B", "SHV", "TLT"]
asset_data = yf.download(
    tickers=TICKERS, 
    start="2010-01-01", 
    end="2020-01-01", 
    group_by="ticker", 
    auto_adjust=True
)

# Initialize a Kelly portfolio with fractional exposure and L2 regularization
# Fractional exposure produces less risky weights and L2 regularization contributes in penalizing concentration
kelly_portfolio = Kelly(fraction=0.8, reg="l2", strength=0.001)

# Compute portfolio weights with custom weight bounds
kelly_portfolio.optimize(data, weight_bounds=(0.05, 0.8))

# Clean negligible allocations
cleaned_weights = kelly_portfolio.clean_weights(threshold=1e-6)

# Output the final portfolio weights
print(cleaned_weights)

This showcases the simplicty of the module. However there are far more diverse features you can still explore. If you're looking for more examples, preferably some of them with "context", I recommend you check out the examples page within the documentation.


Testing

Tests for opes are written using the pytest module. You can run the tests easily using the following command.

cd project-root    # Navigate to project root
pytest

This will run three scripts, each dedicated to testing the optimizer, regularizer and backtesting engine. Note that the tests are heavy and can take a significant amount of time since it tests each of the available objectives. We test using a year's data for the following tickers.

GOOG, AAPL, AMZN, MSFT

The price data is stored in the prices.csv file within the tests/ directory. The number of tickers are limited to 4 since there are computationally heavy portfolio objectives (like UniversalPortfolios) included which may take an eternity to test well using multiple tickers.

Also it eats up RAM like pac-man.


Upcoming Features (Unconfirmed)

These features are still in the works and may or may not appear in later updates:

Objective Name (Category)
Hierarchical Risk Parity (Principled Heuristics)
Online Newton Step (Online Learning)
ADA-BARRONS (Online Learning)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opes-0.8.1.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opes-0.8.1-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file opes-0.8.1.tar.gz.

File metadata

  • Download URL: opes-0.8.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for opes-0.8.1.tar.gz
Algorithm Hash digest
SHA256 571f72573b57eda7e11180dc82b526c5fe6b3186f18d4b475509d68cf155b38c
MD5 c4f225a4cfd400f26242d2b851a68a3e
BLAKE2b-256 d0e23cf9c42d9ff17e3c0ca5a691ad2ba40c35b211d54dd7ebc05802edca961a

See more details on using hashes here.

File details

Details for the file opes-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: opes-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for opes-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e2ec2a10ad7681780a0a075e01338bad0ad062ad60b2b485b245f7f589b0c9c
MD5 2b2bf293ddff7ea8699447f1d5731af5
BLAKE2b-256 2d819bd1b83b24b3056a4cf1f873d4d3614b96cbd92108279ec6d997a4774b2b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page