portfolioBacktest: Backtest multiple portfolios over multiple datasets of stock...

View source: R/portfolioBacktest.R

portfolioBacktestR Documentation

Backtest multiple portfolios over multiple datasets of stock prices in a rolling-window basis

Description

Automated backtesting of multiple portfolios over multiple datasets of stock prices in a rolling-window fashion. Each portfolio design is easily defined as a function that takes as input a window of the stock prices and outputs the portfolio weights. Multiple portfolios can be easily specified as a list of functions or as files in a folder. Multiple datasets can be conveniently obtained with the function financialDataResample that resamples the data downloaded with the function stockDataDownload. The results can be later assessed and arranged with tables and plots. The backtesting can be highly time-consuming depending on the number of portfolios and datasets can be performed with parallel computation over multiple cores. Errors in functions are properly catched and handled so that the execution of the overal backtesting is not stopped (error messages are stored for debugging purposes). See vignette for a detailed explanation.

Usage

portfolioBacktest(
  portfolio_funs = NULL,
  dataset_list,
  folder_path = NULL,
  source_to_local = TRUE,
  price_name = NULL,
  paral_portfolios = 1,
  paral_datasets = 1,
  show_progress_bar = FALSE,
  benchmarks = NULL,
  shortselling = TRUE,
  leverage = Inf,
  lookback = 252,
  T_rolling_window = NULL,
  optimize_every = 20,
  rebalance_every = 1,
  bars_per_year = 252,
  execution = c("same period", "next period"),
  cost = list(buy = 0, sell = 0, short = 0, long_leverage = 0),
  cpu_time_limit = Inf,
  return_portfolio = TRUE,
  return_returns = TRUE
)

Arguments

portfolio_funs

List of functions (can also be a single function), each of them taking as input a dataset containing a list of xts objects (following the format of each element of the argument dataset_list) properly windowed (following the rolling-window approach) and returning the portfolio as a vector of normalized weights. See vignette for details.

dataset_list

List of datasets, each containing a list of xts objects, as generated by the function financialDataResample.

folder_path

If portfolio_funs is not defined, this should contain the path to a folder containing the portfolio functions saved in files. See vignette for details.

source_to_local

Logical value indicating whether to source files to local environment (default is TRUE). It might be dangerous to set it to FALSE as in such case the global environment may be changed. We suggest only to allow FALSE when the code in the source files does not work when locally sourced, e.g., with some versions of package CVXR. In that case, we further recommend to set paral_portfolios > 1 to avoid changing the global environment.

price_name

Name of the xts object in each dataset that contains the prices to be used in the portfolio return computation (default is the name of the first xts object).

paral_portfolios

Interger indicating number of portfolios to be evaluated in parallel (default is 1), see vignette-paralle-mode for details.

paral_datasets

Interger indicating number of datasets to be evaluated in parallel (default is 1), see vignette-paralle-mode for details.

show_progress_bar

Logical value indicating whether to show progress bar (default is FALSE).

benchmarks

String vector indicating the benchmark portfolios to be incorporated, currently supports:

  • 1/N - the 1/N portfolio, w = [1/N, ..., 1/N] with N be number of stocks;

  • IVP - the inverse-volatility portfolio, with weights be inversely proportional the standard deviation of returns;

  • index - the market index, requires an xts named 'index' in the datasets.

shortselling

Logical value indicating whether shortselling is allowed or not (default is TRUE, so no control for shorselling in the backtesting).

leverage

Amount of leverage as in ||w||_1 <= leverage (default is Inf, so no control for leverage in the backtesting).

lookback

Length of the lookback rolling window in periods (default is 252).

T_rolling_window

Deprecated: use lookback instead.

optimize_every

How often the portfolio is to be optimized in periods (default is 20).

rebalance_every

How often the portfolio is to be rebalanced in periods (default is 1).

bars_per_year

Number of bars/periods per year. By default it will be calculated automatically (e.g., for daily data there are 252 bars/periods per year).

execution

String that can be either "same period" (default) or "next period". At the rebalancing period t, the portfolio has used information up to (and including) period t. Same period execution means one can get into the position at that period t, whereas the next period execution means that one can only get into the position the following period.

cost

List containing four different types of transaction costs (common for all assets) for buying, selling, shorting, and long leveraging. The default is cost = list(buy = 0e-4, sell = 0e-4, short = 0e-4, long_leverage = 0e-4). If some elements are not specified then they will be automatically set to zero.

cpu_time_limit

Time limit for executing each portfolio function over a single data set (default is Inf, so no time limit).

return_portfolio

Logical value indicating whether to return the portfolios (default is TRUE). Three portfolio series are returned: w_optimized is the optimized portfolio at each given optimization period (using all the information up to and including that period, which can be executed either on the same period or the following period), w_rebalanced is the rebalanced portfolio at each given rebalancing period, and w_bop is the "beginning-of-period" portfolio (i.e., at each period it contains the weights held in the market in the previous period so that the portfolio return at that period is just the product of the asset returns and w_bop at that period.)

return_returns

Logical value indicating whether to return the portfolio returns (default is TRUE). Three series are returned: return with the portfolio returns, wealth with the portfolio wealth (aka cumulative P&L), and X_lin with the returns of the assets in the universe (note that the portfolio returns can also be obtained as rowSums(X_lin * w_bop) in the absence of transaction costs).

Value

List with the portfolio backtest results, see vignette-result-format for details. It can be accessed directly, but we highly recommend the use of the package specific functions to extract any required information, namely, backtestSelector, backtestTable, backtestBoxPlot, backtestLeaderboard, backtestSummary, summaryTable, summaryBarPlot.

Author(s)

Daniel P. Palomar and Rui Zhou

See Also

stockDataDownload, financialDataResample, backtestSelector, backtestTable, backtestBoxPlot, backtestLeaderboard, backtestSummary, summaryTable, summaryBarPlot.

Examples


library(portfolioBacktest)
data(dataset10)  # load dataset

# define your own portfolio function
ewp_portfolio <- function(dataset, ...) {
  N <- ncol(dataset$adjusted)
  return(rep(1/N, N))
}

# do backtest
bt <- portfolioBacktest(list("EWP" = ewp_portfolio), dataset10)

# check your result
names(bt)
backtestSelector(bt, portfolio_name = "EWP", measures = c("Sharpe ratio", "max drawdown"))
backtestTable(bt, measures = c("Sharpe ratio", "max drawdown"))
bt_summary <- backtestSummary(bt)
summaryTable(bt_summary)



dppalomar/portfolioBacktest documentation built on April 27, 2022, 3:27 a.m.