# Market-Depth Heatmap Aggregation

`D11-F05-A08` · Market Microstructure → Market-Depth Analytics · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-microstructure/market-depth-analytics/market-depth-heatmap-aggregation/
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 { marketDepthHeatmap } from "fintech-algorithms/market-microstructure/market-depth-analytics/market-depth-heatmap-aggregation";
```

## Signature

```ts
marketDepthHeatmap(snapshotsRaw, tickRaw, binRaw, maxRaw)
```

Aggregates a sequence of book snapshots into a time-by-price grid — the data behind a depth heatmap, where persistent liquidity and fleeting quotes look completely different.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `snapshotsRaw` | `Snapshot[]` | yes | Sequential book snapshots. |
| `tickRaw` | `number` | yes | Tick size. · min: 0 |
| `binRaw` | `number` | yes | Price bin width in ticks. · min: 0 |
| `maxRaw` | `number` | yes | Maximum distance from the touch to include. · min: 0 |

## Returns

`{ grid, bins, snapshots, … }`

The aggregated grid with its bin definitions.

## Errors

- When tick or bin width is not positive — throws

## Complexity

Time `O(snapshots × levels)`, space `O(bins × snapshots)`.

## 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

`snapshotsRaw`:

```json
[
  {
    "timestamp_seconds": 0,
    "bids": [
      {
        "price": 99.99,
        "quantity": 500
      },
      {
        "price": 99.98,
        "quantity": 700
      },
      {
        "price": 99.97,
        "quantity": 420
      }
    ],
    "asks": [
      {
        "price": 100.01,
        "quantity": 360
      },
      {
        "price": 100.02,
        "quantity": 620
      },
      {
        "price": 100.03,
        "quantity": 460
      }
    ]
  },
  {
    "timestamp_seconds": 10,
    "bids": [
      {
        "price": 99.99,
        "quantity": 520
      },
      {
        "price": 99.98,
        "quantity": 715
      },
      {
        "price": 99.97,
        "quantity": 430
      }
    ],
    "asks": [
      {
        "price": 100.01,
        "quantity": 375
      },
      {
        "price": 100.02,
        "quantity": 630
      },
      {
        "price": 100.03,
        "quantity": 468
      }
    ]
  },
  {
    "timestamp_seconds": 20,
    "bids": [
      {
        "price": 99.99,
        "quantity": 540
      },
      {
        "price": 99.98,
        "quantity": 730
      },
      {
        "price": 99.97,
        "quantity": 440
      }
    ],
    "asks": [
      {
        "price": 100.01,
        "quantity": 390
      },
      {
        "price": 100.02,
        "quantity": 640
      },
      {
        "price": 100.03,
        "quantity": 476
      }
    ]
  }
]
```

Showing 3 of 12 elements.

`tickRaw`:

```json
0.01
```

`binRaw`:

```json
30
```

`maxRaw`:

```json
2
```

### Call

```ts
marketDepthHeatmap(snapshotsRaw, tickRaw, binRaw, maxRaw)
```

### Returns

object with 9 fields: model, snapshot_interval_seconds, time_bin_seconds, max_distance_ticks, time_bin_count, book_coordinates, cells, peak_cell, …

```json
{
  "model": "regular-snapshot-inside-relative-depth-heatmap",
  "snapshot_interval_seconds": 10,
  "time_bin_seconds": 30,
  "max_distance_ticks": 2,
  "time_bin_count": 4,
  "book_coordinates": [-3, -2, -1, 1, 2, 3],
  "cells": [
    {
      "time_bin_index": 0,
      "time_bin_start_seconds": 0,
      "book_coordinate": -3,
      "mean_quantity": 430,
      "max_quantity": 440,
      "observation_count": 3
    },
    {
      "time_bin_index": 0,
      "time_bin_start_seconds": 0,
      "book_coordinate": -2,
      "mean_quantity": 715,
      "max_quantity": 730,
      "observation_count": 3
    },
    {
      "time_bin_index": 0,
      "time_bin_start_seconds": 0,
      "book_coordinate": -1,
      "mean_quantity": 520,
      "max_quantity": 540,
      "observation_count": 3
    }
  ],
  "peak_cell": {
    "time_bin_index": 3,
    "time_bin_start_seconds": 90,
    "book_coordinate": -2,
    "mean_quantity": 850,
    "max_quantity": 865,
    "observation_count": 3
  },
  "state": "bid-peak"
}
```

## Other exports

`cumulativeDepth`, `topNDepthImbalance`, `depthAtDistanceProfile`, `expectedFillPrice`, `sweepCostAndSlippage`, `liquidityWallConcentration`, `depthDepletionReplenishment`, `calculate`. 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: **verified** (via D).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

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/market-depth-analytics/market-depth-heatmap-aggregation/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/market-depth-analytics/market-depth-heatmap-aggregation/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
