quantlab.model.split.walk_forward_folds#

quantlab.model.split.walk_forward_folds(timestamps, train_periods, test_periods=None, expanding=False, purge_bars=0)[source]#

Return the walk-forward folds of a timestamp axis.

Parameters:
timestampsarray-like of datetime64

The sorted bars to split, already cut to the caller’s date range.

train_periodsint

Bars in each training window (the first one, when expanding).

test_periodsint, optional

Bars in each test window. Defaults to train_periods // 5.

expandingbool, default False

Start every training window at the first bar instead of sliding it.

purge_barsint, default 0

Bars dropped from the end of each training window before fitting: the largest lookahead_bars() of the labels.

Returns:
tuple[Fold, …]

The folds in order. Empty when the axis is too short for one.

Raises:
ValueError

If test_periods is given and below 1, or not given and train_periods is below 5 (the one-fifth test window would be empty); if purge_bars is negative; or if the purge leaves a fold no training bar.

Examples

>>> import numpy as np
>>> bars = np.arange("2024-01-01", "2024-01-21", dtype="datetime64[D]")
>>> [fold.test_window for fold in walk_forward_folds(bars, 10)]
[('2024-01-11', '2024-01-12'), ('2024-01-13', '2024-01-14'), ('2024-01-15', '2024-01-16'), ('2024-01-17', '2024-01-18'), ('2024-01-19', '2024-01-20')]
>>> {fold.train_window[0] for fold in walk_forward_folds(bars, 10, expanding=True)}
{'2024-01-01'}