delay

Defined in fynance.portfolio.rebalance

delay(W, steps=1)[source]

Shift a weight book forward by a fixed number of bars.

Delays execution by steps bars: row t of the output is the target of row t - steps (the first steps rows are zero, i.e. flat). This generalizes the one-bar causal shift the backtest engine applies by default (shift=True) — use it to model a longer decision-to-fill lag, or to pre-shift a book fed to backtest(..., shift=False).

Parameters:
Warray_like

Weight book, shape (T, N) (1-D promoted to (T, 1), output squeezed back). Long-short books are supported.

stepsint, optional

Number of bars to delay; must be >= 0. steps=0 returns a copy unchanged; steps >= T returns an all-zero book. Default 1.

Returns:
np.ndarray

The shifted book, same shape as W.

Raises:
ValueError

If W is not 1-D or 2-D, contains non-finite values, or steps < 0.

Examples

>>> import numpy as np
>>> W = np.array([[0.5, 0.5], [1.0, 0.0], [0.0, 1.0]])
>>> delay(W, steps=1)
array([[0. , 0. ],
       [0.5, 0.5],
       [1. , 0. ]])