Simple policies¶
- class cvxportfolio.AdaptiveRebalance(target, tracking_error)View on GitHub¶
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 or cvx.policy.Policy instance) – 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. You can also pass a policy object instance and its resulting weights will be used as targets, at each point in 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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.HoldView on GitHub¶
Hold initial portfolio, don’t trade.
- 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 policyt (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 theironline_usage
argument toTrue
.
- 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.AllCashView on GitHub¶
Allocate all weight to cash.
This is the default benchmark used in
SinglePeriodOptimization
andMultiPeriodOptimization
policies.- 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 policyt (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 theironline_usage
argument toTrue
.
- 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.FixedTrades(trades_weights)View on GitHub¶
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 toMarketSimulator
). 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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.FixedWeights(target_weights)View on GitHub¶
Each day trade to the provided 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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.MarketBenchmark(mean_volume_forecast=<class 'cvxportfolio.forecast.HistoricalMeanVolume'>)View on GitHub¶
Allocation weighted by average market traded volumes.
This policy provides an approximation of a market capitalization-weighted allocation, by using the average of traded volumes in units of value (e.g., USDOLLAR) over the previous year (by default, can be changed) as proxy.
Added in version 1.2.0: We added the
mean_volume_forecast
parameter.- Parameters:
mean_volume_forecast (pandas.DataFrame, cvx.forecast.BaseForecast class or instance) – Forecaster class that computes the average of historical volumes. You can also pass a DataFrame containing your own forecasts computed externally. Default is
cvxportfolio.forecast.HistoricalMeanVolume
which is instantiated with parameterrolling=pd.Timedelta('365.24d')
(that’s one solar year in number of days). If you want to provide a different forecaster, or change the parameters (like adding exponential smoothing) you should instantiate the forecaster class and pass the instance.
- 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 policyt (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 theironline_usage
argument toTrue
.
- 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.PeriodicRebalance(target, rebalancing_times)View on GitHub¶
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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.ProportionalRebalance(target, target_matching_times)View on GitHub¶
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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.ProportionalTradeToTargets(targets)View on GitHub¶
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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.RankAndLongShort(signal, num_long=1, num_short=1, target_leverage=1.0)View on GitHub¶
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)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 policyt (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 theironline_usage
argument toTrue
.
- 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.Uniform(leverage=1.0)View on GitHub¶
Uniform allocation on non-cash assets.
- Parameters:
leverage (float) – Leverage of the allocation. Default is 1, corresponding to the classic \(1/n\) allocation.
- 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 policyt (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 theironline_usage
argument toTrue
.
- 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.SellAllView on GitHub¶
Sell all assets to cash.
Alias of
AllCash
.