The xtsanalytics package provides a set of function to analyze and develop investment strategies, including:
The package also leverages and extends several finance related packages. These packages should be installed since xtsanalytics requires them.
xtsanalytics makes extensive use of the xts data structure. In addition, a new data structure is created to easily build statistical equity curves of a portfolio
using Walk-Forward optimization (WFO). This is called the statfolio data structure.
There may be different classes of statfolio depending on which function was used to generate it. For example, function optimize_statfolio() creates a statfolio of class "optimize.portfolio.rebalancing".
WFO optimizes an equity curve at a series of specific points in time using a rolling window. For example, a typical use would be to optimize monthly, at the end of each month. In doing so, the data window used is N days up to and including the WFO date, resulting in an equity curve up to the current WFO date. The following month, the same is repeated to generate the section of equity curve from the previous WFO date up to and including the current WFO date. For example, for a 10 year equity curve, this would result in 10 years * 12 months = 120 WFO optimizations if done monthly. Each of these blocks of time (between two consecutive WFO dates) can be used to create a subfolio.
A subfolio consists of multiple optimizations (or models) performed between two consecutive WFO dates. For instance, a portfolio optimizer (see optimize_statfolio) may converge only to an approximate set of portfolio weights. In such a case, it may be prudent to run several optimizations with different seeds in order to get a set of weights. The average of these weights could then be used in a real portfolio. In addition, the distribution of these weights can be analyzed to see how much variance exists between these optimization results, and reach conclusions regarding the importance of rebalancing to accurate weights. This set of multiple weights is the subfolio.
The statfolio is defined to be the set of all subfolios resulting from a WFO run with more than a single optimization performed as each WFO dates.
A statfolio can be used to generate equity curve statistics. For example, let's assume that wfo_statfolio() is run on a set of assets, with argument N = 5. Setting N = 5 means that the optimization is repeated 5 times at each WFO dates. Let's further assume monthly optization.
If the WFO optimization is done over 12 months, there will be 5^12 = 244 million possible equity curve paths, since for every month, there are 5 possible paths to choose! Function stitch_statfolio() can be used to generate a large number of such randomly picked equity curve paths to see the statistical effects on the equity curve resulting from the optimizer approximations.
Note the various macros within the vignette
setion of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the title
field and the \VignetteIndexEntry
to match the title of your vignette.
The html_vignette
template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows:
output: rmarkdown::html_vignette: css: mystyles.css
The figure sizes have been customised so that you can easily put two images side-by-side.
#plot(1:10) #plot(10:1)
You can enable figure captions by fig_caption: yes
in YAML:
output: rmarkdown::html_vignette: fig_caption: yes
Then you can use the chunk option fig.cap = "Your figure caption."
in knitr.
You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using knitr::kable()
.
#knitr::kable(head(mtcars, 10))
Also a quote using >
:
"He who gives up [code] safety for [code] speed deserves neither." (via)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.