Learn R Programming

PortfolioTesteR (version 0.1.4)

ml_backtest: One-call backtest wrapper (tabular features)

Description

One-call backtest wrapper (tabular features)

Usage

ml_backtest(
  features_list,
  labels,
  fit_fn,
  predict_fn,
  schedule = list(is = 156L, oos = 4L, step = 4L),
  group = c("pooled", "per_symbol", "per_group"),
  transform = c("none", "zscore", "rank"),
  selection = list(top_k = 15L, max_per_group = NULL),
  group_map = NULL,
  weighting = list(method = "softmax", temperature = 12, floor = 0),
  caps = list(max_per_symbol = 0.1, max_per_group = NULL),
  turnover = NULL,
  prices,
  initial_capital = 1e+05,
  name = "ML backtest"
)

Value

list: scores, mask, weights, backtest.

Arguments

features_list

list of wide panels (each: Date + symbols).

labels

Wide label panel (Date + symbols).

fit_fn

function (X, y) -> model trained on in-sample stacked rows.

predict_fn

function (model, Xnew) -> numeric scores.

schedule

list with elements is, oos, step.

group

one of "pooled", "per_symbol", "per_group".

transform

"none", "zscore", "rank" for per-date score transform.

selection

list: top_k, max_per_group (optional).

group_map

optional data.frame(Symbol, Group) if group = "per_group".

weighting

list: method, temperature, floor.

caps

list: max_per_symbol, optionally max_per_group.

turnover

Optional turnover cap settings (currently advisory/unused).

prices

price panel used by the backtester (Date + symbols).

initial_capital

starting capital.

name

string for the backtest result.

Examples

Run this code
# \donttest{
data(sample_prices_weekly); data(sample_prices_daily)
mom <- panel_lag(calc_momentum(sample_prices_weekly, 12), 1)
vol <- panel_lag(align_to_timeframe(
  calc_rolling_volatility(sample_prices_daily, 20),
  sample_prices_weekly$Date, "forward_fill"), 1)
Y <- make_labels(sample_prices_weekly, 4, "log")
fit_lm  <- function(X,y){ Xc <- cbind(1,X); list(coef=stats::lm.fit(Xc,y)$coefficients) }
pred_lm <- function(m,X){ as.numeric(cbind(1,X) %*% m$coef) }
res <- ml_backtest(list(mom=mom, vol=vol), Y, fit_lm, pred_lm,
                   schedule = list(is=52,oos=4,step=4),
                   transform = "zscore",
                   selection = list(top_k=10),
                   weighting = list(method="softmax", temperature=12),
                   caps = list(max_per_symbol=0.10),
                   prices = sample_prices_weekly, initial_capital = 1e5)
res$backtest
# }

Run the code above in your browser using DataLab