quantlab.runs.backtest_attribution.rebalanced_group_values#

quantlab.runs.backtest_attribution.rebalanced_group_values(fill, valuation, scores, rebalance, groups, delisted=None)[source]#

Return the value curves of the score groups, each held equally weighted between rebalances.

On every rebalance bar t the universe is each symbol with a finite score on t and a fill price on t + 1. It is ranked by score (ties by symbol order) and cut into groups groups of equal count, the first holding the lowest scores; when it has fewer symbols than groups, every group holds cash until the next rebalance. Each group is bought equally weighted at the fill prices of t + 1 and held, valued at the valuation prices, until the fill bar of the next rebalance, where it is sold at the fill prices and the next group bought: no costs, and the same fill-a-bar-later timing as the simulation. A holding marked in delisted is settled as the engine settles it, at its last valuation price; any other holding without a later price keeps its last one. A group with no symbol holds its value flat. Each curve starts at 1.0 and stays there until the first fill.

Parameters:
fill, valuationarray-like

Fill and valuation prices on [T, S], NaN where a bar has none.

scoresarray-like

Scores on [T, S]; only the rebalance bars’ rows are read, and NaN leaves a symbol out of the universe.

rebalancearray-like

Booleans on [T]: the bars whose scores are traded.

groupsint

How many groups to cut the universe into; 1 gives the universe.

delistedarray-like, optional

Booleans on [T, S]: a symbol’s last bar before it delists, as the price dataset’s delisting_bars marks it.

Returns:
np.ndarray

[groups, T], the value of 1.0 invested in each group.

Raises:
ValueError

If groups is below 1 or the panels’ shapes differ.

Examples

>>> fill = np.array([[10.0, 20.0], [10.0, 20.0], [11.0, 22.0]])
>>> valuation = np.array([[10.0, 20.0], [12.0, 20.0], [12.0, 24.0]])
>>> scores = np.array([[1.0, 2.0], [np.nan, np.nan], [np.nan, np.nan]])
>>> rebalanced_group_values(fill, valuation, scores, [True, False, False], groups=2)
array([[1. , 1.2, 1.2],
       [1. , 1. , 1.2]])