Skip to main content

Portfolio optimization.

Project description

Cvxportfolio

CVXportfolio on PyPI Downloads Coverage Status Documentation Status

cvxportfolio is a python library for portfolio optimization and simulation based on the book Multi-Period Trading via Convex Optimization (also available in print).

The documentation of the package is kindly hosted by Read the Docs at www.cvxportfolio.com. We also show some of our tutorials and examples on our youtube channel.

Installation

All our source code and releases are kindly hosted by the Python Package Index. You can install the latest one with

pip install -U cvxportfolio

You can see how this works on our Installation and Hello World youtube video.

Testing locally

We ship our unit test suite with the pip package. After installing you can test in you local environment by

python -m unittest discover cvxportfolio

Simplest Example

In the following example market data is downloaded by a public source (Yahoo finance) and the forecasts are computed iteratively, at each point in the backtest, from past data. That is, at each point in the backtest, the policy object only operates on past data, and thus the result you get is a realistic simulation of what the strategy would have performed in the market. The simulator by default includes holding and transaction costs, using the models described in the book, and default parameters that are typical for the US stock market. The logic used matches what is described in Chapter 7 of the book. For example, returns are forecasted as the historical mean returns and covariances as historical covariances (both ignoring np.nan's). The logic used is detailed in the forecast module. Many optimizations are applied to make sure the system works well with real data.

import cvxportfolio as cvx

gamma = 3       # risk aversion parameter (Chapter 4.2)
kappa = 0.05    # covariance forecast error risk parameter (Chapter 4.3)
objective = cvx.ReturnsForecast() - gamma * (
	cvx.FullCovariance() + kappa * cvx.RiskForecastError()
) - cvx.StocksTransactionCost()
constraints = [cvx.LeverageLimit(3)]

policy = cvx.MultiPeriodOptimization(objective, constraints, planning_horizon=2)

simulator = cvx.StockMarketSimulator(['AAPL', 'AMZN', 'TSLA', 'GM', 'CVX', 'NKE'])

result = simulator.backtest(policy, start_time='2020-01-01')

# print backtest result statistics
print(result)

# plot backtest results
result.plot()

Some Other Examples

We show in the example on user-provided forecasters how the user can define custom classes to forecast the expected returns and covariances. These provide callbacks that are executed at each point in time during the backtest. The system enforces causality and safety against numerical errors. We recommend to always include the default forecasters that we provide in any analysis you may do, since they are very robust and well-tested.

We show in the examples on DOW30 components and wide assets-classes ETFs how a simple sweep over hyper-parameters, taking advantage of our sophisticated parallel backtest machinery, quickly provides results on the best strategy to apply to any given selection of assets.

Development

To set up a development environment locally you should

git clone https://github.com/cvxgrp/cvxportfolio.git
cd cvxportfolio
make env

This will replicate our development environment. From there you can test with

make test

You activate the shell environment with one of scripts in env/bin (or env\Scripts on windows), for example if you use bash on POSIX

source env/bin/activate

and from the environment you can run any of the scripts in the examples (the cvxportfolio package is installed in editable mode). Or, if you don't want to activate the environment, you can just run scripts directly using env/bin/python or env\Scripts\python on windows, like we do in the Makefile.

Examples from the book

In branch 0.0.X you can find the original material used to generate plots and results in the book. As you may see from those ipython notebooks a lot of the logic that was implemented there, outside of cvxportfolio proper, is being included and made automatic in newer versions of cvxportfolio.

Academic

If you use cvxportfolio in your academic work please cite our book:

@book{BBDKKNS:17,
    author       = {S. Boyd and E. Busseti and S. Diamond and R. Kahn and K. Koh and P. Nystrup and J. Speth},
    title        = {Multi-Period Trading via Convex Optimization},
    series       = {Foundations and Trends in Optimization},
    year         = {2017},
    month        = {August},
    publisher    = {Now Publishers},
    url          = {http://stanford.edu/~boyd/papers/cvx_portfolio.html},
}

License

Cvxportfolio is licensed under the Apache 2.0 permissive open source license.

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

cvxportfolio-0.4.5.tar.gz (256.7 kB view details)

Uploaded Source

Built Distribution

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

cvxportfolio-0.4.5-py3-none-any.whl (274.6 kB view details)

Uploaded Python 3

File details

Details for the file cvxportfolio-0.4.5.tar.gz.

File metadata

  • Download URL: cvxportfolio-0.4.5.tar.gz
  • Upload date:
  • Size: 256.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for cvxportfolio-0.4.5.tar.gz
Algorithm Hash digest
SHA256 8499c80d7989a91cd5c0da026d02dd593e8204e5a34c8b029d4e8811583c2b50
MD5 16b9672e629c3020fab9e45253c6c9fc
BLAKE2b-256 be5b2aa9b68e4d97a2d2e3454ae5fdf71edeaec5de5740e1e85203daf650c715

See more details on using hashes here.

File details

Details for the file cvxportfolio-0.4.5-py3-none-any.whl.

File metadata

  • Download URL: cvxportfolio-0.4.5-py3-none-any.whl
  • Upload date:
  • Size: 274.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for cvxportfolio-0.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a90e8ed0422c630edac3ad0002cd07ceed51de3a35b3dc77e92d3c70e9721354
MD5 6054099888db78ccabc5591dba266764
BLAKE2b-256 a37f42f1f8f215d284518fac78d0cec4b66ad8f6ec2a7c5474291ae754b3dcaf

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