# Depth-Weighted Midprice

`D11-F04-A02` · Market Microstructure → Order-Book Dynamics · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-microstructure/order-book-dynamics/depth-weighted-midprice/
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 { depthWeightedMidprice } from "fintech-algorithms/market-microstructure/order-book-dynamics/depth-weighted-midprice";
```

## Signature

```ts
depthWeightedMidprice(bidPrices, bidSizes, askPrices, askSizes)
```

A midpoint weighted by depth across several levels rather than the naive average of best bid and ask. Less prone to jumping when a single small order sits at the touch.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bidPrices` | `number[]` | yes | Bid prices, best first. |
| `bidSizes` | `number[]` | yes | Sizes aligned with bidPrices. |
| `askPrices` | `number[]` | yes | Ask prices, best first. |
| `askSizes` | `number[]` | yes | Sizes aligned with askPrices. |

## Returns

`{ midprice, simple_midpoint, weighted_bid, weighted_ask, … }`

The depth-weighted midpoint alongside the naive one, so the difference is visible.

## Errors

- When total depth on either side is zero — throws

## Complexity

Time `O(levels)`, space `O(1)`.

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

`bidPrices`:

```json
[99.99, 99.98, 99.97]
```

`bidSizes`:

```json
[500, 1000, 1500]
```

`askPrices`:

```json
[100.01, 100.02, 100.03]
```

`askSizes`:

```json
[300, 700, 2000]
```

### Call

```ts
depthWeightedMidprice(bidPrices, bidSizes, askPrices, askSizes)
```

### Returns

object with 13 fields: model, level_count, bid_level_count, ask_level_count, top_midpoint, depth_weighted_bid, depth_weighted_ask, depth_weighted_midprice, …

```json
{
  "model": "same-side-depth-weighted-midpoint",
  "level_count": 3,
  "bid_level_count": 3,
  "ask_level_count": 3,
  "top_midpoint": 100,
  "depth_weighted_bid": 99.97666666666667,
  "depth_weighted_ask": 100.02566666666667,
  "depth_weighted_midprice": 100.00116666666668,
  "shift": 0.0011666666666769743,
  "shift_bps": 0.11666666666769743,
  "bid_total_depth": 3000,
  "ask_total_depth": 3000,
  "state": "above-top-mid"
}
```

## Other exports

`orderBookSlope`, `microprice`, `orderBookResiliency`, `hawkesOrderArrival`, `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/order-book-dynamics/depth-weighted-midprice/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/order-book-dynamics/depth-weighted-midprice/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
