# VPIN

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

Full page: https://docs.thefintechbuilder.com/market-microstructure/order-flow-and-impact/vpin/
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 { vpin } from "fintech-algorithms/market-microstructure/order-flow-and-impact/vpin";
```

## Signature

```ts
vpin(inputRows, bucketVolume, windowBuckets, includePartial)
```

Volume-synchronised probability of informed trading: order imbalance measured in volume buckets rather than clock time. Proposed as a flash-crash early warning, and contested — its predictive claims are actively disputed in the literature.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `inputRows` | `Row[]` | yes | Trades or volume bars with signed or classifiable volume. |
| `bucketVolume` | `number` | yes | Volume that closes a bucket. The choice materially changes the series. · min: 0 |
| `windowBuckets` | `number` | yes | Buckets averaged into each VPIN reading. · min: 1, integer: true |
| `includePartial` | `boolean` | yes | Whether a final incomplete bucket is included. |

## Returns

`{ vpin, buckets, window_buckets, … }`

The VPIN series with the buckets it was computed from.

## Errors

- When bucketVolume is not positive — throws

## Complexity

Time `O(n)`, space `O(buckets)`.

## 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": "V001",
    "event_time_ns": 0,
    "buy_volume": 50,
    "sell_volume": 50
  },
  {
    "id": "V002",
    "event_time_ns": 1000000,
    "buy_volume": 54.59220119,
    "sell_volume": 45.40779881
  },
  {
    "id": "V003",
    "event_time_ns": 2000000,
    "buy_volume": 58.48528137,
    "sell_volume": 41.51471863
  }
]
```

Showing 3 of 60 elements.

`bucketVolume`:

```json
100
```

`windowBuckets`:

```json
10
```

`includePartial`:

```json
false
```

### Call

```ts
vpin(inputRows, bucketVolume, windowBuckets, includePartial)
```

### Returns

object with 13 fields: model, state, bucket_volume, window_buckets, include_partial, input_event_count, bucket_count, dropped_partial_volume, …

```json
{
  "model": "volume-synchronized-probability-of-informed-trading",
  "state": "estimated",
  "bucket_volume": 100,
  "window_buckets": 10,
  "include_partial": false,
  "input_event_count": 60,
  "bucket_count": 60,
  "dropped_partial_volume": 0,
  "valid_vpin_count": 51,
  "last_vpin": 0.1597998193,
  "mean_vpin": 0.3604789898,
  "max_vpin": 0.86,
  "trace": [
    {
      "id": "V001",
      "index": 0,
      "buy_volume": 50,
      "sell_volume": 50,
      "total_volume": 100,
      "absolute_imbalance": 0,
      "imbalance_fraction": 0,
      "source_event_index": 0,
      "vpin": null,
      "window_count": 1,
      "side": "warm-up",
      "reason": "insufficient-buckets"
    },
    {
      "id": "V002",
      "index": 1,
      "buy_volume": 54.59220119,
      "sell_volume": 45.40779881,
      "total_volume": 100,
      "absolute_imbalance": 9.18440238,
      "imbalance_fraction": 0.0918440238,
      "source_event_index": 1,
      "vpin": null,
      "window_count": 2,
      "side": "warm-up",
      "reason": "insufficient-buckets"
    },
    {
      "id": "V003",
      "index": 2,
      "buy_volume": 58.48528137,
      "sell_volume": 41.51471863,
      "total_volume": 100,
      "absolute_imbalance": 16.97056274,
      "imbalance_fraction": 0.1697056274,
      "source_event_index": 2,
      "vpin": null,
      "window_count": 3,
      "side": "warm-up",
      "reason": "insufficient-buckets"
    }
  ]
}
```

## Other exports

`orderFlowImbalance`, `queueImbalance`, `kyleLambda`, `hasbrouckPriceImpact`, `pin`, `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/vpin/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/order-flow-and-impact/vpin/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
