runMultipleBacktests: Run Multiple Portfolio Backtests and Plot

View source: R/runMultipleBacktests.R

runMultipleBacktestsR Documentation

Run Multiple Portfolio Backtests and Plot

Description

Runs n_simulations independent portfolio backtests, each time drawing a random subset of stocks from stock_list. For every simulation a full backtest is executed via runPortfolioBacktest(), and an individual plot can optionally be saved. After all simulations finish, the function averages the cumulative return series across simulations and can save a summary plot of those averaged returns.

Usage

runMultipleBacktests(
  n_simulations,
  portfolio_size,
  seed = NULL,
  return_portfolio,
  stock_list,
  buildPortfolios = buildPortfolios,
  market_return = NULL,
  rebalance_on = NULL,
  rolling_window = NULL,
  optimize_method = "CVXR",
  moment_list = NULL,
  save_plot = TRUE,
  plot_path = "./",
  plot_name = "backtest",
  plot_main = NULL,
  plotType = "both",
  save_avg_plot = TRUE,
  avg_plot_path = "./",
  avg_plot_name = "avg_backtest",
  avg_plot_main = NULL,
  avgPlotType = "both",
  colorSet = NULL,
  ltySet = NULL,
  lwdSet = NULL
)

Arguments

n_simulations

Integer. Number of independent backtest simulations to run.

portfolio_size

Integer. Number of stocks to randomly sample from stock_list for each simulation.

seed

Integer. Random seed passed to set.seed() before the simulation loop for reproducibility.

return_portfolio

An xts matrix of asset returns. Column names must match the assets in stock_list.

stock_list

Character vector. Universe of stock tickers from which portfolio_size tickers are drawn at random in each simulation.

buildPortfolios

A function that accepts a character vector of selected stock tickers and returns a named list of portfolio.spec objects, one per strategy. The names of the list elements are used as strategy labels in plot legends and all other outputs. Typically built with buildPortfolios and can be customized.

market_return

An xts single-column object of benchmark returns.

rebalance_on

Character string passed to optimize.portfolio.rebalancing(). See endpoints for valid names.

rolling_window

Positive integer. Length of the rolling estimation window in periods.

optimize_method

Character string specifying the solver. Default "CVXR".

moment_list

If different moment functions are passed into multiple GMV portfolios, please define each moment function via this parameter. For the portfolio that do not require moment function, please pass NULL. Example: list('custom.covRob.Rocke', NULL, NULL).

save_plot

Logical. Whether to save the plot to a PNG file. Default TRUE.

plot_path

Character string. Full file path for the each simulation output. Required when save_plot = TRUE.

plot_name

Plot name for each simulation output. Default "backtest"

plot_main

Plot title for each simulation PNG output.

plotType

"cumRet", "drawdown", or the default is "both"

save_avg_plot

Logical. Whether to save the the average cumulative returns plot to a PNG file. Default TRUE.

avg_plot_path

Character string. Full file path for the average simulation output. Required when save_avg_plot = TRUE.

avg_plot_name

Plot name for average simulation output. Default "avg_backtest".

avg_plot_main

Plot title for the average simulation PNG output.

avgPlotType

"cumRet", "drawdown", or the default is "both"

colorSet

Optional character vector of colors passed to backtest.plot().

ltySet

Optional integer vector of line types passed to backtest.plot().

lwdSet

Optional integer vector of line width passed to backtest.plot().

Value

A named list with four elements:

results

A list of length n_simulations. Each element contains the returns component returned by runPortfolioBacktest() for that simulation.

selected_stocks_all

A list of length n_simulations. Each element is a character vector of the tickers chosen for that simulation.

avg_algorithm_returns

An xts object containing the element-wise average of all simulations' algorithm return series.

avg_cumulative_return

An xts object containing the element-wise average of all simulations' cumulative return series.

Examples

args(runMultipleBacktests)

PCRA documentation built on July 15, 2026, 9:06 a.m.