Master this unique indicator in TradingView's Pinescript that identifies potential trend reversals by measuring the widening and narrowing of the trading range.
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 Pinescript, 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.
The calculation of the Mass Index involves several steps:
//@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)
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.
//@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
//@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")
To get the most from the Mass Index in Pinescript:
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.
The Mass Index is a unique and insightful technical indicator in Pinescript 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.
Get a high-performance Pinescript 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.
Get Pinescript Strategy