Overview
Therisk_decompose endpoint (/v2/prices?id=ITUB4&kind=risk, MCP and SQL api.risk_decompose) splits the daily return variance of one B3 share or unit into 3 parts:
- Market: the part that the Ibovespa (
IBOV) explains. - Sector: the part that one B3 sector index explains, after its own Ibovespa part is removed.
- Residual: the part that neither explains. This is the risk specific to the stock.
quote_history and index_history.
1. The data
The stock and the indices are on the same basis, because all three reinvest cash distributions.
Sessions with no total return. A session where
close_total_return is null is dropped. It is never filled. The count is in null_sessions and the reasons are in null_reasons, with flag null_sessions_dropped.
The price-only fallback. Some shares have no total-return series (for example, no cash distribution resolved for the ISIN). When more than 10% of the stock’s sessions in the window have a null close_total_return, the whole stock series uses close_adj instead. close_adj is adjusted for splits but not for dividends. Then stock_series = "price_only" and the flag price_only is set. The two bases are never mixed inside one series. The indices stay total return.
2. Join the levels, then compute the returns
The tape has no row for a session where the stock did not trade. If you compute the stock return and the index return separately and then join them on the date, a stock return can span 2 sessions while the Ibovespa return on the same date spans 1. That makes the covariances wrong. The endpoint does it in this order:- Inner join the three level series on
trade_date. A date that is missing in any leg is not in the table. - Compute simple returns from consecutive rows of the joined table:
r_t = P_t / P_(t-1) - 1, and the same form_tands_t.
3. The window
to: the default is today. It moves back to the last Ibovespa session on or before that date.from: the default is 253 Ibovespa sessions back, which gives 252 returns.- If the ticker’s first session on the tape is after
from, the window starts at the first session, with flagwindow_clamped. - A window of more than 1000 Ibovespa sessions (about 4 years) is refused with SQLSTATE
22023. - If fewer than 120 joined returns remain, the endpoint returns
status = "insufficient_sample". The counts are still in the answer, but the split columns are null. Betas from a smaller sample are too noisy to split the variance in 3 parts.
4. The split: an OLS cascade
All variances and covariances are sample statistics (divisorn - 1) over the n joined returns.
Step 1. The market beta.
cov(s_perp, m) = 0.
Step 3. The sector beta.
eps is uncorrelated with m and with s_perp, so the variance of the stock splits exactly:
r on m and s together. It only decides how to assign the variance that m and s share: the market gets all of it, and the sector leg gets only what is left.
5. The hedge ratios
Puts_perp = s - gamma * m back into the fit:
beta_market when the sector index already carries Ibovespa exposure (gamma > 0). BOVA11 or WIN futures are the tradable proxies for the Ibovespa. No listed ETF tracks most B3 sector indices.
6. The volatilities
window_sessions is the number of Ibovespa sessions in the window. For a stock that trades every session, n = window_sessions - 1 and ann = sqrt(252). When sessions are missing, some joined returns span several sessions, and the factor corrects for that. The variances add up. The volatilities do not, because they are square roots.
7. The sector leg
With nosector parameter, the endpoint maps the company’s current CVM sector (setor_current in research_universe) to one B3 sector index. A holding company (Emp. Adm. Part. - <sector>) takes the index of its sector.
Any other sector (for example oil and gas, telecom, transport, health) has no sector leg. Then the split has 2 parts,
sector_share is null, and the flag no_sector_index is set. The answer gives sector_leg_basis = "cvm_setor_map" or "none".
To choose the leg yourself, send sector with any index code that the platform holds other than IBOV, or none for 2 parts. Then sector_leg_basis = "caller".
The map is not index membership. The platform holds no sector-index portfolio. When the stock is a large member of its own sector index (for example a top-3 weight), the index contains the stock, so part of the stock’s own risk goes into
sector_share and residual_share is too small. The endpoint cannot detect this.8. The shock check
One large session can dominate a 1-year window. For example, on the 2026-10-26 election session the Ibovespa fell 13.9%. A shock session is a joined row where the Ibovespa return is far from its median:shock_dates and runs steps 4 to 6 again without them. The results are the ex_shock_* columns, with the same annualization factor. If there is no shock session, or fewer than 120 returns remain without them, the ex_shock_* columns are null. The flag shock_sensitive is set when any share changes by more than 0.10 (10 percentage points).
9. The liquidity check
no_trade_sessions: the sum ofprior_no_trade_sessionsover the stock’s rows in the window, without the first row (its count looks back before the window).zero_returns: the joined returns that are exactly 0.illiquid: true when either count is more than 5% of the window.
m_(t-1) is the previous joined return. When beta_dimson is much larger than beta_market, the stock’s price reacts to the market with a delay.
10. A worked example: ITUB4
ITUB4, 2025-11-18 to 2026-11-25, 251 returns, total-return series, sector leg IFNC from the map (a bank).
Check it by hand:
- The shares: 0.799814 + 0.052996 + 0.147190 = 1.000000.
- The market share from the volatilities: (0.276025 / 0.308641)^2 = 0.7998.
- The Ibovespa hedge: -(1.131368 - 0.664317 × 1.453225) = -(1.131368 - 0.965403) = -0.165966.
- The sector hedge: -0.664317.
shock_sensitive is false.
The example response on the operation page holds the full row.