Optimization-based policies

class cvxportfolio.SinglePeriodOptimization(objective, constraints=(), include_cash_return=True, benchmark=<class 'cvxportfolio.policies.AllCash'>, **kwargs)View on GitHub

Single Period Optimization policy.

Implements the model developed in chapter 4, page 43 of the paper. You specify the objective terms using classes such as ReturnsForecast and TcostModel, each multiplied by its multiplier. You also specify lists of constraints.

Parameters:
  • objective (CombinedCost) – This algebraic combination of cvxportfolio cost objects will be maximized

  • constraints (list of Constraints) – These will be imposed on the optimization. Default [].

  • include_cash_return (bool) – Whether to automatically include the CashReturn term in the objective, with default parameters. Default is True.

  • benchmark (Policy class or instance) – Benchmark weights to use in the risk model and other terms that need it. You can use any policy here. Suggested ones ones are AllCash, the default, Uniform (uniform allocation on non-cash assets), and MarketBenchmark, which approximates the market-weighted portfolio.

  • kwargs (dict) – Any extra argument will be passed to cvxpy.Problem.solve, so you can choose a solver and pass parameters to it.

execute(h, market_data, t=None)View on GitHub

Execute trading policy at current or user-specified time.

Return the (e.g., dollar) trade vector \(u\), the timestamp of execution (for double check in case you don’t pass it), and a Pandas Series of the number of shares to trade, if you pass a Market Data server which provides open prices (or None).

Parameters:
  • h (pandas.Series) – Holdings vector, in dollars, including the cash account (the last element).

  • market_data (cvxportfolio.MarketData instance) – MarketData instance used to provide data to the policy

  • t (pandas.Timestamp or None) – Time at which we execute. If None (the default), the last timestamp in the trading calendar provided by the MarketData instance is used. Note: if you use a default market data server, you probably want to set their online_usage argument to True.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value or don’t match the market data server’s universe.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.MultiPeriodOptimization(objective, constraints=(), include_cash_return=True, planning_horizon=None, terminal_constraint=None, benchmark=<class 'cvxportfolio.policies.AllCash'>, **kwargs)View on GitHub

Multi Period Optimization policy.

Implements the model developed in chapter 5, page 49 of the paper. You specify the objective terms using classes such as ReturnsForecast and TcostModel, each multiplied by its multiplier. You also specify lists of constraints. There are two ways to do it. You either define the same objective terms and costraints for each step of the multi-period problem, or you define a different objective term and different list of constraints for each step. In addition we offer a terminal_constraint argument to simply impose that at the last step in the optimization the post-trade weights match the given weights (see page 51).

When it computes the trajectory of weights for the future it only returns the first step (to the Simulator, typically). The future steps (planning horizon) are by default not returned.

Parameters:
  • objective (algebra of Cost or list of) – These will be maximized; if you pass a single expression of Cost it is understood as the same for all steps; if it’s a list you must also pass a list of lists for constraints, each term represents the cost for each step of the optimization (starting from the first, i.e., today) and the length of the list is used as planning_horizon (the value you pass there will be ignored)

  • constraints (list of Constraints or list of those) – These will be imposed on the optimization. Default []. Pass this as a list of lists of the same length as objective to specify different constraints at different time steps.

  • planning_horizon (int or None) – How many steps in the future we plan for. Ignored if passing objective and constraints as lists. Default is None.

  • terminal_constraint (pd.Series or None) – If you pass a Series to this (default is None) it will impose that at the last step of the multi period optimization the post-trade weights are equal to this.

  • include_cash_return (bool) – Whether to automatically include the CashReturn term in the objective, with default parameters. Default is True.

  • benchmark (Policy class or instance) – Benchmark weights to use in the risk model and other terms that need it. You can use any policy here. Suggested ones ones are AllCash, the default, Uniform (uniform allocation on non-cash assets), and MarketBenchmark, which approximates the market-weighted portfolio.

  • kwargs (dict) – Any extra argument will be passed to cvxpy.Problem.solve, so you can choose a solver and pass parameters to it.

Raises:

SyntaxError – If the format of provided objective and constraints is not right.

execute(h, market_data, t=None)View on GitHub

Execute trading policy at current or user-specified time.

Return the (e.g., dollar) trade vector \(u\), the timestamp of execution (for double check in case you don’t pass it), and a Pandas Series of the number of shares to trade, if you pass a Market Data server which provides open prices (or None).

Parameters:
  • h (pandas.Series) – Holdings vector, in dollars, including the cash account (the last element).

  • market_data (cvxportfolio.MarketData instance) – MarketData instance used to provide data to the policy

  • t (pandas.Timestamp or None) – Time at which we execute. If None (the default), the last timestamp in the trading calendar provided by the MarketData instance is used. Note: if you use a default market data server, you probably want to set their online_usage argument to True.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value or don’t match the market data server’s universe.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.SinglePeriodOpt(objective, constraints=(), include_cash_return=True, benchmark=<class 'cvxportfolio.policies.AllCash'>, **kwargs)View on GitHub

Alias of SinglePeriodOptimization.

As it was defined originally in section 6.1 of the paper.

class cvxportfolio.MultiPeriodOpt(objective, constraints=(), include_cash_return=True, planning_horizon=None, terminal_constraint=None, benchmark=<class 'cvxportfolio.policies.AllCash'>, **kwargs)View on GitHub

Alias of MultiPeriodOptimization.

As it was defined originally in section 6.1 of the paper.