add_performance: Add a new performance measure to backtests

Description Usage Arguments Value Author(s) Examples

View source: R/portfolioBacktest.R

Description

Add a new performance measure to backtests

Usage

1
add_performance(bt, name, fun, desired_direction = 1)

Arguments

bt

Backtest results as produced by the function portfolioBacktest.

name

String with name of new performance measure.

fun

Function to compute new performance measure from any element returned by portfolioBacktest, e.g., return, wealth, and w_bop.

desired_direction

Number indicating whether the new measure is desired to be larger (1), which is the default, or smaller (-1).

Value

List with the portfolio backtest results, see portfolioBacktest.

Author(s)

Daniel P. Palomar and Rui Zhou

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
library(portfolioBacktest)
data(dataset10)  # load dataset

# define your own portfolio function
uniform_portfolio <- function(dataset) {
  N <- ncol(dataset$adjusted)
  return(rep(1/N, N))
}

# do backtest
bt <- portfolioBacktest(list("Uniform" = uniform_portfolio), dataset10)

# add a new performance measure
bt <- add_performance(bt, name = "SR arithmetic", 
                      fun = function(return, ...) 
                               PerformanceAnalytics::SharpeRatio.annualized(return, 
                                                                            geometric = FALSE))
                               
bt <- add_performance(bt, name = "avg leverage", desired_direction = -1,
                      fun = function(w_bop, ...)
                               if(anyNA(w_bop)) NA else mean(rowSums(abs(w_bop))))

dppalomar/portfolioBacktest documentation built on Aug. 8, 2020, 1:11 a.m.