compute_long_short_returns: Compute Long-Short Returns

View source: R/compute_long_short_returns.R

compute_long_short_returnsR Documentation

Compute Long-Short Returns

Description

Calculates long-short returns based on the returns of portfolios. The long-short return is computed as the difference between the returns of the "top" and "bottom" portfolios. The direction of the calculation can be adjusted based on whether the return from the "bottom" portfolio is subtracted from or added to the return from the "top" portfolio.

Usage

compute_long_short_returns(
  data,
  direction = "top_minus_bottom",
  data_options = NULL
)

Arguments

data

A data frame of portfolio returns as returned by compute_portfolio_returns(). It must include the portfolio identifier and date columns (named via data_options) and at least one of the return columns ret_excess_vw, ret_excess_ew, and ret_excess_vw_capped.

direction

A character string specifying the direction of the long-short return calculation. It can be either "top_minus_bottom" or "bottom_minus_top". Default is "top_minus_bottom". If set to "bottom_minus_top", the return will be computed as (bottom - top).

data_options

A list of class tidyfinance_data_options (created via data_options()) specifying column name mappings. The date element specifies the date column and portfolio the assigned portfolio. Uses data_options() default if NULL: "date" = "date" and "portfolio" = "portfolio".

Value

A data frame with the date column and one column per portfolio return measure present in data (ret_excess_vw, ret_excess_ew, and ret_excess_vw_capped), each holding the long-short return for that measure, arranged by date.

See Also

Other portfolio functions: assign_portfolio(), breakpoint_options(), compute_breakpoints(), compute_portfolio_returns(), data_options(), filter_options(), filter_sorting_data(), implement_portfolio_sort(), portfolio_sort_options()

Examples

set.seed(42)
data <- data.frame(
  permno = 1:100,
  date = rep(
    seq.Date(from = as.Date("2020-01-01"), by = "month", length.out = 100),
    each = 10
  ),
  mktcap_lag = runif(100, 100, 1000),
  ret_excess = rnorm(100),
  size = runif(100, 50, 150)
)

portfolio_returns <- compute_portfolio_returns(
  data, "size", "univariate",
  breakpoint_options_main = breakpoint_options(n_portfolios = 5)
)

compute_long_short_returns(portfolio_returns)


tidyfinance documentation built on Oct. 26, 2026, 5:06 p.m.