OPEN-SOURCE SCRIPT
Margin Debt Expansion vs Contraction Indicator

█ OVERVIEW
This indicator plots the year-over-year percentage change in U.S. margin debt alongside the DJIA to identify major market tops and bottoms. When speculative leverage expands rapidly into the red zone, a market top is forming. When leverage contracts sharply into the green zone, a washout has occurred and a buying opportunity is likely at hand. The concept is adapted from Callum Thomas of Topdown Charts, who has published extensively on the relationship between margin debt momentum and equity market turning points.
The indicator uses the Federal Reserve's Z.1 Financial Accounts series for broker/dealer customer receivables (FRED symbol
BOGZ1FL663067003Q
) as a proxy for FINRA margin debt. This is a quarterly series. FINRA's own monthly margin debt data is not available natively on TradingView, so this quarterly proxy is the best available substitute. The directional signal and zone logic remain valid, though zone crossings will appear with a slight lag compared to monthly data.
█ HOW IT WORKS
The indicator computes the year-over-year percentage change in margin debt by comparing the current quarter's value to the value four quarters prior. This YoY% rate of change is the core signal. Raw margin debt levels trend upward over time with market growth, making absolute levels less informative than the rate of change, which captures the acceleration and deceleration of speculative leverage.
Two colored zones highlight the critical regimes:
Red zone (+40% to +55% YoY)
Margin debt is expanding at a pace historically associated with speculative excess. Investors are borrowing aggressively to chase returns, and the market is vulnerable to a sharp correction. Major tops in 2000, 2007, and 2021 were preceded by margin debt YoY% reaching or entering this zone.
Green zone (-20% to -40% YoY)
Margin debt is contracting at a pace historically associated with capitulation and forced deleveraging. This typically coincides with or shortly follows major market lows. The 2002-2003, 2008-2009, and 2020 bottoms all registered margin debt YoY% readings in or near this zone.
The YoY% line changes color based on the active regime: red when at or above the red zone lower bound, green when at or below the green zone upper bound, and neutral otherwise.
█ SIGNALS
The indicator provides four distinct signal types, each with a corresponding alert condition:
Zone entries
• Red Zone Entry (triangle-down marker): YoY% crosses above +40% for the first time. This is the initial top warning.
• Green Zone Entry (triangle-up marker): YoY% crosses below -20% for the first time. This is the initial buy-the-dip signal.
Rollovers
• Red Zone Rollover (circle marker): YoY% peaks and begins declining while still inside the red zone. This is a top confirmation, suggesting the rate of leverage expansion has peaked and the market is losing its speculative fuel.
• Green Zone Rollover (circle marker): YoY% troughs and begins rising while still inside the green zone. This is a bottom confirmation, suggesting the worst of the deleveraging is behind.
Rollovers are arguably the sharper timing signals. A zone entry tells you the regime is dangerous or washed out; a rollover within that zone tells you the turn is underway.
█ INPUTS
Zone Thresholds
All four zone boundaries are configurable. The defaults (+40/+55 for red, -20/-40 for green) are derived from the historical distribution of margin debt YoY% at major turning points, but users may wish to adjust them for sensitivity.
Display
• Show DJIA Overlay: Toggles a reference DJIA price plot. Note that because the indicator runs in a separate pane, the DJIA overlay uses `display.none` by default and is available primarily for data reference via the Data Window.
• Show Zone Entry Labels: Toggles the triangle and circle markers at signal points.
█ LIMITATIONS
• The underlying FRED series (
BOGZ1FL663067003Q
) is quarterly, not monthly. Values are held constant between quarterly prints, producing a step-like YoY% line. Zone crossings may appear one quarter later than they would with FINRA's monthly data. This is a data availability constraint on TradingView, not a methodological choice.
• Margin debt is a coincident-to-lagging indicator of speculative sentiment. It confirms regime shifts rather than predicting them. It should be used alongside leading indicators (breadth, credit spreads, yield curve) rather than in isolation.
• The FRED series occasionally returns fetch errors on TradingView. If the indicator shows no data, removing and re-adding it to the chart typically resolves the issue.
• This indicator is designed for use on the DJIA (or S&P 500) on a daily timeframe. Applying it to other instruments or intraday timeframes will produce misleading results, since the underlying data is a macro leverage measure for U.S. equities.
• The red and green zone thresholds are empirically derived from a relatively small sample of major market cycles (~6 complete cycles since 1995). They should be treated as guideposts, not bright lines.
█ DATA SOURCE
The data comes from the Federal Reserve's Z.1 Financial Accounts, specifically the series "Security Brokers and Dealers; Receivables Due from Customers (Margin Loans and Other Receivables); Asset, Level," accessed via
BOGZ1FL663067003Q
on TradingView. This series is a close proxy for, but not identical to, the FINRA margin debt data that FINRA member firms report monthly. The FRED series is derived from the same underlying regulatory reporting but is aggregated and published quarterly by the Federal Reserve as part of the Flow of Funds accounts.
Historical context: this data was originally published by the NYSE. FINRA assumed responsibility for aggregating and distributing margin statistics from its member firms. The underlying economic meaning has remained consistent across the transition.
This indicator plots the year-over-year percentage change in U.S. margin debt alongside the DJIA to identify major market tops and bottoms. When speculative leverage expands rapidly into the red zone, a market top is forming. When leverage contracts sharply into the green zone, a washout has occurred and a buying opportunity is likely at hand. The concept is adapted from Callum Thomas of Topdown Charts, who has published extensively on the relationship between margin debt momentum and equity market turning points.
The indicator uses the Federal Reserve's Z.1 Financial Accounts series for broker/dealer customer receivables (FRED symbol
BOGZ1FL663067003Q
) as a proxy for FINRA margin debt. This is a quarterly series. FINRA's own monthly margin debt data is not available natively on TradingView, so this quarterly proxy is the best available substitute. The directional signal and zone logic remain valid, though zone crossings will appear with a slight lag compared to monthly data.
█ HOW IT WORKS
The indicator computes the year-over-year percentage change in margin debt by comparing the current quarter's value to the value four quarters prior. This YoY% rate of change is the core signal. Raw margin debt levels trend upward over time with market growth, making absolute levels less informative than the rate of change, which captures the acceleration and deceleration of speculative leverage.
Two colored zones highlight the critical regimes:
Red zone (+40% to +55% YoY)
Margin debt is expanding at a pace historically associated with speculative excess. Investors are borrowing aggressively to chase returns, and the market is vulnerable to a sharp correction. Major tops in 2000, 2007, and 2021 were preceded by margin debt YoY% reaching or entering this zone.
Green zone (-20% to -40% YoY)
Margin debt is contracting at a pace historically associated with capitulation and forced deleveraging. This typically coincides with or shortly follows major market lows. The 2002-2003, 2008-2009, and 2020 bottoms all registered margin debt YoY% readings in or near this zone.
The YoY% line changes color based on the active regime: red when at or above the red zone lower bound, green when at or below the green zone upper bound, and neutral otherwise.
█ SIGNALS
The indicator provides four distinct signal types, each with a corresponding alert condition:
Zone entries
• Red Zone Entry (triangle-down marker): YoY% crosses above +40% for the first time. This is the initial top warning.
• Green Zone Entry (triangle-up marker): YoY% crosses below -20% for the first time. This is the initial buy-the-dip signal.
Rollovers
• Red Zone Rollover (circle marker): YoY% peaks and begins declining while still inside the red zone. This is a top confirmation, suggesting the rate of leverage expansion has peaked and the market is losing its speculative fuel.
• Green Zone Rollover (circle marker): YoY% troughs and begins rising while still inside the green zone. This is a bottom confirmation, suggesting the worst of the deleveraging is behind.
Rollovers are arguably the sharper timing signals. A zone entry tells you the regime is dangerous or washed out; a rollover within that zone tells you the turn is underway.
█ INPUTS
Zone Thresholds
All four zone boundaries are configurable. The defaults (+40/+55 for red, -20/-40 for green) are derived from the historical distribution of margin debt YoY% at major turning points, but users may wish to adjust them for sensitivity.
Display
• Show DJIA Overlay: Toggles a reference DJIA price plot. Note that because the indicator runs in a separate pane, the DJIA overlay uses `display.none` by default and is available primarily for data reference via the Data Window.
• Show Zone Entry Labels: Toggles the triangle and circle markers at signal points.
█ LIMITATIONS
• The underlying FRED series (
BOGZ1FL663067003Q
) is quarterly, not monthly. Values are held constant between quarterly prints, producing a step-like YoY% line. Zone crossings may appear one quarter later than they would with FINRA's monthly data. This is a data availability constraint on TradingView, not a methodological choice.
• Margin debt is a coincident-to-lagging indicator of speculative sentiment. It confirms regime shifts rather than predicting them. It should be used alongside leading indicators (breadth, credit spreads, yield curve) rather than in isolation.
• The FRED series occasionally returns fetch errors on TradingView. If the indicator shows no data, removing and re-adding it to the chart typically resolves the issue.
• This indicator is designed for use on the DJIA (or S&P 500) on a daily timeframe. Applying it to other instruments or intraday timeframes will produce misleading results, since the underlying data is a macro leverage measure for U.S. equities.
• The red and green zone thresholds are empirically derived from a relatively small sample of major market cycles (~6 complete cycles since 1995). They should be treated as guideposts, not bright lines.
█ DATA SOURCE
The data comes from the Federal Reserve's Z.1 Financial Accounts, specifically the series "Security Brokers and Dealers; Receivables Due from Customers (Margin Loans and Other Receivables); Asset, Level," accessed via
BOGZ1FL663067003Q
on TradingView. This series is a close proxy for, but not identical to, the FINRA margin debt data that FINRA member firms report monthly. The FRED series is derived from the same underlying regulatory reporting but is aggregated and published quarterly by the Federal Reserve as part of the Flow of Funds accounts.
Historical context: this data was originally published by the NYSE. FINRA assumed responsibility for aggregating and distributing margin statistics from its member firms. The underlying economic meaning has remained consistent across the transition.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.