Master this unique momentum oscillator in TradingView's Pinescript that ingeniously blends price and volume to measure underlying buying and selling pressure, helping confirm trends and anticipate reversals.
The Demand Index is a complex technical indicator that combines price and volume data to provide insights into the forces of supply and demand driving an asset's price. Unlike simpler volume indicators, the Demand Index attempts to determine whether buying or selling pressure is truly dominant, accounting for various aspects of price movement (open, high, low, close) relative to volume. It was developed by James Sibbet and is known for its ability to sometimes signal price reversals before they become apparent in price action alone.
The Demand Index is plotted as an oscillator that fluctuates above and below a zero line. Generally:
The calculation of the Demand Index is quite intricate, combining multiple elements of price and volume for each period (bar). It typically involves the following steps:
//@version=5
indicator("My Demand Index", overlay=false) // overlay=false to plot in a separate pane
// Input for Demand Index length (smoothing period)
length = input.int(5, title="DI Length (Smoothing)", minval=1)
// Calculate Demand Index using the built-in function
// ta.demandindex() takes 'close', 'high', 'low', 'open', and 'volume' implicitly or as source.
// The built-in usually takes 'close' as default source and length for smoothing.
demandIndexValue = ta.demandindex(close, length) // Default source is `close`
// Plot the Demand Index line
plot(demandIndexValue, title="Demand Index", color=color.new(color.blue, 0), linewidth=2)
// Plot the Zero Line as a key reference point
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted)
// Optional: Add a signal line (e.g., an EMA of the Demand Index)
signalLength = input.int(9, title="Signal Line Length", minval=1)
demandIndexSignal = ta.ema(demandIndexValue, signalLength)
plot(demandIndexSignal, title="DI Signal Line", color=color.new(color.orange, 0), linewidth=1)
// Highlight zones above/below zero
fill(demandIndexValue, 0, color=color.new(color.lime, 90), title="Positive Demand")
fill(demandIndexValue, 0, color=color.new(color.red, 90), title="Negative Demand")
The Demand Index helps reveal if underlying buying or selling pressure is truly dominant, even if price action seems ambiguous. It's often used to confirm trend strength.
//@version=5
strategy("DI Zero Line Crossover Strategy", overlay=true)
length = input.int(5, title="DI Length", minval=1)
signalLength = input.int(9, title="Signal Line Length", minval=1)
demandIndexValue = ta.demandindex(close, length)
demandIndexSignal = ta.ema(demandIndexValue, signalLength)
plot(demandIndexValue, "Demand Index", color.blue, display=display.pane_only)
plot(demandIndexSignal, "DI Signal Line", color.orange, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)
// Long entry: DI crosses above zero (and optionally above its signal line)
longCondition = ta.crossover(demandIndexValue, 0) // or ta.crossover(demandIndexValue, demandIndexSignal)
// Short entry: DI crosses below zero (and optionally below its signal line)
shortCondition = ta.crossunder(demandIndexValue, 0) // or ta.crossunder(demandIndexValue, demandIndexSignal)
if (longCondition)
strategy.entry("Long DI Cross", strategy.long)
if (shortCondition)
strategy.entry("Short DI Cross", strategy.short)
// Basic exit: opposite signal
strategy.close("Long DI Cross", when=shortCondition)
strategy.close("Short DI Cross", when=longCondition)
//@version=5
strategy("DI Divergence Strategy", overlay=true)
length = input.int(5, title="DI Length", minval=1)
demandIndexValue = ta.demandindex(close, length)
plot(demandIndexValue, "Demand Index", color.blue, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)
// Simple divergence detection (conceptual, robust detection requires advanced pivot logic)
// This example looks for recent higher/lower swings in price and Demand Index.
// Bullish Divergence: Price lower low, DI higher low
bullishDiv = close < close[1] and close[1] < close[2] and demandIndexValue > demandIndexValue[1] and demandIndexValue[1] > demandIndexValue[2]
// Bearish Divergence: Price higher high, DI lower high
bearishDiv = close > close[1] and close[1] > close[2] and demandIndexValue < demandIndexValue[1] and demandIndexValue[1] < demandIndexValue[2]
// Plot shapes on the chart to indicate divergence
plotshape(bullishDiv, title="Bullish DI Divergence", location=location.belowbar, color=color.new(color.green, 0), style=shape.triangleup, size=size.small)
plotshape(bearishDiv, title="Bearish DI Divergence", location=location.abovebar, color=color.new(color.red, 0), style=shape.triangledown, size=size.small)
if (bullishDiv)
strategy.entry("Long Divergence", strategy.long)
if (bearishDiv)
strategy.entry("Short Divergence", strategy.short)
// Basic exit after a few bars or on opposite signal
strategy.exit("Long Divergence Exit", from_entry="Long Divergence", profit=close*0.02, loss=close*0.01)
strategy.exit("Short Divergence Exit", from_entry="Short Divergence", profit=close*0.02, loss=close*0.01)
//@version=5
indicator("DI Extreme Readings & Return to Zero", overlay=true)
length = input.int(5, title="DI Length", minval=1)
demandIndexValue = ta.demandindex(close, length)
plot(demandIndexValue, "Demand Index", color.blue, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)
// Define thresholds for "extreme" readings (adjust based on asset)
// These values are often small for Demand Index depending on how it's scaled
upperExtremeThreshold = input.float(0.1, title="Upper Extreme Threshold", minval=0.01, step=0.01)
lowerExtremeThreshold = input.float(-0.1, title="Lower Extreme Threshold", maxval=-0.01, step=0.01)
hline(upperExtremeThreshold, "Upper Extreme", color.new(color.green, 0), linestyle=hline.style_dashed, display=display.pane_only)
hline(lowerExtremeThreshold, "Lower Extreme", color.new(color.red, 0), linestyle=hline.style_dashed, display=display.pane_only)
// Conditions for extremes and return to zero
isExtremePositive = demandIndexValue > upperExtremeThreshold
isExtremeNegative = demandIndexValue < lowerExtremeThreshold
// Return to zero from positive extreme
returnToZeroFromPositive = isExtremePositive[1] and demandIndexValue < upperExtremeThreshold
// Return to zero from negative extreme
returnToZeroFromNegative = isExtremeNegative[1] and demandIndexValue > lowerExtremeThreshold
// Highlight backgrounds for visual clarity
bgcolor(isExtremePositive ? color.new(color.lime, 90) : na, title="Extreme Positive DI")
bgcolor(isExtremeNegative ? color.new(color.maroon, 90) : na, title="Extreme Negative DI")
// Alert conditions
alertcondition(returnToZeroFromPositive, "DI Returning from Positive", "Demand Index returning from extreme positive, potential reversal.")
alertcondition(returnToZeroFromNegative, "DI Returning from Negative", "Demand Index returning from extreme negative, potential reversal.")
To get the most from the Demand Index in Pinescript:
The Demand Index can sometimes provide earlier signals of trend changes or exhaustion than purely price-based indicators, as it directly measures the underlying force of demand/supply.
The Demand Index is a sophisticated and highly insightful momentum oscillator available in Pinescript for TradingView. By uniquely combining price changes with volume to measure underlying buying and selling pressure, it offers traders a deeper understanding of market conviction and potential future price movements. While its greatest strength lies in identifying powerful divergences with price (signaling potential trend reversals), it also excels at confirming existing trends and validating shifts in market sentiment through its zero-line crossovers and extreme readings. By understanding its calculation, thoughtfully tuning its parameters, and integrating it strategically with price action and other technical analysis tools, you can leverage the Demand Index to enhance your Pinescript strategies and gain a clearer understanding of the forces driving market movement.
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