View source: R/tq_performance.R
| tq_performance | R Documentation |
Asset and portfolio performance analysis is a deep field with a wide range of theories and
methods for analyzing risk versus reward. The PerformanceAnalytics package
consolidates many of the most widely used performance metrics as functions that can
be applied to stock or portfolio returns. tq_performance
implements these performance analysis functions in a tidy way, enabling scaling
analysis using the split, apply, combine framework.
tq_performance(data, Ra, Rb = NULL, performance_fun, ...)
tq_performance_(data, Ra, Rb = NULL, performance_fun, ...)
tq_performance_fun_options()
data |
A |
Ra |
The column of asset returns |
Rb |
The column of baseline returns (for functions that require comparison to a baseline) |
performance_fun |
The performance function from |
... |
Additional parameters passed to the |
Important concept: Performance is based on the statistical properties of returns, and as a result this function uses stock or portfolio returns as opposed to stock prices.
tq_performance is a wrapper for various PerformanceAnalytics functions
that return portfolio statistics.
The main advantage is the ability to scale with the tidyverse.
Ra and Rb are the columns containing asset and baseline returns, respectively.
These columns are mapped to the PerformanceAnalytics functions. Note that Rb
is not always required, and in these instances the argument defaults to Rb = NULL.
The user can tell if Rb is required by researching the underlying performance function.
... are additional arguments that are passed to the PerformanceAnalytics
function. Search the underlying function to see what arguments can be passed through.
tq_performance_fun_options returns a list of compatible PerformanceAnalytics functions
that can be supplied to the performance_fun argument.
Returns data in the form of a tibble object.
tq_transmute() which can be used to calculate period returns from a
set of stock prices. Use mutate_fun = periodReturn with the appropriate periodicity
such as period = "monthly".
tq_portfolio() which can be used to aggregate period returns from
multiple stocks to period returns for a portfolio.
The PerformanceAnalytics package, which contains the underlying functions
for the performance_fun argument. Additional parameters can be passed via ....
# Load libraries
library(dplyr)
# Use FANG data set
# Get returns for individual stock components grouped by symbol
Ra <- FANG %>%
group_by(symbol) %>%
tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "Ra")
# Get returns for SP500 as baseline
Rb <- "^GSPC" %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31") %>%
tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "Rb")
# Merge stock returns with baseline
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
##### Performance Metrics #####
# View options
tq_performance_fun_options()
# Get performance metrics
RaRb %>%
tq_performance(Ra = Ra, performance_fun = SharpeRatio, p = 0.95)
RaRb %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.