-
Notifications
You must be signed in to change notification settings - Fork 12
/
README.Rmd
144 lines (111 loc) · 4.81 KB
/
README.Rmd
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
always_allow_html: yes
output:
html_document:
keep_md: yes
variant: markdown_github
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
library(knitr)
opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.align = "center",
fig.retina = 2,
out.width = "75%",
dpi = 96
)
knit_hooks$set(pngquant = hook_pngquant)
```
# portfolioBacktest
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/portfolioBacktest)](https://CRAN.R-project.org/package=portfolioBacktest)
[![CRAN Downloads](https://cranlogs.r-pkg.org/badges/portfolioBacktest)](https://CRAN.R-project.org/package=portfolioBacktest)
[![CRAN Downloads Total](https://cranlogs.r-pkg.org/badges/grand-total/portfolioBacktest?color=brightgreen)](https://CRAN.R-project.org/package=portfolioBacktest)
Automated backtesting of multiple portfolios over multiple
datasets of stock prices in a rolling-window fashion. Intended for
researchers and practitioners to backtest a set of different portfolios,
as well as by a course instructor to assess the students in their portfolio
design in a fully automated and convenient manner, with results conveniently
formatted in tables and plots. 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
extracted randomly from different markets, different time periods, and
different subsets of the stock universe. The results can be later assessed
and ranked with tables based on a number of performance criteria (e.g.,
expected return, volatility, Sharpe ratio, drawdown, turnover rate, return
on investment, computational time, etc.), as well as plotted in a number of
ways with nice barplots and boxplots.
## Installation
The package can be installed from [CRAN](https://CRAN.R-project.org/package=portfolioBacktest) or [GitHub](https://github.com/dppalomar/portfolioBacktest):
```{r, eval=FALSE}
# install stable version from CRAN
install.packages("portfolioBacktest")
# install development version from GitHub
devtools::install_github("dppalomar/portfolioBacktest")
```
To get help:
```{r, eval=FALSE}
library(portfolioBacktest)
help(package = "portfolioBacktest")
?portfolioBacktest
```
To cite `portfolioBacktest` in publications:
```{r, eval=FALSE}
citation("portfolioBacktest")
```
## Quick Start
Do the backtest on your own portfolio following few steps:
- **Step 1** - load package & 10 datasets
```{r, message=FALSE, results="hide"}
library(portfolioBacktest)
data("dataset10")
```
- **Step 2** - define your own portfolio
```{r}
my_portfolio <- function(dataset, w_current) {
prices <- dataset$adjusted
N <- ncol(prices)
return(rep(1/N, N))
}
```
- **Step 3** - do backtest
```{r, message=FALSE, results="hide"}
bt <- portfolioBacktest(my_portfolio, dataset10)
```
- **Step 4** - check your portfolio performance (e.g., median of the 10 individual backtests)
```{r}
backtestSummary(bt)$performance
```
For a more detailed explanation on how to use the package with all the features, check the [vignette](https://CRAN.R-project.org/package=portfolioBacktest/vignettes/PortfolioBacktest.html).
## Package Snapshot
This package backtests a list of portfolios over multiple datasets on a rolling-window basis, producing final results as in the following.
- Performance table:
```{r README-table, echo=FALSE, message=FALSE, out.width="95%"}
#library(portfolioBacktest)
#load("man/figures/bt.RData")
#res_sum <- backtestSummary(bt)
#summaryTable(res_sum, type = "DT", order_col = 2, order_dir = "desc")
knitr::include_graphics("man/figures/README-table.png")
```
- Barplot:
```{r README-barplot, echo=FALSE, out.width="70%"}
#summaryBarPlot(res_sum, measures = c("Sharpe ratio", "max drawdown"), type = "ggplot2")
knitr::include_graphics("man/figures/README-barplot.png")
```
- Boxplot:
```{r README-boxplot, echo=FALSE, out.width="70%"}
#backtestBoxPlot(bt, measure = "Sharpe ratio")
knitr::include_graphics("man/figures/README-boxplot.png")
```
## Documentation
For more detailed information, please check the
[vignette](https://CRAN.R-project.org/package=portfolioBacktest/vignettes/PortfolioBacktest.html).
## Links
Package: [CRAN](https://CRAN.R-project.org/package=portfolioBacktest) and [GitHub](https://github.com/dppalomar/portfolioBacktest).
README file: [GitHub-readme](https://github.com/dppalomar/portfolioBacktest/blob/master/README.md).
Vignette: [CRAN-vignette](https://CRAN.R-project.org/package=portfolioBacktest/vignettes/PortfolioBacktest.html) and [GitHub-vignette](https://htmlpreview.github.io/?https://github.com/dppalomar/portfolioBacktest/blob/master/vignettes/PortfolioBacktest.html).