# Order-Book Resiliency

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

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

## Signature

```ts
orderBookResiliency(timesSeconds, displacementBps, forecastSeconds)
```

How quickly the book refills after being depleted, fitted as a decay over observed displacement. Resiliency is the third dimension of liquidity beside spread and depth, and the one that decides whether a large order can be worked at all.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `timesSeconds` | `number[]` | yes | Observation times after the depleting event, in seconds. |
| `displacementBps` | `number[]` | yes | Displacement from the pre-event level at each time, in basis points. |
| `forecastSeconds` | `number` | yes | Horizon at which to project the remaining displacement. · min: 0 |

## Returns

`{ decay_rate, half_life_seconds, forecast_displacement, fit_quality, … }`

The decay rate and half-life, with fit quality — a resiliency figure from a poor fit is not usable.

## Errors

- When the two series differ in length or contain fewer than two points — throws

## Complexity

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

`timesSeconds`:

```json
[0, 1, 2, 3, 4, 5]
```

Showing 6 of 7 elements.

`displacementBps`:

```json
[8, 5.637504, 3.972682, 2.7995, 1.972776, 1.390191]
```

Showing 6 of 7 elements.

`forecastSeconds`:

```json
5
```

### Call

```ts
orderBookResiliency(timesSeconds, displacementBps, forecastSeconds)
```

### Returns

object with 11 fields: model, observation_count, initial_displacement_bps, recovery_rate_per_second, half_life_seconds, forecast_seconds, forecast_displacement_bps, recovered_fraction, …

```json
{
  "model": "exponential-displacement-recovery",
  "observation_count": 7,
  "initial_displacement_bps": 8,
  "recovery_rate_per_second": 0.3500000711368042,
  "half_life_seconds": 1.980420113369107,
  "forecast_seconds": 5,
  "forecast_displacement_bps": 1.3901910531347295,
  "recovered_fraction": 0.8262261183581588,
  "fitted_displacement_bps": [
    8,
    5.637504316715653,
    3.972681865123452,
    2.799501395446431,
    1.9727751501850936,
    1.3901910531347295
  ],
  "rmse_bps": 6.314204089006822e-7,
  "state": "estimated"
}
```

## Other exports

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