calc_metric: Calculate Performance Metric

Description Usage Arguments Value Examples

View source: R/calc_metric.R

Description

Mainly a helper function for calc_metrics and calc_metrics_overtime, but could also be used independently.

Usage

1
calc_metric(gains, metric = "mean", units.year = 252, benchmark.gains = NULL)

Arguments

gains

Numeric vector.

metric

Character string specifying metric to calculate. Choices are "mean", "sd", "growth.x" for growth of $x where x is the initial value, "growth" for percent growth, "cagr" for compound annualized growth rate, "mdd" for max drawdown, "sharpe", "sortino", "alpha", "alpha.annualized", "beta", "r.squared", "pearson" or "spearman" for Pearson/Spearman correlation with benchmark, and "auto.pearson" or "auto.spearman" for Pearson/Spearman autocorrelation.

units.year

Integer value.

benchmark.gains

Numeric vector.

Value

Numeric value.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Not run: 
# Load daily gains for SPY in 2019 and calculate various metrics
gains <- load_gains(tickers = "SPY", from = "2019-01-01", to = "2019-12-31")
calc_metric(gains$SPY, "growth")
calc_metric(gains$SPY, "cagr")
calc_metric(gains$SPY, "mdd")
calc_metric(gains$SPY, "sharpe")
calc_metric(gains$SPY, "growth.10k")

# Calculate alpha and beta for TLT in 2019, using SPY as a benchmark
gains <- load_gains(tickers = c("SPY", "TLT"), from = "2019-01-01", to = "2019-12-31")
calc_metric(gains = gains$TLT, metric = "alpha", benchmark.gains = gains$SPY)
calc_metric(gains = gains$TLT, metric = "beta", benchmark.gains = gains$SPY)

## End(Not run)

vandomed/stocks documentation built on July 22, 2020, 3:25 a.m.