# Order Flow Imbalance

`D11-F03-A01` · Market Microstructure → Order-Flow and Impact · archetype `record-transform` · difficulty 3/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-microstructure/order-flow-and-impact/order-flow-imbalance/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

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

## Signature

```ts
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

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `inputRows` | `Row[]` | yes | Sequential top-of-book observations with prices and sizes on both sides. |
| `resetOnSession` | `boolean` | yes | Whether the cumulative measure restarts each session. Carrying it overnight mixes regimes. |
| `normalizeByDepth` | `boolean` | yes | Divide 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

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`:

```json
[
  {
    "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`:

```json
true
```

`normalizeByDepth`:

```json
false
```

### Call

```ts
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, …

```json
{
  "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

`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.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/market-microstructure/order-flow-and-impact/order-flow-imbalance/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/order-flow-and-impact/order-flow-imbalance/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
