Master this unique indicator in TradingView's Pinescript that quantifies the degree of market choppiness (ranging) versus trending behavior.
The Choppiness Index (CI) is a technical indicator developed by Bill Williams, though often attributed to E.W. Dreiss. It measures whether a market is trending or ranging (choppy). The CI is designed to quantify the "choppiness" or "trendiness" of price action, based on fractal geometry. It oscillates between 0 and 100. High CI values indicate a choppy, non-trending market (sideways consolidation), while low CI values indicate a strong, trending market (directional movement).
In Pinescript, CI is a valuable tool for traders to decide which type of trading strategy to employ: trend-following strategies during low CI periods, and range-bound or breakout strategies during high CI periods. It essentially helps determine the market environment.
The calculation of the Choppiness Index is based on the summation of True Ranges and the highest high and lowest low over a specified period. The formula involves logarithmic functions to normalize the output between 0 and 100.
//@version=5
indicator("My Choppiness Index", overlay=false, format=format.percent) // overlay=false to plot in a separate pane
// Input for CI length
length = input.int(14, title="CI Length", minval=1)
// Calculate Choppiness Index using the built-in function
ciValue = ta.ci(length)
// Plot the CI line
plot(ciValue, title="Choppiness Index", color=color.new(color.blue, 0), linewidth=2)
// Plot standard reference levels
hline(61.8, "Choppy Zone (61.8)", color=color.new(color.red, 0), linestyle=hline.style_dashed)
hline(38.2, "Trending Zone (38.2)", color=color.new(color.green, 0), linestyle=hline.style_dashed)
// Highlight background zones for visual clarity
bgcolor(ciValue > 61.8 ? color.new(color.red, 90) : na, title="Highly Choppy")
bgcolor(ciValue < 38.2 ? color.new(color.green, 90) : na, title="Highly Trending")
Above 61.8 (often 60-70 range) = choppy. Below 38.2 (often 30-40 range) = trending. Between these values = transition.
The fundamental use of the Choppiness Index is to determine whether the market is currently trending or ranging, which helps dictate the appropriate trading strategy.
//@version=5
strategy("CI Market Environment Strategy", overlay=true)
length = input.int(14, title="CI Length", minval=1)
choppyThreshold = input.float(61.8, title="Choppy Threshold")
trendingThreshold = input.float(38.2, title="Trending Threshold")
ciValue = ta.ci(length)
plot(ciValue, "CI", color.blue, display=display.pane_only)
hline(choppyThreshold, "Choppy Line", color.red, linestyle=hline.style_dashed, display=display.pane_only)
hline(trendingThreshold, "Trending Line", color.green, linestyle=hline.style_dashed, display=display.pane_only)
// Define market states based on CI
isChoppy = ciValue > choppyThreshold
isTrending = ciValue < trendingThreshold
isTransitioning = not isChoppy and not isTrending
// Example strategy based on market environment
// You would combine this with actual entry/exit logic suitable for each environment.
// For illustration, we'll just show entry signals.
// If trending, attempt a simple MA crossover strategy
maLength = input.int(20, title="MA Length for Trend", minval=1)
maValue = ta.sma(close, maLength)
if (isTrending)
if (close > maValue and close[1] <= maValue[1]) // Simple bullish MA crossover during trending
strategy.entry("TrendLong", strategy.long)
if (close < maValue and close[1] >= maValue[1]) // Simple bearish MA crossover during trending
strategy.entry("TrendShort", strategy.short)
// If choppy, maybe look for range-bound entries (e.g., at support/resistance levels)
// This part is conceptual as it requires defining support/resistance.
if (isChoppy)
// strategy.entry("RangeBuy", strategy.long, when=isAtSupportAndRanging)
// strategy.entry("RangeSell", strategy.short, when=isAtResistanceAndRanging)
// For now, let's just close trending trades if market becomes choppy
strategy.close("TrendLong", when=isChoppy)
strategy.close("TrendShort", when=isChoppy)
A significant drop in CI from high levels (e.g., above 61.8) to low levels (below 38.2) can signal the initiation of a new trend, or the end of a long consolidation phase.
//@version=5
strategy("CI Trend Initiation Strategy", overlay=true)
length = input.int(14, title="CI Length", minval=1)
choppyThreshold = input.float(61.8, title="Choppy Threshold")
trendingThreshold = input.float(38.2, title="Trending Threshold")
ciValue = ta.ci(length)
plot(ciValue, "CI", color.blue, display=display.pane_only)
hline(choppyThreshold, "Choppy Line", color.red, linestyle=hline.style_dashed, display=display.pane_only)
hline(trendingThreshold, "Trending Line", color.new(color.green, 0), linestyle=hline.style_dashed, display=display.pane_only)
// Detect transition from choppy to trending
fromChoppyToTrending = ciValue < trendingThreshold and ciValue[1] > choppyThreshold
plotshape(fromChoppyToTrending, title="Trend Initiating", location=location.belowbar, color=color.new(color.purple, 0), style=shape.diamond, size=size.small)
alertcondition(fromChoppyToTrending, "CI Trend Initiation", "Choppiness Index indicating start of a new trend.")
// Example: Enter long if market starts trending up after being choppy
// Needs a directional filter on price
if (fromChoppyToTrending)
if (close > open) // Simple bullish candle confirmation
strategy.entry("BreakoutLong", strategy.long)
else if (close < open) // Simple bearish candle confirmation
strategy.entry("BreakoutShort", strategy.short)
To get the most from the Choppiness Index in Pinescript:
The CI is primarily a filter for strategy selection. Don't try to trade the CI itself; rather, use it to decide *when* to use a trend-following strategy and *when* to use a range-bound strategy (or avoid trading).
The Choppiness Index (CI) is a valuable and often underutilized technical indicator in Pinescript for TradingView. Its primary strength lies in its ability to quantify the degree of trending versus ranging behavior in the market, allowing traders to adapt their strategies to the prevailing market environment. By understanding its calculation, thoughtfully tuning its parameters and thresholds, and integrating it strategically with directional indicators and price action analysis, you can leverage the CI to make more informed decisions about when to pursue trend-following opportunities and when to exercise caution or switch to range-bound approaches. It acts as an excellent "market filter" for a robust trading system.
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