fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

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 0 positions are not applicable — no position is null. The first bar has no prior close, so its direction is 0 and the running total starts at initial. Those are defined values rather than warm-up nulls, so every position of every returned series is populated.

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 accumulationDistributionLine, chaikinMoneyFlow, moneyFlowIndex, volumePriceTrend, forceIndex. 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

Calculation flow#

Calculation flow
flowchart LR
  A["Finalized finalized close and nonnegative volume"] --> B["Validate order, alignment, and finite values"]
  B --> C["Close comparison"]
  C --> D["Direction sign"]
  D --> E["Signed volume"]
  E --> F{"History and denominator valid?"}
  F -- "No · short history" --> G["warming-up + reason"]
  F -- "No · no finite scale" --> H["undefined + reason"]
  F -- "Yes" --> I["Cumulative OBV"]
  I --> J["ready + aligned component trace"]
Data-basis reconciliation — On-Balance Volume (OBV)
flowchart LR
    A["Same instrument and identifier?"] -->|Yes| B["Same venue or consolidated scope?"]
    A -->|No| X["Stop: different measurement"]
    B -->|Yes| C["Same volume unit and bar interval?"]
    B -->|No| X
    C -->|Yes| D["Same session and close policy?"]
    C -->|No| X
    D -->|Yes| E["Same corrections and price-volume adjustment basis?"]
    D -->|No| X
    E -->|Yes| F["Same OBV convention, seed, warm-up, and precision?"]
    E -->|No| X
    F -->|Yes| G["Compare intermediate components"]
    F -->|No| Y["Document convention difference"]
    G --> H["Compare final output"]

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

The rest of the Volume Indicators family#