knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of AssetAllocation is to perform backtesting of customizable asset allocation strategies. The main function that the user interacts with is backtest_allocation().
You can install the development version of AssetAllocation from GitHub with:
# install.packages("devtools") devtools::install_github("rubetron/AssetAllocation")
Simple example using pre-loaded strategy (see the vignette for other examples):
library(AssetAllocation) ## Example 1: backtesting one of the asset allocations in the package us_60_40 <- asset_allocations$static$us_60_40 # test using the data set provided in the package bt_us_60_40 <- backtest_allocation(us_60_40, ETFs$Prices, ETFs$Returns, ETFs$risk_free) # plot returns library(PerformanceAnalytics) chart.CumReturns(bt_us_60_40$returns, main = paste0("Backtest of the ", bt_us_60_40$strat$name, " portfolio"), ylab = "Cumulative returns" ) # show table with performance metrics bt_us_60_40$table_performance
Another example creating a strategy from scratch, retrieving data from Yahoo Finance, and backtesting:
library(AssetAllocation) # create a strategy that invests equally in momentum (MTUM), value (VLUE), low volatility (USMV) and quality (QUAL) ETFs. factors_EW <- 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_data <- get_data_from_tickers(factors_EW$tickers, starting_date = "2013-08-01") # backtest the strategy bt_factors_EW <- backtest_allocation(factors_EW,factor_ETFs_data$P, factor_ETFs_data$R) # plot returns charts.PerformanceSummary(bt_factors_EW$returns, main = bt_factors_EW$strat$name, ) # table with performance metrics bt_factors_EW$table_performance
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.