Portfolio & pairs#

Rank assets against each other, turn scores into weights, backtest a book of many assets from one account, and trade pairs.

Cross-sectional#

Transforms across the assets of one time step: ranks, z-scores, demeaning, winsorizing, weight normalization, and the dispersion of a row.

Function

Description

Subtract the row mean

Subtract the mean across assets at each step, removing the common component.

Scale a row of scores into portfolio weights

Scale each row of scores to portfolio weights with a chosen gross exposure, and optionally a net exposure.

Rank of each asset within the row

Rank each asset against the others at the same step, scaled to [-1, 1] or [0, 1].

Standard deviation across the row

Standard deviation across the assets of each step: one dispersion value per row.

Long the top, short the bottom of each row

+1 for the highest values of each row, -1 for the lowest, 0 for the rest.

Clip each row to its quantiles

Clip each asset's value to the lower and upper quantiles of its row.

Z-score of each asset within the row

Standardize each asset against the others at the same step: (x - mean) / std across the row.

Portfolio backtesting#

Backtest many assets from one shared account: weights, rebalancing, exposure limits, and book statistics.

Function

Description

Backtest a portfolio of target weights on OHLC bars

Backtest K assets traded toward per-asset targets from one shared capital account on OHLC bars, executed delay bars later at a chosen bar price.

Backtest a portfolio of target weights on prices

Backtest K assets traded toward per-asset target weights, notionals, or positions from one shared capital account, filled at each step's price.

Running report columns for a multi-asset backtest

Reduce a multi-asset backtest engine output into causal portfolio-level report columns.

Pairs & mean reversion#

Hedge ratios, spreads, stationarity tests, and entry and exit rules for a pairs trade.

Function

Description

Entry and exit bands on a z-score

Three-state mean-reversion position from a z-score: short above +entry, long below -entry, flat again inside the exit level, with an optional stop and lockout.

Kalman filter regression (dynamic hedge ratio)

Regression with a random-walk intercept and slope, estimated by a Kalman filter: the dynamic hedge ratio of a pairs trade, its spread, and the spread's predicted deviation.

Rolling augmented Dickey-Fuller statistic

Rolling augmented Dickey-Fuller t-statistic: how strongly a series, such as a pairs spread, reverts to a mean over the last window.

Rolling Ornstein-Uhlenbeck fit

Rolling MLE fit of a mean-reverting Ornstein-Uhlenbeck process.

Rolling spread

Rolling hedge-adjusted residual x - beta * y, with beta the rolling regression slope.

Rolling total least squares hedge ratio

Rolling total least squares (orthogonal regression) slope of the first input on the second: a hedge ratio that does not depend on which leg is called dependent.