backtestChartStackedBar: Chart of the weight allocation over time for a portfolio over...

View source: R/plotsNtables.R

backtestChartStackedBarR 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 portfolioBacktest.

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 dataset_num = 1.

num_bars

Number of bars shown over time (basically a downsample of the possibly long sequence).

type

Type of plot. Valid options: "ggplot2", "simple". Default is "ggplot2".

legend

Boolean to choose whether legend is plotted or not. Default is legend = FALSE.

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)



portfolioBacktest documentation built on April 22, 2022, 9:05 a.m.