Learn R Programming

PortfolioTesteR (version 0.1.4)

ml_backtest_multi: Run multi-horizon ML backtests (pooled or sector-neutral)

Description

Convenience wrapper around ml_backtest() that repeats the same specification across multiple horizons, returning a named list of backtest objects keyed as "H1w", "H4w", "H13w", etc.

Usage

ml_backtest_multi(
  features_list,
  prices_weekly,
  horizons,
  fit_fn,
  predict_fn,
  schedule,
  transform = "zscore",
  selection = list(top_k = 20L),
  weighting = list(method = "softmax", temperature = 12),
  caps = list(max_per_symbol = 0.1),
  group_mode = c("pooled", "per_group"),
  group_map = NULL,
  initial_capital = 1e+05,
  name_prefix = "",
  seed = NULL,
  ...
)

Value

A named list of backtest objects (as returned by ml_backtest()), with names like "H1w", "H4w", … .

Arguments

features_list

Named list of data.tables with factor scores (each with a Date column and one column per symbol). Typically from ml_prepare_features().

prices_weekly

Wide price table (weekly) with Date + one column per symbol (adjusted prices). Used both to create labels and run the backtest.

horizons

Integer vector of horizons in weeks (e.g., c(1L,4L,13L)).

fit_fn, predict_fn

Model fit/predict closures as returned by ml_make_model().

schedule

Walk-forward schedule list with elements is, oos, step.

transform

Feature transform (default "zscore"). Passed to ml_backtest().

selection

List describing selection rules (e.g., list(top_k=20L, max_per_group=3L)).

weighting

List describing weighting rules (e.g., list(method="softmax", temperature=12)).

caps

List with exposure caps (e.g., list(max_per_symbol=0.10, max_per_group=NULL)).

group_mode

"pooled" or "per_group". If "per_group", you must pass group_map.

group_map

A two-column table with columns Symbol and Group defining the grouping (e.g., sectors) for "per_group" mode.

initial_capital

Numeric. Starting capital for the backtest (default 1e5).

name_prefix

Optional string prefixed to each backtest title.

seed

Optional integer. If provided, the same seed is set before each horizon’s backtest call to ensure deterministic tie-breaks.

...

Additional arguments forwarded to ml_backtest() (kept for future extensibility; no effect if unused).

Details

This function does not change core behavior; it only removes boilerplate when running identical specs across horizons and (optionally) grouping regimes. It preserves all defaults you pass for selection, weighting, transforms, caps, and schedule.

Examples

Run this code
# \donttest{
library(PortfolioTesteR)
data(sample_prices_weekly, package = "PortfolioTesteR")

# Minimal features for the example
X <- ml_prepare_features(
  prices_weekly = sample_prices_weekly,
  include = c("mom12","mom26")
)

# Simple deterministic model
model <- ml_make_model("linear")
sched <- list(is = 156L, oos = 4L, step = 4L)

set.seed(42)
bt_list <- ml_backtest_multi(
  features_list = X,
  prices_weekly = sample_prices_weekly,
  horizons = c(1L, 4L),
  fit_fn = model$fit,
  predict_fn = model$predict,
  schedule = sched,
  selection = list(top_k = 5L),
  weighting = list(method = "softmax", temperature = 12),
  caps = list(max_per_symbol = 0.10),
  group_mode = "pooled",
  name_prefix = "Demo ",
  seed = 42
)

names(bt_list)   # "H1w" "H4w"
# }

Run the code above in your browser using DataLab