Hello World Example

This is a simple example of Cvxportfolio’s capabilities.

A multi-period optimization policy, with default forecasts and simple choice of objective terms and constraints, is compared to a uniform (1/n) allocation for a certain selection of stocks and time period.

The results are printed and plotted using the default methods.

import cvxportfolio as cvx

# risk aversion parameter (Chapter 4.2)
# chosen to match resulting volatility with the
# uniform portfolio (for illustrative purpose)
GAMMA = 2.5

# covariance forecast error risk parameter (Chapter 4.3)
# this can help regularize a noisy covariance estimate
KAPPA = 0.05

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', 'UBER', 'ZM', 'CVX', 'TSLA', 'GM', 'ABNB', 'CTAS',
    'GOOG'])

results = simulator.backtest_many(
    [policy, cvx.Uniform()], start_time='2020-01-01')

# print multi-period result
print("\n# MULTI-PERIOD OPTIMIZATION\n")
print(results[0])

# print uniform allocation result
print("\n# UNIFORM ALLOCATION:\n")
print(results[1])

# plot value and weights of the portfolio in time for MPO
mpo_figure = results[0].plot()

# plot value and weights of the portfolio in time for uniform
uniform_figure = results[1].plot()

This is the output printed to screen when executing this script. You can see many statistics of the back-tests. The timestamps of the back-test are the open times of the New York stock market (9.30am New York time) expressed in UTC.

Updating data..........

# MULTI-PERIOD OPTIMIZATION
 
#################################################################
Universe size                                                  11
Initial timestamp                       2020-01-02 14:30:00+00:00
Final timestamp                         2024-03-18 13:30:00+00:00
Number of periods                                            1059
Initial value (USDOLLAR)                                1.000e+06
Final value (USDOLLAR)                                  3.126e+06
Profit (USDOLLAR)                                       2.126e+06
                                                                 
Avg. return (annualized)                                    33.0%
Volatility (annualized)                                     34.4%
Avg. excess return (annualized)                             31.1%
Avg. active return (annualized)                             31.1%
Excess volatility (annualized)                              34.4%
Active volatility (annualized)                              34.4%
                                                                 
Avg. growth rate (annualized)                               27.1%
Avg. excess growth rate (annualized)                        25.2%
Avg. active growth rate (annualized)                        25.2%
                                                                 
Avg. StocksTransactionCost                                    0bp
Max. StocksTransactionCost                                    2bp
Avg. StocksHoldingCost                                        1bp
Max. StocksHoldingCost                                        3bp
                                                                 
Sharpe ratio                                                 0.90
Information ratio                                            0.90
                                                                 
Avg. drawdown                                              -14.4%
Min. drawdown                                              -43.1%
Avg. leverage                                              154.7%
Max. leverage                                              246.5%
Avg. turnover                                                0.7%
Max. turnover                                               31.1%
                                                                 
Avg. policy time                                           0.008s
Avg. simulator time                                        0.005s
    Of which: market data                                  0.001s
Total time                                                14.134s
#################################################################


# UNIFORM ALLOCATION:
 
#################################################################
Universe size                                                  11
Initial timestamp                       2020-01-02 14:30:00+00:00
Final timestamp                         2024-03-18 13:30:00+00:00
Number of periods                                            1059
Initial value (USDOLLAR)                                1.000e+06
Final value (USDOLLAR)                                  2.401e+06
Profit (USDOLLAR)                                       1.401e+06
                                                                 
Avg. return (annualized)                                    25.8%
Volatility (annualized)                                     31.2%
Avg. excess return (annualized)                             23.8%
Excess volatility (annualized)                              31.2%
                                                                 
Avg. growth rate (annualized)                               20.9%
Avg. excess growth rate (annualized)                        18.9%
                                                                 
Avg. StocksTransactionCost                                    0bp
Max. StocksTransactionCost                                    3bp
Avg. StocksHoldingCost                                        0bp
Max. StocksHoldingCost                                        0bp
                                                                 
Sharpe ratio                                                 0.76
                                                                 
Avg. drawdown                                              -12.6%
Min. drawdown                                              -39.7%
Avg. leverage                                               99.9%
Max. leverage                                              100.0%
Avg. turnover                                                0.8%
Max. turnover                                               50.0%
                                                                 
Avg. policy time                                           0.001s
Avg. simulator time                                        0.005s
    Of which: market data                                  0.001s
Total time                                                 6.367s
#################################################################

And these are the figure that are plotted. The result of the cvxportfolio.MultiPeriodOptimization policy:

hello_world.py result figure

This figure is made by the cvxportfolio.result.BacktestResult.plot() method.

And result of the cvxportfolio.Uniform policy, which allocates equal weight to all non-cash assets:

hello_world.py result figure

This figure is made by the cvxportfolio.result.BacktestResult.plot() method.