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

Description Usage Arguments Value Author(s) See Also Examples

View source: R/portfolioBacktest.R

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 stockDataResample 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
portfolioBacktest(
  portfolio_funs = NULL,
  dataset_list,
  folder_path = NULL,
  price_name = "adjusted",
  paral_portfolios = 1,
  paral_datasets = 1,
  show_progress_bar = FALSE,
  benchmark = NULL,
  shortselling = TRUE,
  leverage = Inf,
  T_rolling_window = 252,
  optimize_every = 20,
  rebalance_every = 1,
  execution = c("same day", "next day"),
  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 stockDataResample.

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.

price_name

Name of the xts column in each dataset that contains the prices to be used in the portfolio return computation (default is "adjusted").

paral_portfolios

Interger indicating number of portfolios to be evaluated in parallel (default is 1).

paral_datasets

Interger indicating number of datasets to be evaluated in parallel (default is 1).

show_progress_bar

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

benchmark

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

  • uniform - the uniform 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).

T_rolling_window

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

optimize_every

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

rebalance_every

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

execution

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

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). Two portfolios are returned: w_designed is the designed portfolio at each given rebalancing period (using all the information up to and including that period, which can be executed either on the same day or the following day) 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). Two series are returned: return with the portfolio returns and wealth with the portfolio wealth (aka cumulative P&L).

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, stockDataResample, backtestSelector, backtestTable, backtestBoxPlot, backtestLeaderboard, backtestSummary, summaryTable, summaryBarPlot.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
library(portfolioBacktest)
data(dataset10)  # load dataset

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

# do backtest
bt <- portfolioBacktest(list("Uniform" = uniform_portfolio), dataset10)

# check your result
names(bt)
backtestSelector(bt, portfolio_name = "Uniform", 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 Sept. 25, 2020, 2:44 p.m.