summaryTable: Create table from backtest summary

View source: R/plotsNtables.R

summaryTableR Documentation

Create table from backtest summary

Description

After performing a backtest with portfolioBacktest and obtaining a summary of the performance measures with backtestSummary, this function creates a table from the summary. By default the table is a simple matrix, but if the user has installed the package DT or grid.table nicer tables can be generated.

Usage

summaryTable(
  bt_summary,
  measures = NULL,
  caption = "Performance table",
  type = c("simple", "DT", "kable", "grid.table"),
  digits = 2,
  order_col = NULL,
  order_dir = c("asc", "desc"),
  page_length = 10
)

Arguments

bt_summary

Backtest summary as obtained from the function backtestSummary.

measures

String vector to select performane measures (default is all) from 'Sharpe ratio', 'max drawdown', 'annual return', 'annual volatility', 'Sterling ratio', 'Omega ratio', 'ROT bps', etc.

caption

Table caption (only works for type = "DT").

type

Type of table. Valid options: "simple", "DT", "kable", "grid.table". Default is "simple" and generates a simple matrix (with the other choices the corresponding package must be installed).

digits

Integer indicating the number of decimal places when rounding (default is 2).

order_col

Column number or column name of the performance measure to be used to sort the rows (only used for table type = "DT"). By default the last column will be used.

order_dir

Direction to be used to sort the rows (only used for table type = "DT"). Valid options: "asc", "desc". Default is "asc".

page_length

Page length for the table (only used for table type = "DT"). Default is 10.

Author(s)

Daniel P. Palomar and Rui Zhou

See Also

summaryBarPlot

Examples

 
library(portfolioBacktest)
data(dataset10)  # load dataset

# define your own portfolio function
quintile_portfolio <- function(data, ...) {
  X <- diff(log(data$adjusted))[-1]  
  N <- ncol(X)
  ranking <- sort(colMeans(X), decreasing = TRUE, index.return = TRUE)$ix
  w <- rep(0, N)
  w[ranking[1:round(N/5)]] <- 1/round(N/5)
  return(w)
}

# do backtest
bt <- portfolioBacktest(list("Quintile" = quintile_portfolio), 
                        dataset10,
                        benchmark = c("1/N", "index"))

# now we can obtain the table
bt_summary_median <- backtestSummary(bt)
summaryTable(bt_summary_median, measures = c("max drawdown", "annual volatility"))
summaryTable(bt_summary_median, measures = c("max drawdown", "annual volatility"), type = "DT")
summaryTable(bt_summary_median, type = "kable") 
# this returned kable object can be combined with: " |> kableExtra::kable_styling()"



dppalomar/portfolioBacktest documentation built on April 27, 2022, 3:27 a.m.