OPEN-SOURCE SCRIPT
Market Entropy Index

Market Entropy Index (MEI)
Most risk indicators react to price. They measure what has already happened. The Market Entropy Index takes a different approach: it measures the structural organization of the market itself, identifying fragility before it becomes visible in price. When sector participation narrows, when sectors stop agreeing on direction, and when credit markets become complacent, the MEI detects these precursor conditions. It applies information theoretic entropy to three independent dimensions of market structure, producing a single composite that distinguishes broad, healthy markets from concentrated, fragile ones. This makes it a leading indicator of structural risk, not a coincident crash detector.
What entropy means in financial markets
Entropy, as formalized by Shannon (1948), quantifies uncertainty in a probability distribution. In information theory, a distribution where all outcomes are equally likely has maximum entropy. A distribution concentrated on a single outcome has minimum entropy. Applied to financial markets, this framework has been used in two distinct ways that should not be confused.
The first is temporal return entropy: measuring how the distribution of an index's daily returns changes over time. Risso (2008) showed that Shannon entropy of stock market return distributions drops before financial crashes, as returns become more extreme and less uniformly distributed. Zunino et al. (2009) found that permutation entropy of return series tracks market efficiency and deteriorates during stress. Gu (2017) extended this to multiple time scales. These studies all measure the statistical properties of a single return series over time.
The second is cross-sectional entropy, which is what the MEI uses. Instead of asking "how are returns distributed over time?", it asks "how is market activity distributed across sectors right now?" When all nine S&P 500 GICS sectors contribute equally to market movement, the entropy of their return distribution reaches its theoretical maximum: roughly log2(9) = 3.17 bits. This corresponds to broad, healthy participation. When movement concentrates in two or three sectors while the rest are flat, entropy drops. The market relies on a narrow base.
These two types of entropy can move in opposite directions. During an acute crash, temporal return entropy drops (Risso's finding: returns become extreme and non-normal). But cross-sectional breadth entropy often rises, because all sectors sell off together, producing a more uniform distribution across the cross-section. The MEI does not measure temporal return entropy. It measures cross-sectional breadth entropy and two related structural conditions. This distinction matters for interpretation (see the section on what the MEI does not do).
How the MEI is constructed
The indicator combines three dimensions, each measuring a distinct aspect of market fragility. All three were validated through statistical screening with Bonferroni correction across seven different parameter configurations to guard against data-mining bias.
Sector Breadth Concentration (weight: 0.40)
This is the primary dimension. It computes the Shannon entropy of the distribution of smoothed absolute returns across nine GICS sector ETFs (XLB, XLE, XLF, XLI, XLK, XLP, XLU, XLV, XLY) over a 21-day rolling window. The entropy value is normalized to the theoretical maximum so it ranges from 0 (all activity in one sector) to 1 (perfectly uniform distribution).
The critical finding from backtesting: low sector entropy (concentrated breadth) is the danger condition, not high entropy. When market movement narrows to a few sectors, the rally or sell-off lacks structural support. This is consistent with the well-documented market breadth divergence effect: narrow rallies tend to precede corrections. In our testing, the low-entropy tercile showed significantly worse forward returns than the high-entropy tercile across a 21-day horizon (spread = +1.18%, t = 5.81, p = 7.3e-09, Bonferroni-significant in all seven parameter configurations).
Sector Directional Discord (weight: 0.30)
This dimension measures the fraction of sectors that agree on daily direction (all up or all down), averaged over 21 days. When eight of nine sectors move in the same direction, concordance is high, indicating a coherent market. When sectors split nearly evenly between positive and negative days, concordance drops, signaling confusion, rotation, or conflicting macro forces.
Low concordance (high discord) is the danger condition. Sectors disagreeing on direction means the market lacks conviction and is vulnerable to dislocations. This dimension was Bonferroni-significant in five of seven parameter configurations (21d: spread = +0.97%, t = 5.20, p = 2.2e-07).
Credit Complacency (weight: 0.30)
The third dimension measures the rolling standard deviation of the daily return spread between iShares High Yield Corporate Bond ETF (HYG) and iShares Investment Grade Corporate Bond ETF (LQD), normalized by its 252-day average. This ratio captures how volatile credit spreads are relative to their recent history.
Low credit spread volatility is the danger condition. When credit markets are calm and spreads barely move, it often reflects complacent risk pricing. The empirical parallel is well-supported: Gilchrist and Zakrajsek (2012) showed that credit spread dynamics, specifically the excess bond premium, predict economic downturns and equity returns. In our testing, this dimension produced the strongest individual t-statistic (63d: spread = +1.89%, t = 6.73, p = 2.2e-11, Bonferroni-significant in three of seven configurations).
Signal processing
Each dimension is z-scored over a 252-day lookback and clipped at three standard deviations. The z-scores are sign-inverted so that high values consistently indicate danger across all three dimensions. After weighting, the composite is re-standardized over 252 days to restore the variance lost through averaging weakly correlated signals. The result is scaled to a 0-10 range (5.0 + z * 2.0) and smoothed with a Kaufman Adaptive Moving Average (Kaufman, 2013). The KAMA adjusts its smoothing speed based on the efficiency ratio of the composite: during clear regime transitions, it responds quickly; during choppy sideways periods, it filters noise. A minimum smoothing constant floor prevents the filter from becoming excessively sluggish.
How to read the MEI
0 to 3: Low Risk. All three dimensions read safe. Sectors participate broadly, agree on direction, and credit markets are actively pricing risk. These conditions are historically associated with favorable forward equity returns.
3 to 7: Normal. No structural signal in either direction. The market is in equilibrium. This is the expected reading roughly two-thirds of the time.
7 to 10: Elevated Risk. One or more dimensions show stress. Sector participation is narrowing, directional agreement is breaking down, or credit markets have become complacent. The higher the reading, the more dimensions agree on risk.
The dashboard shows each dimension individually, so you can diagnose what is driving the composite. The historical percentile tells you where the current reading sits relative to the past 252 days. The trend direction (with arrow symbols) shows whether risk is rising or falling.
What the MEI detects and what it does not
The MEI is a leading indicator of structural fragility, not a coincident crash detector. It measures conditions that build up before market stress: narrowing sector participation, loss of directional agreement, and complacent credit pricing. These are precursor conditions. They describe a market that has become structurally fragile, not one that is already falling apart.
During an acute sell off, the MEI typically drops toward the green zone. This is not a malfunction. When all sectors sell off together, breadth entropy actually increases (uniform distribution across sectors), concordance rises (all sectors agree on the down direction), and credit spread volatility spikes (the opposite of complacency). All three dimensions read "safe" precisely because the structural fragility has already resolved through the sell-off itself.
The practical implication: the MEI is most useful in the quiet periods before stress, when markets look calm but the underlying structure is deteriorating. If the MEI reads 8 while the SPX is making new highs, that is a warning worth paying attention to. If the MEI reads 2 during a violent correction, that means the correction is broad-based and structural participation is actually healthy, which is historically a better setup for recovery than a narrow, concentrated decline.
How to use it in practice
The MEI is a regime monitor, not a timing signal. It answers the question "what kind of market are we in?" rather than "should I buy or sell today?" The most productive way to use it:
As a confluence filter: combine the MEI with your existing trend-following or mean-reversion strategy. When the MEI reads above 7, tighten stops, reduce position sizes, or require stronger entry signals. When it reads below 3, conditions favor taking positions.
As an allocation tool: for portfolio managers running multi-asset or tactical allocation, the MEI provides a daily structural risk reading that can scale equity exposure. Reduce equity allocation when the composite is elevated, increase when it is low.
As a diagnostic tool: enable the individual components (Breadth Concentration, Directional Discord, Credit Complacency) to understand what is driving the composite. If only one dimension is elevated while the others are normal, the risk may be localized. If all three converge, the structural case is stronger.
For monitoring credit conditions: the Credit Complacency dimension alone serves as a real-time gauge of credit market risk pricing. Low readings (complacency) have historically preceded episodes of spread widening.
Quant fund applications
For systematic portfolio managers and quantitative research teams, the MEI framework offers several practical applications.
As a regime classifier for conditional strategies: most equity strategies behave differently in ordered versus disordered markets. Momentum strategies, for example, tend to work well when breadth entropy is high (broad participation) and poorly when it is low (concentrated leadership). The MEI provides a daily regime classification that can condition strategy selection or parameter adjustment. In our backtesting, the composite showed a spread of +2.86% (21-day forward returns, t = 5.91) in high-volatility regimes, offering a quantitatively meaningful signal for regime-conditional allocation.
As a risk budget input: the three z-scored danger signals can feed directly into a risk budgeting framework. When breadth_danger or credit_danger exceeds one standard deviation, the risk model can automatically reduce gross exposure or hedge tail risk. The low cross-correlation between dimensions (breadth-credit: rho = -0.07, breadth-discord: rho = 0.20) means each dimension adds genuine incremental information to the risk estimate.
As an alpha decay monitor: sector concentration (low breadth entropy) is one mechanism through which crowded trades develop. When the breadth dimension rises, it may indicate that a previously broad factor exposure has narrowed to a few names or sectors, which is a warning sign for factor crowding and potential alpha decay.
As a multi-asset overlay: the framework extends naturally beyond equities. The same entropy-based approach can be applied to any cross-section of assets (currencies, commodities, fixed income sectors) to detect concentration and complacency.
Limitations
This indicator has clear boundaries that users should understand.
It detects fragility, not crashes. The MEI measures structural precursors (concentration, complacency, discord) that build up before stress events. During acute sell-offs, the indicator typically drops because the conditions it measures dissolve once panic selling is broad-based. Do not expect the MEI to read red during a crash. Expect it to read red before one.
The signal is regime-dependent. In high-volatility and bear markets, the composite works as designed: high readings correspond to worse forward returns, low readings to better. In calm, trending bull markets, the relationship weakens and can reverse. This is because the "danger" conditions (concentrated breadth, credit complacency) can persist for extended periods during healthy trends without leading to corrections. Weight MEI readings more heavily when realized volatility is already elevated.
It is designed for the S&P 500. The sector ETFs and credit instruments are U.S.-specific. Applying the indicator to other indices or asset classes without modifying the data sources would not be methodologically sound.
It requires a daily timeframe. The cross-sector entropy and credit spread calculations require daily closing prices. Intraday data introduces noise that degrades the signal quality.
It needs historical depth. The z-score normalization uses a 252-day lookback. Results during the first year of data should be treated with caution.
It is not a standalone system. No single indicator captures all relevant market dynamics. The MEI measures structural conditions. It does not measure momentum, valuation, sentiment, or liquidity directly. Use it alongside other analytical tools.
References
Gilchrist, S. and Zakrajsek, E. (2012) 'Credit Spreads and Business Cycle Fluctuations', American Economic Review, 102(4), pp. 1692-1720.
Gu, R. (2017) 'Multiscale Shannon entropy and its application in the stock market', Physica A, 484, pp. 215-224.
Kaufman, P.J. (2013) Trading Systems and Methods. 5th edn. Hoboken: Wiley.
Risso, W.A. (2008) 'The informational efficiency and the financial crashes', Research in International Business and Finance, 22(3), pp. 396-408.
Shannon, C.E. (1948) 'A Mathematical Theory of Communication', Bell System Technical Journal, 27(3), pp. 379-423.
Zunino, L., Zanin, M., Tabak, B.M., Perez, D.G. and Rosso, O.A. (2009) 'Forbidden patterns, permutation entropy and stock market inefficiency', Physica A, 388(14), pp. 2854-2864.
Most risk indicators react to price. They measure what has already happened. The Market Entropy Index takes a different approach: it measures the structural organization of the market itself, identifying fragility before it becomes visible in price. When sector participation narrows, when sectors stop agreeing on direction, and when credit markets become complacent, the MEI detects these precursor conditions. It applies information theoretic entropy to three independent dimensions of market structure, producing a single composite that distinguishes broad, healthy markets from concentrated, fragile ones. This makes it a leading indicator of structural risk, not a coincident crash detector.
What entropy means in financial markets
Entropy, as formalized by Shannon (1948), quantifies uncertainty in a probability distribution. In information theory, a distribution where all outcomes are equally likely has maximum entropy. A distribution concentrated on a single outcome has minimum entropy. Applied to financial markets, this framework has been used in two distinct ways that should not be confused.
The first is temporal return entropy: measuring how the distribution of an index's daily returns changes over time. Risso (2008) showed that Shannon entropy of stock market return distributions drops before financial crashes, as returns become more extreme and less uniformly distributed. Zunino et al. (2009) found that permutation entropy of return series tracks market efficiency and deteriorates during stress. Gu (2017) extended this to multiple time scales. These studies all measure the statistical properties of a single return series over time.
The second is cross-sectional entropy, which is what the MEI uses. Instead of asking "how are returns distributed over time?", it asks "how is market activity distributed across sectors right now?" When all nine S&P 500 GICS sectors contribute equally to market movement, the entropy of their return distribution reaches its theoretical maximum: roughly log2(9) = 3.17 bits. This corresponds to broad, healthy participation. When movement concentrates in two or three sectors while the rest are flat, entropy drops. The market relies on a narrow base.
These two types of entropy can move in opposite directions. During an acute crash, temporal return entropy drops (Risso's finding: returns become extreme and non-normal). But cross-sectional breadth entropy often rises, because all sectors sell off together, producing a more uniform distribution across the cross-section. The MEI does not measure temporal return entropy. It measures cross-sectional breadth entropy and two related structural conditions. This distinction matters for interpretation (see the section on what the MEI does not do).
How the MEI is constructed
The indicator combines three dimensions, each measuring a distinct aspect of market fragility. All three were validated through statistical screening with Bonferroni correction across seven different parameter configurations to guard against data-mining bias.
Sector Breadth Concentration (weight: 0.40)
This is the primary dimension. It computes the Shannon entropy of the distribution of smoothed absolute returns across nine GICS sector ETFs (XLB, XLE, XLF, XLI, XLK, XLP, XLU, XLV, XLY) over a 21-day rolling window. The entropy value is normalized to the theoretical maximum so it ranges from 0 (all activity in one sector) to 1 (perfectly uniform distribution).
The critical finding from backtesting: low sector entropy (concentrated breadth) is the danger condition, not high entropy. When market movement narrows to a few sectors, the rally or sell-off lacks structural support. This is consistent with the well-documented market breadth divergence effect: narrow rallies tend to precede corrections. In our testing, the low-entropy tercile showed significantly worse forward returns than the high-entropy tercile across a 21-day horizon (spread = +1.18%, t = 5.81, p = 7.3e-09, Bonferroni-significant in all seven parameter configurations).
Sector Directional Discord (weight: 0.30)
This dimension measures the fraction of sectors that agree on daily direction (all up or all down), averaged over 21 days. When eight of nine sectors move in the same direction, concordance is high, indicating a coherent market. When sectors split nearly evenly between positive and negative days, concordance drops, signaling confusion, rotation, or conflicting macro forces.
Low concordance (high discord) is the danger condition. Sectors disagreeing on direction means the market lacks conviction and is vulnerable to dislocations. This dimension was Bonferroni-significant in five of seven parameter configurations (21d: spread = +0.97%, t = 5.20, p = 2.2e-07).
Credit Complacency (weight: 0.30)
The third dimension measures the rolling standard deviation of the daily return spread between iShares High Yield Corporate Bond ETF (HYG) and iShares Investment Grade Corporate Bond ETF (LQD), normalized by its 252-day average. This ratio captures how volatile credit spreads are relative to their recent history.
Low credit spread volatility is the danger condition. When credit markets are calm and spreads barely move, it often reflects complacent risk pricing. The empirical parallel is well-supported: Gilchrist and Zakrajsek (2012) showed that credit spread dynamics, specifically the excess bond premium, predict economic downturns and equity returns. In our testing, this dimension produced the strongest individual t-statistic (63d: spread = +1.89%, t = 6.73, p = 2.2e-11, Bonferroni-significant in three of seven configurations).
Signal processing
Each dimension is z-scored over a 252-day lookback and clipped at three standard deviations. The z-scores are sign-inverted so that high values consistently indicate danger across all three dimensions. After weighting, the composite is re-standardized over 252 days to restore the variance lost through averaging weakly correlated signals. The result is scaled to a 0-10 range (5.0 + z * 2.0) and smoothed with a Kaufman Adaptive Moving Average (Kaufman, 2013). The KAMA adjusts its smoothing speed based on the efficiency ratio of the composite: during clear regime transitions, it responds quickly; during choppy sideways periods, it filters noise. A minimum smoothing constant floor prevents the filter from becoming excessively sluggish.
How to read the MEI
0 to 3: Low Risk. All three dimensions read safe. Sectors participate broadly, agree on direction, and credit markets are actively pricing risk. These conditions are historically associated with favorable forward equity returns.
3 to 7: Normal. No structural signal in either direction. The market is in equilibrium. This is the expected reading roughly two-thirds of the time.
7 to 10: Elevated Risk. One or more dimensions show stress. Sector participation is narrowing, directional agreement is breaking down, or credit markets have become complacent. The higher the reading, the more dimensions agree on risk.
The dashboard shows each dimension individually, so you can diagnose what is driving the composite. The historical percentile tells you where the current reading sits relative to the past 252 days. The trend direction (with arrow symbols) shows whether risk is rising or falling.
What the MEI detects and what it does not
The MEI is a leading indicator of structural fragility, not a coincident crash detector. It measures conditions that build up before market stress: narrowing sector participation, loss of directional agreement, and complacent credit pricing. These are precursor conditions. They describe a market that has become structurally fragile, not one that is already falling apart.
During an acute sell off, the MEI typically drops toward the green zone. This is not a malfunction. When all sectors sell off together, breadth entropy actually increases (uniform distribution across sectors), concordance rises (all sectors agree on the down direction), and credit spread volatility spikes (the opposite of complacency). All three dimensions read "safe" precisely because the structural fragility has already resolved through the sell-off itself.
The practical implication: the MEI is most useful in the quiet periods before stress, when markets look calm but the underlying structure is deteriorating. If the MEI reads 8 while the SPX is making new highs, that is a warning worth paying attention to. If the MEI reads 2 during a violent correction, that means the correction is broad-based and structural participation is actually healthy, which is historically a better setup for recovery than a narrow, concentrated decline.
How to use it in practice
The MEI is a regime monitor, not a timing signal. It answers the question "what kind of market are we in?" rather than "should I buy or sell today?" The most productive way to use it:
As a confluence filter: combine the MEI with your existing trend-following or mean-reversion strategy. When the MEI reads above 7, tighten stops, reduce position sizes, or require stronger entry signals. When it reads below 3, conditions favor taking positions.
As an allocation tool: for portfolio managers running multi-asset or tactical allocation, the MEI provides a daily structural risk reading that can scale equity exposure. Reduce equity allocation when the composite is elevated, increase when it is low.
As a diagnostic tool: enable the individual components (Breadth Concentration, Directional Discord, Credit Complacency) to understand what is driving the composite. If only one dimension is elevated while the others are normal, the risk may be localized. If all three converge, the structural case is stronger.
For monitoring credit conditions: the Credit Complacency dimension alone serves as a real-time gauge of credit market risk pricing. Low readings (complacency) have historically preceded episodes of spread widening.
Quant fund applications
For systematic portfolio managers and quantitative research teams, the MEI framework offers several practical applications.
As a regime classifier for conditional strategies: most equity strategies behave differently in ordered versus disordered markets. Momentum strategies, for example, tend to work well when breadth entropy is high (broad participation) and poorly when it is low (concentrated leadership). The MEI provides a daily regime classification that can condition strategy selection or parameter adjustment. In our backtesting, the composite showed a spread of +2.86% (21-day forward returns, t = 5.91) in high-volatility regimes, offering a quantitatively meaningful signal for regime-conditional allocation.
As a risk budget input: the three z-scored danger signals can feed directly into a risk budgeting framework. When breadth_danger or credit_danger exceeds one standard deviation, the risk model can automatically reduce gross exposure or hedge tail risk. The low cross-correlation between dimensions (breadth-credit: rho = -0.07, breadth-discord: rho = 0.20) means each dimension adds genuine incremental information to the risk estimate.
As an alpha decay monitor: sector concentration (low breadth entropy) is one mechanism through which crowded trades develop. When the breadth dimension rises, it may indicate that a previously broad factor exposure has narrowed to a few names or sectors, which is a warning sign for factor crowding and potential alpha decay.
As a multi-asset overlay: the framework extends naturally beyond equities. The same entropy-based approach can be applied to any cross-section of assets (currencies, commodities, fixed income sectors) to detect concentration and complacency.
Limitations
This indicator has clear boundaries that users should understand.
It detects fragility, not crashes. The MEI measures structural precursors (concentration, complacency, discord) that build up before stress events. During acute sell-offs, the indicator typically drops because the conditions it measures dissolve once panic selling is broad-based. Do not expect the MEI to read red during a crash. Expect it to read red before one.
The signal is regime-dependent. In high-volatility and bear markets, the composite works as designed: high readings correspond to worse forward returns, low readings to better. In calm, trending bull markets, the relationship weakens and can reverse. This is because the "danger" conditions (concentrated breadth, credit complacency) can persist for extended periods during healthy trends without leading to corrections. Weight MEI readings more heavily when realized volatility is already elevated.
It is designed for the S&P 500. The sector ETFs and credit instruments are U.S.-specific. Applying the indicator to other indices or asset classes without modifying the data sources would not be methodologically sound.
It requires a daily timeframe. The cross-sector entropy and credit spread calculations require daily closing prices. Intraday data introduces noise that degrades the signal quality.
It needs historical depth. The z-score normalization uses a 252-day lookback. Results during the first year of data should be treated with caution.
It is not a standalone system. No single indicator captures all relevant market dynamics. The MEI measures structural conditions. It does not measure momentum, valuation, sentiment, or liquidity directly. Use it alongside other analytical tools.
References
Gilchrist, S. and Zakrajsek, E. (2012) 'Credit Spreads and Business Cycle Fluctuations', American Economic Review, 102(4), pp. 1692-1720.
Gu, R. (2017) 'Multiscale Shannon entropy and its application in the stock market', Physica A, 484, pp. 215-224.
Kaufman, P.J. (2013) Trading Systems and Methods. 5th edn. Hoboken: Wiley.
Risso, W.A. (2008) 'The informational efficiency and the financial crashes', Research in International Business and Finance, 22(3), pp. 396-408.
Shannon, C.E. (1948) 'A Mathematical Theory of Communication', Bell System Technical Journal, 27(3), pp. 379-423.
Zunino, L., Zanin, M., Tabak, B.M., Perez, D.G. and Rosso, O.A. (2009) 'Forbidden patterns, permutation entropy and stock market inefficiency', Physica A, 388(14), pp. 2854-2864.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
Where others speculate, we systematize.
edgetools.org
edgetools.org
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
Where others speculate, we systematize.
edgetools.org
edgetools.org
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.