backtestChartStackedBar | R Documentation |
Create chart of the weight allocation over time for a portfolio over a single
backtest obtained with the function portfolioBacktest
.
By default the chart is based on the package ggplot2
, but the user can also
specify a plot based on PerformanceAnalytics
.
backtestChartStackedBar( bt, portfolio = names(bt[1]), dataset_num = 1, num_bars = 100, type = c("ggplot2", "simple"), legend = FALSE )
bt |
Backtest results as produced by the function |
portfolio |
String with portfolio name to be charted. Default charts the first portfolio in the backtest. |
dataset_num |
Dataset index to be charted. Default is |
num_bars |
Number of bars shown over time (basically a downsample of the possibly long sequence). |
type |
Type of plot. Valid options: |
legend |
Boolean to choose whether legend is plotted or not. Default is |
Daniel P. Palomar and Rui Zhou
summaryBarPlot
, backtestBoxPlot
,
backtestChartCumReturn
, backtestChartDrawdown
library(portfolioBacktest) data(dataset10) # load dataset # for better illustration, let's use only the first 5 stocks dataset10_5stocks <- lapply(dataset10, function(x) {x$adjusted <- x$adjusted[, 1:5]; return(x)}) # define GMVP (with heuristic not to allow shorting) GMVP_portfolio_fun <- function(dataset, ...) { X <- diff(log(dataset$adjusted))[-1] # compute log returns Sigma <- cov(X) # compute SCM # design GMVP w <- solve(Sigma, rep(1, nrow(Sigma))) w <- abs(w)/sum(abs(w)) return(w) } # backtest bt <- portfolioBacktest(list("GMVP" = GMVP_portfolio_fun), dataset10_5stocks, rebalance_every = 20) # now we can chart backtestChartStackedBar(bt, "GMVP", type = "simple") backtestChartStackedBar(bt, "GMVP", type = "simple", legend = TRUE) backtestChartStackedBar(bt, "GMVP") backtestChartStackedBar(bt, "GMVP", legend = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.