> ## Documentation Index
> Fetch the complete documentation index at: https://octo-98895abd.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Portfolio comparison functions

> Fee peers, class return distributions, ETF equivalents and the CRA, CRI and debenture value series behind a portfolio diagnosis, served as stored.

> [!NOTE]
>
> **Parameters, response columns and types are generated** from the SQL catalog and
> render under **Endpoints** in the sidebar. This page carries what a schema cannot.
> All five functions refuse above one 1,000-row page (`22023`) and have no cursor.
> None gives advice: they name a peer group, an equivalent or a value on a curve.

### Overview

| Function | Answers |
| - | - |
| `portfolio_fee_peers(p_cnpjs, p_as_of)` | A fund's administration fee against the active FI funds of its exact ANBIMA class. |
| `class_return_distribution(p_classe_anbima, p_fundo_cotas, p_month)` | What a class's 12 and 6 month returns look like across its funds. |
| `portfolio_equivalents(p_classes, p_as_of)` | The active ETFs that track an index mapped to an ANBIMA class. |
| `portfolio_credit_returns(p_codes, p_end_month, p_series, p_classes)` | A CRA or CRI's value on the securitizer's curve. |
| `portfolio_debenture_returns(p_tickers, p_end_month, p_max_monthly_drop_pct)` | A debenture's mark as the funds that hold it report it. |

### `portfolio_fee_peers`

Compares the fee with the active FI funds of the same ANBIMA class, `FUNDO_COTAS` flag and document scope. Active ETFs join a class only when their `underlying_index` is mapped to it in an owner-reviewed list. An ETF's fee comes from a third-party site (etfsbrasil), never from CVM, and `etf_peer_fee_source` says so. At least 30 usable fund fees are required (including the target when eligible); ETF fees do not count toward that minimum. Once the fund group qualifies, statistics use the combined fund and ETF fees. The function does not widen the group.

### `class_return_distribution`

Two rows, with `window_months` 12 and 6, ending at the close of `p_month` (NULL means the last complete FI month). It gives `p25_pct`, `median_pct` and `p75_pct` over `n_funds` returns, and counts the funds it left out. With fewer than 30 funds or an incomplete month, the row is `nao_avaliado`, with NULL statistics and a reason. ETFs are not in the universe.

### `portfolio_equivalents`

Takes 1 to 50 classes as filed. `is_equivalent` marks the ETF with the largest PL (`pl_rank` 1) across the class's indices. A class with no approved index pair (`sem_par`) or no active ETF (`sem_etf`) returns one row with NULL ETF columns. If active ETFs exist but none has PL, each ETF keeps its identity columns with status `sem_pl`, NULL `pl_rank` and `is_equivalent = false`. PL and fee are third-party values with their dates. It names an ETF with the same objective, not a recommendation.

### `portfolio_credit_returns`

Takes 1 to 70 CETIP codes (a leading `CRA-` or `CRI-` is stripped) and returns up to 13 month-ends from the securitizer's filings. `pu` is `valor_certificados / quantidade_certificados`, and `factor` is (pu + paid) / previous pu. A `month_flag` makes a month unknown, for example `mes_ausente`, `pu_repetido` or `queda_sem_evento_arquivado`. A fall in PU with no filed payment is never read as a loss. It is the securitizer's value on its curve, not a market price.

### `portfolio_debenture_returns`

Takes up to 70 debenture tickers and returns 13 month-ends from CVM CDA block 4. For each month it takes the median PU across funds (at least 3 funds), and `factor` is that median over the previous month's. A fall beyond `p_max_monthly_drop_pct` is flagged `queda_pu_possivel_evento`; with a NULL threshold every fall is flagged `limite_pendente`. It is the funds' marks, not a trade price, and it invents no coupon or amortization.

### Implementation source

The five functions are implemented in [`31_api_portfolio.sql`](https://github.com/PedroDnT/SILO-BZ/blob/b1c26551/src/store/analytical/31_api_portfolio.sql#L2437). The fee minimum is checked against `n_fund`; the `portfolio_equivalents` query retains ETF rows when `n_pl = 0`. The monthly debenture decline threshold remains an owner decision pending in the implementation: NULL flags every decline as `limite_pendente`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.