# Expected Market-Order Fill Price

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

Full page: https://docs.thefintechbuilder.com/market-microstructure/market-depth-analytics/expected-market-order-fill-price/
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 { expectedFillPrice } from "fintech-algorithms/market-microstructure/market-depth-analytics/expected-market-order-fill-price";
```

## Signature

```ts
expectedFillPrice(bids, asks, side, quantity, limitPrice)
```

Walks a market order through the book and returns the volume-weighted price it would pay. The honest answer to 'what will this cost' — and it can be a partial fill, which a naive estimate silently ignores.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bids` | `Level[]` | yes | Bid levels, best first. |
| `asks` | `Level[]` | yes | Ask levels, best first. |
| `side` | `"buy" \| "sell"` | yes | Order direction, which selects the side consumed. |
| `quantity` | `number` | yes | Order quantity. · min: 0 |
| `limitPrice` | `number` | no | Optional limit beyond which the order stops consuming levels. |

## Returns

`{ average_price, filled_quantity, unfilled_quantity, levels_consumed, … }`

The average price with the filled and **unfilled** quantities stated separately — a book too thin to fill the order is the case that matters.

## Errors

- When quantity is not positive, or side is unrecognised — 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

`bids`:

```json
[
  {
    "price": 99.99,
    "quantity": 500
  },
  {
    "price": 99.98,
    "quantity": 800
  },
  {
    "price": 99.97,
    "quantity": 1200
  }
]
```

Showing 3 of 5 elements.

`asks`:

```json
[
  {
    "price": 100.01,
    "quantity": 300
  },
  {
    "price": 100.02,
    "quantity": 600
  },
  {
    "price": 100.03,
    "quantity": 1000
  }
]
```

Showing 3 of 5 elements.

`side`:

```json
"buy"
```

`quantity`:

```json
1500
```

`limitPrice`:

```json
null
```

### Call

```ts
expectedFillPrice(bids, asks, side, quantity, limitPrice)
```

### Returns

object with 15 fields: model, side, requested_quantity, filled_quantity, unfilled_quantity, full_fill, fills, fill_notional, …

```json
{
  "model": "deterministic-visible-book-fill-estimate",
  "side": "buy",
  "requested_quantity": 1500,
  "filled_quantity": 1500,
  "unfilled_quantity": 0,
  "full_fill": true,
  "fills": [
    {
      "level_index": 0,
      "price": 100.01,
      "quantity": 300,
      "notional": 30003
    },
    {
      "level_index": 1,
      "price": 100.02,
      "quantity": 600,
      "notional": 60012
    },
    {
      "level_index": 2,
      "price": 100.03,
      "quantity": 600,
      "notional": 60018
    }
  ],
  "fill_notional": 150033,
  "partial_vwap": 100.022,
  "worst_fill_price": 100.03,
  "best_bid": 99.99,
  "best_ask": 100.01,
  "midpoint": 100,
  "expected_fill_price": 100.022
}
```

Showing 14 of 15 fields.

## Other exports

`cumulativeDepth`, `topNDepthImbalance`, `depthAtDistanceProfile`, `sweepCostAndSlippage`, `liquidityWallConcentration`, `depthDepletionReplenishment`, `marketDepthHeatmap`, `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/expected-market-order-fill-price/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/market-depth-analytics/expected-market-order-fill-price/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
