Skip to main content

Backtest Windows

A BacktestWindow defines the historical periods evaluated by a Study. A window always has a training range and can also have a test range, which supports simple date ranges, holdouts, rolling tests, anchored tests, and walk-forward folds with the same model.

Define a window

from datetime import datetime, timezone

from investing_algorithm_framework import BacktestDateRange, BacktestWindow

window = BacktestWindow(
name="2024_q1_fold",
train_range=BacktestDateRange(
start_date=datetime(2023, 1, 1, tzinfo=timezone.utc),
end_date=datetime(2024, 1, 1, tzinfo=timezone.utc),
),
test_range=BacktestDateRange(
start_date=datetime(2024, 1, 1, tzinfo=timezone.utc),
end_date=datetime(2024, 4, 1, tzinfo=timezone.utc),
),
warmup_days=30,
fold_index=0,
)
FieldPurpose
nameHuman-readable identity for the window.
train_rangeRequired range used for training or a standalone run.
test_rangeOptional held-out range used for validation.
warmup_daysInitial days reserved for indicator initialization.
fold_indexStable fold identity for rolling or walk-forward studies.

Dates should be timezone-aware. The framework persists both ranges and the window metadata, so each run can be traced back to its intended evaluation period.

Choose which range runs

Set Study.window_part to control how its windows are executed:

  • WindowPart.TRAIN runs each training range.
  • WindowPart.TEST runs each test range and falls back to the training range when no test range exists. This is the default.
  • WindowPart.BOTH runs the training and test ranges separately.
from investing_algorithm_framework import Study, WindowPart

study = Study(
name="walk_forward_validation",
universe=universe,
backtest_windows=[window],
window_part=WindowPart.TEST,
)

Common window designs

DesignWindow pattern
Single periodOne train_range, no test_range.
HoldoutOne fixed training range followed by one test range.
RollingMove both range boundaries forward for each fold.
AnchoredKeep the training start fixed and extend its end for each fold.
Walk-forwardCreate ordered train/test pairs and assign fold_index.
Time out-of-samplePut later unseen dates in test ranges or a separate study.

Use multiple named windows when performance must be evaluated across regimes. Summaries can then aggregate the runs while preserving per-window metrics.

Warmup periods

warmup_days reserves the beginning of the training range for loading enough history to initialize indicators. It does not create a separate result window. Choose a warmup long enough for the strategy's largest lookback and keep it consistent when comparing variants.