add_performance: Add a new performance measure to backtests

View source: R/performance_portfolios.R

add_performanceR Documentation

Add a new performance measure to backtests

Description

Add a new performance measure to backtests

Usage

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

Examples


library(portfolioBacktest)
data(dataset10)  # load dataset

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

# do backtest
bt <- portfolioBacktest(list("EWP" = EWP_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 April 27, 2022, 3:27 a.m.