Master this powerful volume-based momentum indicator in TradingView's Pine Script that measures buying and selling pressure to confirm trends and anticipate price movements.
On-Balance Volume (OBV) is a momentum indicator that relates volume to price change. Developed by Joe Granville, OBV is a cumulative total of volume, adding or subtracting volume based on whether the price closed higher or lower than the previous day. The core idea behind OBV is that volume precedes price. A rising OBV indicates that volume is flowing into an asset, suggesting accumulation (buying pressure), which often precedes price increases. Conversely, a falling OBV indicates distribution (selling pressure), often preceding price declines.
OBV is a leading or confirming indicator, providing insights into whether smart money is accumulating or distributing an asset. It helps traders confirm trend direction, identify potential reversals through divergence, and spot breakout validity.
In Pine Script, OBV is a crucial tool for understanding the underlying strength of price movements and anticipating future trends by observing volume flow.
The calculation of On-Balance Volume is quite simple and cumulative:
//@version=5
indicator("My On-Balance Volume (OBV)", overlay=false)
// Calculate OBV using the built-in function
obvValue = ta.obv(close, volume)
// Plot the OBV line
plot(obvValue, title="OBV", color=color.blue, linewidth=2)
// Optional: Add a Moving Average of OBV to act as a signal line
obvMALength = input.int(20, title="OBV MA Length", minval=1)
obvMA = ta.sma(obvValue, obvMALength)
// Plot the OBV Moving Average (signal line)
plot(obvMA, title="OBV MA", color=color.orange, linewidth=1)
The core philosophy of OBV is that major price moves are often preceded by significant volume flow. OBV quantifies this accumulation/distribution.
Divergence between price and OBV is one of the most powerful signals generated by the indicator, suggesting a weakening trend and potential reversal.
//@version=5
strategy("OBV Divergence Strategy", overlay=true)
obvValue = ta.obv(close, volume)
plot(obvValue, "OBV", color.blue, display=display.pane_only)
bullishDiv = close < close[1] and close[1] < close[2] and obvValue > obvValue[1] and obvValue[1] > obvValue[2]
bearishDiv = close > close[1] and close[1] > close[2] and obvValue < obvValue[1] and obvValue[1] < obvValue[2]
plotshape(bullishDiv, title="Bullish OBV Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDiv, title="Bearish OBV Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
if (bullishDiv)
strategy.entry("Long Divergence", strategy.long)
if (bearishDiv)
strategy.entry("Short Divergence", strategy.short)
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)
OBV can confirm the strength and direction of a price trend.
//@version=5
strategy("OBV Trend Confirmation Strategy", overlay=true)
obvValue = ta.obv(close, volume)
obvMALength = input.int(20, title="OBV MA Length", minval=1)
obvMA = ta.sma(obvValue, obvMALength)
plot(obvValue, "OBV", color.blue, display=display.pane_only)
plot(obvMA, "OBV MA", color.orange, display=display.pane_only)
priceMALength = input.int(50, title="Price MA Length", minval=1)
priceMA = ta.sma(close, priceMALength)
longConfirm = close > priceMA and obvValue > obvMA
shortConfirm = close < priceMA and obvValue < obvMA
if (longConfirm)
strategy.entry("Long Confirm", strategy.long)
if (shortConfirm)
strategy.entry("Short Confirm", strategy.short)
strategy.close("Long Confirm", when=close < priceMA or obvValue < obvMA)
strategy.close("Short Confirm", when=close > priceMA or obvValue > obvMA)
Sometimes, OBV will break out of its own trading range before price does. This can act as an early warning signal for an impending price breakout.
//@version=5
indicator("OBV Breakout Detection", overlay=true)
obvValue = ta.obv(close, volume)
obvMALength = input.int(20, title="OBV MA Length", minval=1)
obvMA = ta.sma(obvValue, obvMALength)
plot(obvValue, "OBV", color.blue)
plot(obvMA, "OBV MA", color.orange)
obvHighest = ta.highest(obvValue, 20)
obvLowest = ta.lowest(obvValue, 20)
bullishObvBreakout = obvValue > obvHighest[1] and obvValue[1] <= obvHighest[1]
bearishObvBreakout = obvValue < obvLowest[1] and obvValue[1] >= obvLowest[1]
plotshape(bullishObvBreakout, title="Bullish OBV Breakout", location=location.belowbar, color=color.lime, style=shape.arrowup, size=size.small)
plotshape(bearishObvBreakout, title="Bearish OBV Breakout", location=location.abovebar, color=color.maroon, style=shape.arrowdown, size=size.small)
alertcondition(bullishObvBreakout, "OBV Bullish Breakout", "OBV has broken out upwards, anticipate price move.")
alertcondition(bearishObvBreakout, "OBV Bearish Breakout", "OBV has broken out downwards, anticipate price move.")
To get the most from On-Balance Volume in Pine Script:
OBV helps you see if a price move has real conviction (volume backing it) or if it's just noise (low volume).
On-Balance Volume (OBV) is a foundational and highly effective volume-based momentum indicator available in Pine Script for TradingView. By uniquely quantifying buying and selling pressure through cumulative volume, it provides traders with invaluable insights into the market's underlying strength and potential future price movements. While its greatest strength lies in identifying powerful divergences with price, it also excels at confirming existing trends and validating breakouts. By understanding its calculation, thoughtfully utilizing its signals (especially divergence), and integrating it strategically with price action and other technical analysis tools, you can leverage OBV to enhance your trading strategies and gain a clearer understanding of market conviction.
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.
Get Pine Script Strategy