# Microprice

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

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

## Signature

```ts
microprice(bid, bidSize, ask, askSize)
```

The midpoint weighted by the *opposite* side's size, so a heavy bid pulls the price up. Counter-intuitive until you see the derivation — the imbalance predicts where the midpoint is going, not where it is.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bid` | `number` | yes | Best bid price. |
| `bidSize` | `number` | yes | Size at the best bid. · min: 0 |
| `ask` | `number` | yes | Best ask price. |
| `askSize` | `number` | yes | Size at the best ask. · min: 0 |

## Returns

`{ microprice, midpoint, imbalance, … }`

The microprice with the imbalance that tilted it.

## Errors

- When both sizes are zero, or ask is below bid — throws

## Complexity

Time `O(1)`, 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

`bid`:

```json
99.99
```

`bidSize`:

```json
800
```

`ask`:

```json
100.01
```

`askSize`:

```json
200
```

### Call

```ts
microprice(bid, bidSize, ask, askSize)
```

### Returns

object with 13 fields: model, bid, ask, bid_size, ask_size, midpoint, spread, queue_imbalance, …

```json
{
  "model": "top-of-book-imbalance-weighted-quote",
  "bid": 99.99,
  "ask": 100.01,
  "bid_size": 800,
  "ask_size": 200,
  "midpoint": 100,
  "spread": 0.020000000000010232,
  "queue_imbalance": 0.6,
  "microprice": 100.006,
  "shift": 0.006000000000000227,
  "shift_bps": 0.6000000000000227,
  "identity_error": 0,
  "state": "bid-pressure"
}
```

## Other exports

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