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

Order Flow Imbalance

Install and import#

bash
npm install fintech-algorithms
ts
import { orderFlowImbalance } from "fintech-algorithms/market-microstructure/order-flow-and-impact/order-flow-imbalance";

Signature#

orderFlowImbalance(inputRows, resetOnSession, normalizeByDepth)

Net signed change in depth at the touch — the quantity that best explains short-horizon price moves in the microstructure literature. It counts additions and cancellations, not just trades.

Parameters#

NameTypeNotes
inputRowsRow[]Sequential top-of-book observations with prices and sizes on both sides.
resetOnSessionbooleanWhether the cumulative measure restarts each session. Carrying it overnight mixes regimes.
normalizeByDepthbooleanDivide by prevailing depth, which makes the measure comparable across instruments of different thickness.

Returns#

{ ofi, cumulative, rows, … }

Per-observation imbalance and its cumulative path.

Errors#

  • When rows are not in sequence order — throws

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#

inputRows
[
  {
    "id": "E001",
    "session": "S1",
    "event_time_ns": 0,
    "bid_price": 100,
    "bid_size": 500,
    "ask_price": 100.02,
    "ask_size": 520
  },
  {
    "id": "E002",
    "session": "S1",
    "event_time_ns": 1000000,
    "bid_price": 100,
    "bid_size": 573,
    "ask_price": 100.02,
    "ask_size": 567
  },
  {
    "id": "E003",
    "session": "S1",
    "event_time_ns": 2000000,
    "bid_price": 100,
    "bid_size": 996,
    "ask_price": 100.02,
    "ask_size": 614
  }
]

Showing 3 of 60 elements.

resetOnSession
true
normalizeByDepth
false

Call#

orderFlowImbalance(inputRows, resetOnSession, normalizeByDepth)

Returns#

object with 12 fields: model, state, reset_on_session, normalize_by_depth, event_count, classified_event_count, positive_event_count, negative_event_count, …

{
  "model": "cont-best-quote-ofi",
  "state": "estimated",
  "reset_on_session": true,
  "normalize_by_depth": false,
  "event_count": 60,
  "classified_event_count": 59,
  "positive_event_count": 34,
  "negative_event_count": 25,
  "zero_event_count": 0,
  "cumulative_ofi": 58,
  "mean_absolute_event_ofi": 440.5762711864,
  "trace": [
    {
      "id": "E001",
      "index": 0,
      "session": "S1",
      "bid_price": 100,
      "bid_size": 500,
      "ask_price": 100.02,
      "ask_size": 520,
      "event_ofi": 0,
      "cumulative_ofi": 0,
      "side": "balanced",
      "reason": "seed"
    },
    {
      "id": "E002",
      "index": 1,
      "session": "S1",
      "bid_price": 100,
      "bid_size": 573,
      "ask_price": 100.02,
      "ask_size": 567,
      "event_ofi": 26,
      "cumulative_ofi": 26,
      "side": "buy-pressure",
      "reason": "best-quote-event"
    },
    {
      "id": "E003",
      "index": 2,
      "session": "S1",
      "bid_price": 100,
      "bid_size": 996,
      "ask_price": 100.02,
      "ask_size": 614,
      "event_ofi": 376,
      "cumulative_ofi": 402,
      "side": "buy-pressure",
      "reason": "best-quote-event"
    }
  ]
}

Other exports#

This module also exports queueImbalance, kyleLambda, hasbrouckPriceImpact, pin, vpin, runTopic. 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#

Order Flow Imbalance — calculation map
Order Flow Imbalance — decision boundary
Order Flow Imbalance — failure boundary
Order Flow Imbalance — scenario matrix
Order Flow Imbalance — worked example

Calculation flow#

Order Flow Imbalance Calculation Flow
flowchart LR
    A["Point-in-time source records"] --> B["Validate clocks, sequence, and finality"]
    B --> C["Apply Order Flow Imbalance profile"]
    C --> D["Preserve trace and diagnostics"]
    D --> E["Report bounded output"]
    E --> F["Compare profiles"]
    F --> G["Reject unsupported inference"]

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#

The rest of the Order-Flow and Impact family#