-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathREADME.Rmd
More file actions
88 lines (65 loc) · 2.56 KB
/
README.Rmd
File metadata and controls
88 lines (65 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# AssetAllocation
<!-- badges: start -->
<!-- badges: end -->
The goal of AssetAllocation is to perform backtesting of customizable asset allocation strategies. The main function that the user interacts with is backtest_allocation().
## Installation
You can install the development version of AssetAllocation from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("rubetron/AssetAllocation")
```
## Example
Simple example using pre-loaded strategy (see the vignette for other examples):
```{r example1}
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:
```{r example2}
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
```