% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/implement_portfolio_sort.R
\name{implement_portfolio_sort}
\alias{implement_portfolio_sort}
\title{Implement Portfolio Sort}
\usage{
implement_portfolio_sort(
  data,
  sorting_variables,
  sorting_method,
  rebalancing_month = NULL,
  portfolio_sort_options,
  breakpoint_function_main = compute_breakpoints,
  breakpoint_function_secondary = compute_breakpoints,
  min_portfolio_size = 1L,
  cap_weight = 0.8,
  data_options = NULL,
  quiet = FALSE
)
}
\arguments{
\item{data}{A data frame containing the stock-level panel data.}

\item{sorting_variables}{A character vector of one or two column names to
sort portfolios on.}

\item{sorting_method}{A string specifying the sorting method:
\code{"univariate"}, \code{"bivariate-dependent"}, or \code{"bivariate-independent"}.}

\item{rebalancing_month}{An optional integer specifying the month in which
portfolios are rebalanced annually. \code{NULL} (the default) means monthly
rebalancing.}

\item{portfolio_sort_options}{A list of class
\code{tidyfinance_portfolio_sort_options} created by
\code{\link[=portfolio_sort_options]{portfolio_sort_options()}}, bundling filter and breakpoint
specifications. The arguments accepted by \code{\link[=portfolio_sort_options]{portfolio_sort_options()}}
include
\itemize{
\item \code{filter_options} A list of class \code{tidyfinance_filter_options}
created by \code{\link[=filter_options]{filter_options()}}, or \code{NULL} (the default, which applies
no filters). Options include \code{exclude_financials},
\code{exclude_utilities}, \code{min_stock_price}, \code{min_size_quantile},
\code{min_listing_age}, \code{exclude_negative_book_equity}, and
\code{exclude_negative_earnings}.
\item \code{breakpoint_options_main} A list of class
\code{tidyfinance_breakpoint_options} created by \code{\link[=breakpoint_options]{breakpoint_options()}},
specifying breakpoints for the primary sorting variable. Options
include \code{n_portfolios}, \code{percentiles}, \code{breakpoints_exchanges},
\code{smooth_bunching}, and \code{breakpoints_min_size_threshold}.
\item \code{breakpoint_options_secondary} A list of class
\code{tidyfinance_breakpoint_options} created by \code{\link[=breakpoint_options]{breakpoint_options()}},
specifying breakpoints for the secondary sorting variable, or \code{NULL}
(the default) for univariate sorts. Options are the same as for
\code{breakpoint_options_main}.
}}

\item{breakpoint_function_main}{The function used to compute breakpoints for
the main sorting variable. Defaults to \code{\link[=compute_breakpoints]{compute_breakpoints()}}.}

\item{breakpoint_function_secondary}{The function used to compute breakpoints
for the secondary sorting variable. Defaults to \code{\link[=compute_breakpoints]{compute_breakpoints()}}.}

\item{min_portfolio_size}{An integer specifying the minimum number of
firms in the reported portfolio cross-section on a given date. Defaults
to \code{1L} (at least one observation per reported portfolio). For univariate
sorts that is firms per portfolio-date; for bivariate sorts that is firms
per main-portfolio-date summed across the secondary buckets.
Cross-sections below the threshold have their returns set to \code{NA}.
Set to \code{0L} to deactivate the check. See \code{\link[=compute_portfolio_returns]{compute_portfolio_returns()}}.}

\item{cap_weight}{A numeric between 0 and 1 specifying the quantile at which
portfolio weights are capped for the capped value-weighted return.
Defaults to \code{0.8}.}

\item{data_options}{A list of class \code{tidyfinance_data_options} (created via
\code{\link[=data_options]{data_options()}}) specifying column name mappings. All elements are
forwarded to \code{\link[=filter_sorting_data]{filter_sorting_data()}} and \code{\link[=compute_portfolio_returns]{compute_portfolio_returns()}}.
Uses \code{\link[=data_options]{data_options()}} default if \code{NULL}: \code{"id" = "permno"},
\code{"date" = "date"}, \code{"exchange" = "exchange"},
\code{"mktcap_lag" = "mktcap_lag"}, \code{"ret_excess" = "ret_excess"},
\code{"siccd" = "siccd"}, \code{"price" = "prc_adj"},
\code{"listing_age" = "listing_age"}, \code{"be" = "be"}, and
\code{"earnings" = "ib"}.}

\item{quiet}{A logical indicating whether informational messages should be
suppressed. Defaults to \code{FALSE}.}
}
\value{
A data frame of portfolio returns as returned by
\code{\link[=compute_portfolio_returns]{compute_portfolio_returns()}}.
}
\description{
A convenience wrapper that combines sample construction filtering and
portfolio return computation into a single call. Equivalent to calling
\code{\link[=filter_sorting_data]{filter_sorting_data()}} followed by \code{\link[=compute_portfolio_returns]{compute_portfolio_returns()}}.
}
\examples{

set.seed(123)
data <- data.frame(
  permno = 1:500,
  date = rep(
    seq.Date(
      from = as.Date("2020-01-01"),
      by = "month",
      length.out = 100
    ),
    each = 10
  ),
  mktcap_lag = runif(500, 100, 1000),
  ret_excess = rnorm(500),
  prc_adj = runif(500, 0.5, 50),
  size = runif(500, 50, 150)
)
implement_portfolio_sort(
  data = data,
  sorting_variables = "size",
  sorting_method = "univariate",
  portfolio_sort_options = portfolio_sort_options(
    filter_options = filter_options(
      min_stock_price = 1
    ),
    breakpoint_options_main = breakpoint_options(n_portfolios = 5)
  )
)


}
\seealso{
Other portfolio functions: 
\code{\link{assign_portfolio}()},
\code{\link{breakpoint_options}()},
\code{\link{compute_breakpoints}()},
\code{\link{compute_long_short_returns}()},
\code{\link{compute_portfolio_returns}()},
\code{\link{data_options}()},
\code{\link{filter_options}()},
\code{\link{filter_sorting_data}()},
\code{\link{portfolio_sort_options}()}
}
\concept{portfolio functions}
