Pine Script Mass Index

Master this unique indicator in TradingView's Pine Script that identifies potential trend reversals by measuring the widening and narrowing of the trading range.

Subscribe now @ $99/month

What is the Mass Index?

The Mass Index, developed by Donald Dorsey, is a technical indicator that aims to predict trend reversals by measuring the expanding and contracting trading range (high-low difference) over a specific period. Unlike momentum indicators that focus on price speed or direction, the Mass Index focuses on volatility compression and expansion. It signals an impending trend reversal when the trading range widens significantly, often referred to as the "reversal bulge." The indicator sums up a series of Exponential Moving Averages (EMAs) of the high-low range.

In Pine Script, the Mass Index is a valuable tool for traders who believe that significant shifts in price range often precede a change in the prevailing trend, allowing them to anticipate potential reversals.

Components and Calculation

The calculation of the Mass Index involves several steps:

  1. Single Period High-Low Range: `Range = High - Low`
  2. Exponential Moving Average (EMA) of the Range: Calculate an EMA of the `Range` over a specified `emaPeriod` (e.g., 9 periods).
    `EMA_Range = ta.ema(Range, emaPeriod)`
  3. Double EMA of the Range: Calculate another EMA of the `EMA_Range` (smoothed EMA_Range) over the same `emaPeriod`.
    `Double_EMA_Range = ta.ema(EMA_Range, emaPeriod)`
  4. Ratio: Divide the `EMA_Range` by the `Double_EMA_Range`.
    `Ratio = EMA_Range / Double_EMA_Range`
  5. Mass Index Formula: Sum the `Ratio` over a longer `sumPeriod` (e.g., 25 periods).
    `Mass Index = sum(Ratio, sumPeriod)`

The standard parameters are 9 periods for the EMA (`emaPeriod`) and 25 periods for the summation (`sumPeriod`).

Basic Mass Index Implementation in Pine Script

Pine Script v5 provides a built-in function `ta.massindex()` for calculating the Mass Index.

//@version=5
indicator("My Mass Index", overlay=false) // overlay=false to plot in a separate pane

// Inputs for Mass Index lengths
emaPeriod = input.int(9, title="EMA Period", minval=1)
sumPeriod = input.int(25, title="Summation Period", minval=1)

// Calculate Mass Index using the built-in function
massIndexValue = ta.massindex(emaPeriod, sumPeriod)

// Plot the Mass Index line
plot(massIndexValue, title="Mass Index", color=color.blue, linewidth=2)

// Plot critical trigger lines
// The 27 level is the "reversal bulge" threshold
hline(27, "Reversal Threshold (27)", color.red, linestyle=hline.style_dashed)
// The 26.5 level is a confirmation line, often watched for downside breach after 27 is hit
hline(26.5, "Confirmation Line (26.5)", color.gray, linestyle=hline.style_dashed)
"Reversal Bulge": The core signal of the Mass Index is when it rises above 27 and then falls back below 26.5. This pattern suggests an imminent trend reversal.

Practical Mass Index Trading Strategies

1. The Reversal Bulge (Primary Strategy)

This is the most widely recognized and crucial strategy for the Mass Index. It signals that the current trend, regardless of its direction, is likely to reverse.

//@version=5
strategy("Mass Index Reversal Bulge Strategy", overlay=true)

emaPeriod = input.int(9, title="EMA Period", minval=1)
sumPeriod = input.int(25, title="Summation Period", minval=1)

massIndexValue = ta.massindex(emaPeriod, sumPeriod)

plot(massIndexValue, "Mass Index", color.blue, display=display.pane_only)
hline(27, "Reversal Threshold", color.red, linestyle=hline.style_dashed, display=display.pane_only)
hline(26.5, "Confirmation Line", color.gray, linestyle=hline.style_dashed, display=display.pane_only)

// Define conditions for the reversal bulge
// Mass Index touches 27 (or higher)
hits27 = massIndexValue >= 27
// Mass Index falls back below 26.5 AFTER hitting 27
// We need to keep track if 27 was hit recently
var bool hit27Recently = false
if (hits27)
    hit27Recently := true
if (massIndexValue[1] > 26.5 and massIndexValue <= 26.5 and hit27Recently) // Crosses down 26.5
    hit27Recently := false // Reset after signal

// The strategy assumes you are in a trend and this signal indicates reversal of that trend.
// For practical use, you'd combine this with a trend identification method.
// Here, we'll use a simple moving average for trend direction for example purposes.
trendLength = input.int(200, title="Trend EMA Length", minval=10)
trendEMA = ta.ema(close, trendLength)

// Reversal signal is active when Mass Index falls below 26.5 after hitting 27
reversalSignal = massIndexValue[1] > 26.5 and massIndexValue <= 26.5 and (massIndexValue[2] >= 27 or massIndexValue[3] >= 27 or massIndexValue[4] >= 27) // Check if 27 was hit recently

// If price is above long-term EMA, it's an uptrend, so reversal means short.
// If price is below long-term EMA, it's a downtrend, so reversal means long.
if (reversalSignal)
    if (close > trendEMA) // Was in uptrend, now expecting reversal down
        strategy.entry("Short Reversal", strategy.short)
    else if (close < trendEMA) // Was in downtrend, now expecting reversal up
        strategy.entry("Long Reversal", strategy.long)

// Add exits based on time or profit target/stop loss for robust strategy
// strategy.close_all() after X bars or at profit target/stop loss

2. Confirmation of Strength/Weakness (Less Common)

While the "reversal bulge" is the primary use, some traders might also look at the Mass Index as an indicator of general volatility. When it's below 26.5, it suggests price ranges are relatively narrow, often indicative of consolidating markets. When it's rising towards 27, it means volatility is increasing, which can precede strong moves in either direction.

//@version=5
indicator("Mass Index Volatility State", overlay=true)

emaPeriod = input.int(9, title="EMA Period", minval=1)
sumPeriod = input.int(25, title="Summation Period", minval=1)

massIndexValue = ta.massindex(emaPeriod, sumPeriod)

plot(massIndexValue, "Mass Index", color.blue)
hline(27, "Reversal Threshold", color.red, linestyle=hline.style_dashed)
hline(26.5, "Confirmation Line", color.gray, linestyle=hline.style_dashed)

// Highlight periods of volatility expansion (approaching 27)
var bool approaching27 = false
if (massIndexValue >= 26 and massIndexValue < 27)
    approaching27 := true
else
    approaching27 := false

// Highlight periods of range contraction (below 26.5)
var bool below26_5 = false
if (massIndexValue < 26.5)
    below26_5 := true
else
    below26_5 := false

bgcolor(approaching27 ? color.new(color.yellow, 90) : na, title="Volatility Expanding")
bgcolor(below26_5 ? color.new(color.blue, 90) : na, title="Range Contracting")

Optimizing Mass Index Performance

To get the most from the Mass Index in Pine Script:

Not a Directional Indicator: The Mass Index is not a directional indicator. It doesn't tell you *which way* the price will go, only *that a reversal is likely*. The direction must be inferred from the preceding trend or other indicators.

Common Mass Index Pitfalls

Conclusion

The Mass Index is a unique and insightful technical indicator in Pine Script for TradingView. Its primary strength lies in its ability to predict potential trend reversals by analyzing the expansion and contraction of an asset's trading range, specifically through the "reversal bulge" pattern. By understanding its calculation and focusing on its key signal (rising above 27 then falling below 26.5), and by combining it with robust trend identification and price action confirmation, traders can leverage the Mass Index to anticipate significant market turns. While it requires careful interpretation and integration into a broader strategy, the Mass Index offers a powerful edge for identifying high-probability reversal opportunities.

Enhance Your Trading

Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.

This strategy runs in live mode on TradingView, helping you identify potential opportunities.

Subscribe now @ $99/month