View source: R/backtest_allocation.R
backtest_allocation | R Documentation |
backtest_allocation
computes a backtest of a given portfolio
allocation rule.
backtest_allocation(strat, P, R, risk_free = 0, start_date = NULL)
strat |
A list representing an asset allocation strategy. |
P |
An xts object with daily prices of the tickers in strat. |
R |
An xts object with daily returns of the tickers in strat. |
risk_free |
Either an xts object with daily returns of the risk-free asset, or a scalar numeric with the annual risk-free rate in decimals. |
start_date |
Optional starting date |
The function first determines the rebalancing dates based
on strat$rebalance_frequency
. Then, it cycles through intermediate
dates and calculates daily returns based on the allocation. If the optional
parameter start_date
is provided, the backtest will start on that
date. Otherwise, it will start from the date from which data on all assets
becomes available.
An object of class "List"
with the following elements:
strat |
The strat provided to the function |
returns |
An xts object with the daily returns of the strategy |
table_performance |
A table with performance metrics |
rebalance_dates |
Vector of rebalancing dates |
rebalance_weights |
Vector of rebalancing dates |
# Example 1: backtesting one of the asset allocations in the package
us_60_40 <- asset_allocations$static$us_60_40
bt_us_60_40 <- backtest_allocation(us_60_40,
ETFs$Prices,
ETFs$Returns,
ETFs$risk_free)
# show table with performance metrics
bt_us_60_40$table_performance
# Example 2: creating and backtesting an asset allocation from scratch
# create a strategy that invests equally in momentum (MTUM), value (VLUE),
# low volatility (USMV) and quality (QUAL) ETFs.
factor_strat <- list(name = "EW Factors",
tickers = c("MTUM", "VLUE", "USMV", "QUAL"),
default_weights = c(0.25, 0.25, 0.25, 0.25),
rebalance_frequency = "month",
portfolio_rule_fn = "constant_weights")
# get data for tickers using getSymbols
factor_ETFs <- get_data_from_tickers(factor_strat$tickers,
starting_date = "2020-01-01")
# backtest the strategy
bt_factor_strat <- backtest_allocation(factor_strat,
factor_ETFs$P,
factor_ETFs$R)
# show table with performance metrics
bt_factor_strat$table_performance
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.