Asset and portfolio performance analysis is a deep field with a wide range of theories and
methods for analyzing risk versus reward. The
consolidates many of the most widely used performance metrics as functions that can
be applied to stock or portfolio returns.
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()
The column of asset returns
The column of baseline returns (for functions that require comparison to a baseline)
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
that return portfolio statistics.
The main advantage is the ability to scale with the
Rb are the columns containing asset and baseline returns, respectively.
These columns are mapped to the
PerformanceAnalytics functions. Note that
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
function. Search the underlying function to see what arguments can be passed through.
tq_performance_fun_options returns a list of compatible
that can be supplied to the
Returns data in the form of a
tq_transmute() which can be used to calculate period returns from a
set of stock prices. Use
mutate_fun = periodReturn with the appropriate periodicity
period = "monthly".
tq_portfolio() which can be used to aggregate period returns from
multiple stocks to period returns for a portfolio.
PerformanceAnalytics package, which contains the underlying functions
performance_fun argument. Additional parameters can be passed via
# Load libraries library(tidyquant) library(dplyr) # Use FANG data set data(FANG) # 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.