backtestChartStackedBar {portfolioBacktest} | R Documentation |
Chart of the weight allocation over time for a portfolio over a single backtest
Description
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
.
Usage
backtestChartStackedBar(
bt,
portfolio = names(bt[1]),
dataset_num = 1,
num_bars = 100,
type = c("ggplot2", "simple"),
legend = FALSE
)
Arguments
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 |
Author(s)
Daniel P. Palomar and Rui Zhou
See Also
summaryBarPlot
, backtestBoxPlot
,
backtestChartCumReturn
, backtestChartDrawdown
Examples
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)