fintech-algorithms

On-Balance Volume (OBV)

Install and import

bash
npm install fintech-algorithms
ts
import { obv } from "fintech-algorithms/technical-indicators/volume-indicators/obv";

Signature

obv(close, volume, initial)

On-balance volume: a running total that adds the bar's volume when price closed up and subtracts it when price closed down. The level is arbitrary; only its direction carries information.

Parameters

NameTypeNotes
closenumber[]Per-bar closing prices, chronological.
volumenumber[]Per-bar traded volume, aligned index-for-index with the price series.
initialnumberStarting value of the running total. Only affects the level, never the shape.
optional

Returns

Record<string, (number | null)[]> · length same-as-input

Parallel series: direction, volume, signed_volume and the running obv.

Warm-up

The first 1 positions are null. The first bar has no prior close, so it has no direction.

Errors

  • When the input series are not all the same length — throws RangeError

Complexity: time O(n), space O(n).

Worked example

executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

Input

close
[100.28, 100.686113, 101.200346, 101.789282, 102.408843, 103.010585]

Showing 6 of 120 elements.

volume
[990000, 1066468.036, 1130033.236, 1170141.961, 1180511.26, 1160272.794]

Showing 6 of 120 elements.

Call

obv(close, volume, initial)

Returns

object with 4 fields: direction, volume, signed_volume, obv

{
  "direction": [0, 1, 1, 1, 1, 1],
  "volume": [990000, 1066468.036, 1130033.236, 1170141.961, 1180511.26, 1160272.794],
  "signed_volume": [0, 1066468.036, 1130033.236, 1170141.961, 1180511.26, 1160272.794],
  "obv": [0, 1066468.036, 2196501.272, 3366643.233, 4547154.493, 5707427.287]
}

Other exports

This module also exports accumulation_distribution_line, chaikin_money_flow, money_flow_index, volume_price_trend, force_index. Every module additionally exports run as an alias of its primary function, and a meta object carrying its catalog id, domain, family, shape and article URL.

Diagrams

On-Balance Volume (OBV) — obv comparison
On-Balance Volume (OBV) — obv mechanism

How it works

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References

  • On Balance Volume (OBV) — TradingView
  • Volume — TradingView
  • TA-Lib function API and volume indicators — TA-Lib
  • Consolidated Tape — U.S. Securities and Exchange Commission, Investor.gov
  • Closing Price — U.S. Securities and Exchange Commission, Investor.gov
  • What is Volume? — CME Group
  • Claim-role ledger
  • Evidence boundary
  • Enhanced claim-to-source map
  • Public evidence boundary