Simple policies#

class cvxportfolio.AdaptiveRebalance(target, tracking_error)#

Rebalance portfolio when deviates too far from target.

We use the 2-norm as trigger for rebalance. You may want to calibrate tracking_error for your application by backtesting this policy, e.g., to get your desired turnover.

Parameters:
  • target (pd.Series or pd.DataFrame) – target weights to rebalance to. It is assumed a constant if it is a Series. If it varies in time (you must specify it for every trading day) pass a DataFrame indexed by time.

  • tracking_error (pd.Series or pd.DataFrame) – we trade to match the target weights whenever the 2-norm of our weights minus the target is larger than this. Pass a Series if you want to vary it in time.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.Hold#

Hold initial portfolio, don’t trade.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.AllCash#

Allocate all weight to cash.

This is the default benchmark used in SinglePeriodOptimization and MultiPeriodOptimization policies.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.FixedTrades(trades_weights)#

Each day trade the provided trade weights vector.

If there are no weights defined for the given day, default to no trades.

Parameters:

trades_weights (pd.Series or pd.DataFrame) – target trade weights \(z_t\) to trade at each period. If constant in time use a pandas Series indexed by the assets’ names, including the cash account name (cash_key option to MarketSimulator). If varying in time, use a pandas DataFrame with datetime index and as columns the assets names including cash. If a certain time in the backtest is not present in the data provided the policy defaults to not trading in that period.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.FixedWeights(target_weights)#

Each day trade to the provided trade weights vector.

If there are no weights defined for the given day, default to no trades.

Parameters:

target_weights (pd.Series or pd.DataFrame) – target weights \(w_t^+\) to trade to at each period. If constant in time use a pandas Series indexed by the assets’ names, including the cash account name (cash_key option to the simulator). If varying in time, use a pandas DataFrame with datetime index and as columns the assets names including cash. If a certain time in the backtest is not present in the data provided the policy defaults to not trading in that period.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.MarketBenchmark#

Allocation weighted by last year’s total market volumes.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.PeriodicRebalance(target, rebalancing_times)#

Track a target weight vector rebalancing at given times.

This calls FixedWeights. If you want to change the target in time use that policy directly.

Parameters:
  • target (pandas.Series) – Allocation weights to rebalance to.

  • rebalancing_times (pandas.DateTimeIndex):) – Times at which we rebalance.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.ProportionalRebalance(target, target_matching_times)#

Trade proportionally in time to track fixed target weights at times.

This calls ProportionalTradeToTargets. If you want to change the target in time use that policy directly.

Parameters:
  • target (pandas.Series) – Allocation weights to rebalance to.

  • rebalancing_times (pandas.DateTimeIndex):) – Times at which we rebalance.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.ProportionalTradeToTargets(targets)#

Trade in equal proportion to match target weights in time.

Initially, it loads the list of trading days and so at each day it knows how many are missing before the next target’s day, and trades in equal proportions to reach those targets. If there are no targets remaining it defaults to not trading.

Parameters:

targets (pandas.DataFrame) – time-indexed DataFrame of target weight vectors at given points in time (e.g., start of each month).

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.RankAndLongShort(signal, num_long=1, num_short=1, target_leverage=1.0)#

Rank assets by signal; long highest and short lowest.

Parameters:
  • signal (pd.DataFrame) – time-indexed DataFrame of signal for all symbols excluding cash. At each point in time the num_long assets with highest signal will have equal positive weight, and the num_short assets with lower signal will have equal negative weight. If two or more assets have the same signal value and they are on the boundary of either the top or bottom set, alphanumerical ranking will prevail.

  • num_long (int or pd.Series) – number of assets to long, default 1; if specified as Series it must be indexed by time.

  • num_short (int or pd.Series)) – Number of assets to short, default 1; if specified as Series it must be indexed by time.

  • target_leverage (float or pd.Series) – leverage of the resulting portfolio, default 1; if specified as Series it must be indexed by time.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.Uniform(leverage=1.0)#

Uniform allocation on non-cash assets.

Parameters:

leverage (float) – Leverage of the allocation.

execute(h, market_data, t=None)#

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.

Raises:

cvxportfolio.errors.DataError – Holdings vector sum to a negative value.

Returns:

u, t, shares_traded

Return type:

pandas.Series, pandas.Timestamp, pandas.Series

class cvxportfolio.SellAll#

Sell all assets to cash.

Alias of AllCash.