OPEN-SOURCE SCRIPT

Multi-Factor Return Attribution / Fundamental Analysis

684
Here's the updated post with ORCL woven in:



Multi-Factor Return Attribution (MFRA)

Every stock return is a mix of forces. Part of the move comes from the broad market lifting or dragging everything with it. Part comes from capital rotating into or out of the sector. Part comes from flows specific to the sub-industry. And whatever's left — the piece none of those explain — is the stock's own fundamental or idiosyncratic signal.

MFRA decomposes equity returns into exactly these four layers: Broad Market, Sector, Sub-Sector, and Idiosyncratic, using a rolling orthogonalized regression framework that runs directly on your chart.

The default configuration ships with **ORCL** (Oracle) as the stock, **SPY** as the market proxy, **XLK** as the sector ETF, and **IGV** (software sub-sector) as the sub-industry ETF — but every symbol is configurable to any equity/ETF combination.



How It Works

The indicator takes four price feeds as input. Daily returns are computed for each, then transformed into orthogonal factors using a Gram-Schmidt-style subtraction chain — the sector factor becomes XLK return minus SPY return, the sub-sector factor becomes IGV return minus XLK return. This ensures each layer captures only incremental information, with no double-counting between levels.

A rolling OLS regression (configurable window, default 21 days) solves a full 4×4 normal equation system on every bar, estimating an intercept (alpha) and three slope coefficients (betas) against the orthogonalized factors. These betas represent the stock's current sensitivity to each layer of the return hierarchy. The residual — total return minus the sum of all factor contributions — isolates the idiosyncratic component: moves driven by earnings, guidance, analyst revisions, or anything else unique to the name.

The decomposition is exact. On every bar, the four attributed components sum precisely to the stock's total return. Nothing is lost, nothing is fabricated.



Reading It: ORCL as an Example

Oracle sits at an interesting intersection — it's a mega-cap tech name with exposure to cloud infrastructure, enterprise software, and database, which means its returns get pulled by broad market risk (SPY), tech sector rotation (XLK), and software-specific flows (IGV) simultaneously. MFRA separates all of that.

When ORCL rallies on a day the entire market is up, the stacked histogram shows a large cyan (Broad Market) bar — most of the move was just beta. But when Oracle moves on an AI infrastructure deal, a cloud revenue beat, or a Larry Ellison keynote and neither SPY nor XLK nor IGV moved proportionally, the magenta (Idiosyncratic) component dominates. That's the stock's own story, isolated from everything systematic.

If tech is rotating into software names specifically — IGV outperforming XLK which is outperforming SPY — you'll see the green (Sub-Sector) layer stacking up, telling you Oracle is riding a sub-industry wave, not generating alpha on its own.

The snapshot table in the top right gives you the exact numbers: how many percentage points of ORCL's recent return came from each source, the current betas, R², and daily alpha in basis points.



What Each Component Tells You

- Broad Market (cyan) — How much of the stock's move is just beta to the index. When this dominates, the stock is being carried by macro sentiment, not its own story.

- Sector Flows (orange) — Capital rotating into or out of the sector beyond what the broad market explains. Large sector contributions during flat market days reveal pure sector rotation trades.

- Sub-Sector Flows (green) — Thematic or sub-industry-specific moves beyond sector-level flow. For ORCL, this captures software/cloud momentum (IGV) that's distinct from broader tech (XLK).

- Fundamental / Idiosyncratic (magenta) — The residual. This is the stock's own signal — the return component that none of the systematic factors explain. Persistent idiosyncratic contribution suggests a name-specific catalyst is in play. Sudden spikes often align with earnings, news, or positioning shifts.



Four Chart Views

*Rolling Decomposition* — Stacked histogram showing the N-day rolling sum of each component's contribution (in percentage points), with positive and negative values stacked independently from the zero line. A white line traces total return for reference.

*Cumulative Attribution* — Same stacking logic, but cumulated from the start of the visible history. Shows how much of ORCL's total performance over the period came from each source — useful for answering "was this a beta trade or an alpha trade?"

*Rolling Betas* — Time series of the three regression coefficients. Tracks how ORCL's factor sensitivities evolve. A market beta rising above 1.0 means the name is becoming more aggressive than SPY; a sector beta collapsing toward zero means Oracle is decoupling from XLK.

*R-Squared* — The model's explanatory power over time. High R² means ORCL is moving in lockstep with its systematic factors. Low R² means idiosyncratic forces dominate — exactly when single-stock analysis matters most.



Practical Use Cases

Pre-earnings, check whether ORCL's recent run is broad-market-driven or idiosyncratic — the answer changes how you size and hedge the position. During sector rotations, isolate whether Oracle is participating because of tech/software flows or its own fundamentals. After a large move, decompose it immediately: was it SPY, XLK, IGV, or Oracle itself? That distinction determines whether the move is likely to mean-revert or persist.



Configuration

All four symbols are configurable — swap ORCL for any stock, change the sector/sub-sector ETFs to match. OLS window controls regression lookback (default 21 days). Rolling decomposition window controls the summation period for the histogram view (default 5 days). Snapshot table can be toggled on or off.



Methodology Note

The orthogonalization is a simplified Gram-Schmidt procedure applied to return differences rather than a full iterative projection. This is deliberate — it produces clean, interpretable factor layers (market → sector excess → sub-sector excess) without the numerical overhead of a full QR decomposition, and the resulting attribution remains exact. The regression is solved via direct 4×4 matrix inversion using cofactor expansion, with no external library dependencies.

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.