Concept 35 / 52·Volume & Flow·Cumulative volume
‹ Multi-Timeframe Trend FilterOpening Range Breakout ›

On-Balance Volume (OBV) in Pine Script

OBV · on balance volume · cumulative signed volume · volume trend line

A running total that adds a bar's volume when the close rises and subtracts it when the close falls, so the line shows whether volume is flowing in or out over time.

Reads best on: Stocks, index futures and exchange-traded crypto on daily charts, where volume is real and accumulation plays out over weeks. Weak on spot forex tick volume and noisy on very short intraday bars.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
01

Definition

On-balance volume (OBV) is a running total of volume signed by the direction of each bar's close. When a bar closes above the previous close its whole volume is added; when it closes below, its whole volume is subtracted; an unchanged close adds nothing. It belongs to the cumulative-volume family, where the line has no fixed scale and only its direction and shape carry meaning. The absolute number depends on where the total started, so two charts of the same market can show different values and identical turns.

The line measures whether volume is arriving on up bars or on down bars, which is a rough proxy for accumulation and distribution. In a healthy advance OBV makes new highs with price, because the up days carry the volume. When price pushes to a new high but OBV does not, the rally is being carried by fewer participants and the divergence warns of a weak move. The reverse holds in a decline. OBV often turns before price because volume shifts before the trend visibly ends.

Joseph Granville introduced OBV in 1963 as a way of reading informed money in stock volume before charts were electronic. It has no length parameter: the only common addition is a moving average of the line, typically 20 or 30 bars, which turns the shape into a signal. The implementation plots OBV with a 20-bar exponential average and marks a long when OBV crosses above the average and a short when it crosses below.

On the chart expect a jagged line that trends when price trends and flattens in ranges. A single high-volume bar produces a visible step, up or down, that stays in the line for good. Because every bar's full volume is counted regardless of how small the close-to-close change was, a one-tick up close on heavy volume moves OBV as much as a limit-up day. Read the line for its slope, its highs and lows against price, and its position against its own average, never for its value.

02

Reading the chart

OBV is read as a shape against price, so identification is a comparison, not a threshold.

  1. Confirm the market reports real volume. Stocks, futures and exchange-traded crypto qualify; spot forex tick counts produce an OBV line that measures activity rather than flow.
  2. Look at the slope of the line over the same window as the price trend. Rising OBV with rising price is confirmation; flat or falling OBV under rising price is a warning.
  3. Compare the swing highs and lows. Price at a new high with OBV below its previous high is bearish divergence; price at a new low with OBV above its previous low is bullish divergence.
  4. Add a moving average of OBV, 20 bars by default. OBV above a rising average says volume favours the buyers; a cross below the average is the first sign that the flow has turned.
  5. Watch OBV at range boundaries. When price is still inside a range but OBV has already broken out, the line is leading and the price break is more likely to hold.
  6. Check the steps. A large single-bar jump in OBV came from one heavy bar; find it on the chart and read its close before treating the new level as a trend.
03

The maths

OBV adds or subtracts each bar's full volume according to the direction of its close and keeps the running total.

OBVt={OBVt−1+VtCt>Ct−1OBVt−1−VtCt<Ct−1OBVt−1Ct=Ct−1\mathrm{OBV}_t = \begin{cases} \mathrm{OBV}_{t-1} + V_t & C_t > C_{t-1} \\ \mathrm{OBV}_{t-1} - V_t & C_t < C_{t-1} \\ \mathrm{OBV}_{t-1} & C_t = C_{t-1} \end{cases}
Signalt=EMAn(OBVt)\mathrm{Signal}_t = \mathrm{EMA}_n(\mathrm{OBV}_t)
OBVt\mathrm{OBV}_ton-balance volume at the current bar
OBVt−1\mathrm{OBV}_{t-1}on-balance volume at the previous bar
VtV_tvolume of the current bar
CtC_tclose of the current bar
Ct−1C_{t-1}close of the previous bar
Signalt\mathrm{Signal}_tthe exponential moving average of OBV used for the crosses
nnthe signal average length, 20 by default

The starting value is arbitrary. The implementation begins the total at the first bar loaded on the chart, so the level changes with the amount of history and only the shape is comparable.

Every bar contributes its full volume whatever the size of the close-to-close change. A one-tick up close on a heavy bar counts the same as a wide up bar, which is the main weakness of the method.

The signal line is an exponential average of OBV, 20 bars by default. The script colours OBV green above the average and red below it and fires its long and short marks on the crosses.

Variants that scale each bar's contribution by where the close sits in the bar's range address the full-volume problem; they are separate indicators with their own names.

04

Inputs that matter

InputTypicalWhat changing it does
Signal length20A longer average smooths the crosses and cuts whipsaws in ranges; a shorter one turns earlier and gives more false crosses.
Price basisclose against previous closeSome variants sign the bar by close against open instead, which reads gaps differently; the classic close-to-close rule is the one traders compare across charts.
TimeframedailyOn daily bars the line tracks multi-week accumulation; on intraday bars the open and close volume dominate and the line jumps at the session boundaries.
05

In practice

  • As trend confirmation: a rally with OBV making higher highs alongside price is carried by volume and is trusted; a rally with OBV lagging is treated as suspect until the line catches up.
  • As a divergence tool: a new price high without a new OBV high, or a new price low without a new OBV low, is a warning that the move is thinning, and stops are tightened.
  • As a leading breakout read: OBV breaking above its own range while price is still inside it says accumulation has started, and the price breakout has volume behind it.
  • As a crossover trigger: the script marks a long when OBV crosses above its 20-bar average and a short when it crosses below, which is the mechanical version of reading the slope.
  • As a filter for other entries: a moving-average cross or a pattern in the direction of the OBV slope is preferred over one against it.
  • As a check on gaps and news bars: a large step in the line shows where the volume arrived, and whether the step held tells you if the move was accepted.
06

Where it goes wrong

  • Reading the level. The number depends on where the total started; only the slope, the swing points and the average cross carry information.
  • Trusting it on tick-volume feeds. Spot forex volume is a count of price changes, so the line measures activity, not flow; use it on markets with real volume.
  • Reacting to one step. A single heavy bar moves OBV permanently; find the bar and read it before calling the new level a trend.
  • Trading every average cross in a range. OBV flattens in ranges and crosses its average often; use the crosses in trends and the divergences at range edges.
07

Compared with

Money Flow Index. MFI weights each bar's typical price change by volume and squeezes the result into 0 to 100 over a lookback, so it has overbought and oversold zones; OBV keeps an unbounded running total with no lookback. MFI is an oscillator, OBV is a cumulative line.

Relative Volume. Relative volume asks whether one bar's size is unusual and ignores its direction; OBV signs every bar by its close and accumulates them to show the direction of flow over time. One reads events, the other reads the trend of participation.

Accumulation/Distribution Line. The accumulation/distribution line scales each bar's volume by where the close sits in the bar's range, so a close near the high adds most of the volume and a mid-range close adds little; OBV adds all of it whenever the close is up.

VWAP. VWAP uses volume to weight price and produces a price level; OBV uses price direction to sign volume and produces a flow line with no price scale. They answer different questions and are not substitutes.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Open On-Balance Volume (OBV) on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • What does the OBV value mean?

    Nothing on its own. OBV is a running total that started at an arbitrary point, so the same market shows different numbers on charts with different history. The information is in the slope, in whether OBV confirms price highs and lows, and in its position against its own moving average. Compare shapes, not levels.

  • Does OBV work on forex?

    Poorly on spot forex, because most feeds report tick count rather than traded volume. The line still rises and falls with activity, but activity is not flow. On currency futures, where volume is real, OBV reads the same way as on stocks. Crypto on exchanges that report traded size is fine.

  • Why does OBV lead price?

    Because volume tends to shift before the trend visibly turns. Distribution near a top happens on heavy down days inside a still-rising range, which pulls OBV down before price breaks. Accumulation near a bottom does the reverse. The lead is real but irregular, and a divergence can persist for a long time before price follows.

  • What is the best moving average length for OBV?

    Twenty bars is the common default and the one the implementation ships with. A longer average, 30 to 50 bars, suits daily charts and swing trades where whipsaws are the main cost; a shorter one suits intraday charts where the line moves quickly. Change it to match how long you hold.

  • How is OBV different from a volume histogram?

    The histogram shows each bar's volume in isolation with no memory. OBV signs each bar's volume by its close and keeps the total, so it shows the balance of up-volume and down-volume pressure over weeks. A heavy bar is one tall column on the histogram and a permanent step on OBV.

  • Should I use OBV on intraday charts?

    It works, with two cautions. The session open and close carry most of the volume, so the line jumps at those bars and drifts in between, and a large gap at the open adds the whole opening bar's volume in one step. Daily bars give a cleaner line; on intraday charts read OBV within the session and lengthen the average.