portfolioBacktest {portfolioBacktest} | R 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 |
dataset_list |
List of datasets, each containing a list of |
folder_path |
If |
source_to_local |
Logical value indicating whether to source files to local environment (default is |
price_name |
Name of the |
paral_portfolios |
Interger indicating number of portfolios to be evaluated in parallel (default is |
paral_datasets |
Interger indicating number of datasets to be evaluated in parallel (default is |
show_progress_bar |
Logical value indicating whether to show progress bar (default is |
benchmarks |
String vector indicating the benchmark portfolios to be incorporated, currently supports:
|
shortselling |
Logical value indicating whether shortselling is allowed or not
(default is |
leverage |
Amount of leverage as in |
lookback |
Length of the lookback rolling window in periods (default is |
T_rolling_window |
Deprecated: use |
optimize_every |
How often the portfolio is to be optimized in periods (default is |
rebalance_every |
How often the portfolio is to be rebalanced in periods (default is |
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 |
cost |
List containing four different types of transaction costs (common for all assets)
for buying, selling, shorting, and long leveraging. The default is
|
cpu_time_limit |
Time limit for executing each portfolio function over a single data set
(default is |
return_portfolio |
Logical value indicating whether to return the portfolios (default is |
return_returns |
Logical value indicating whether to return the portfolio returns (default is |
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)