# Stale-Quote Detector

`D01-F02-A04` · Market Data Engineering → Cleaning and Validation · archetype `row-classify` · difficulty 2/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/cleaning-and-validation/stale-quote-detector/
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 { detectStaleQuotes } from "fintech-algorithms/market-data-engineering/cleaning-and-validation/stale-quote-detector";
```

## Signature

```ts
detectStaleQuotes(events, config)
```

Separates the several distinct ways a quote can be stale: old at the source, delayed in transport, unchanged for too long, or arriving after a missed heartbeat. They have different causes and different remedies, so they are reported separately.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `events` | `QuoteEvent[]` | yes | Quote and heartbeat events with both `source_event_ts` and `observed_ts`, which is what makes source age and transport age separable. |
| `config` | `{ max_source_event_age_ms: number; max_transport_age_ms: number; unchanged_threshold_ms: number; heartbeat_timeout_ms: number }` | yes | One budget per failure mode: how old the venue's own timestamp may be, how long transport may take, how long a quote may sit unchanged, and how long a heartbeat may be missing. |

## Returns

`Verdict[]` · length same-as-input

One verdict per event naming which budget was breached. Returns one verdict per input row rather than throwing, so a single bad record cannot abort the batch — and cannot pass unnoticed either.

## Errors

- When any threshold is negative — throws

## Complexity

Time `O(n)`, space `O(n)`.

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

`events`:

```json
[
  {
    "event_id": "E01",
    "kind": "quote",
    "source_event_ts": "2026-07-22T09:30:00.000Z",
    "observed_ts": "2026-07-22T09:30:00.100Z",
    "bid": 100,
    "ask": 100.02,
    "clock_sync_ok": true,
    "session_id": "S1",
    "session_state": "ACTIVE",
    "activity_expected": true
  },
  {
    "event_id": "E02",
    "kind": "heartbeat",
    "observed_ts": "2026-07-22T09:30:00.900Z",
    "session_id": "S1",
    "session_state": "ACTIVE",
    "activity_expected": true
  },
  {
    "event_id": "E03",
    "kind": "check",
    "observed_ts": "2026-07-22T09:30:01.200Z",
    "session_id": "S1",
    "session_state": "ACTIVE",
    "activity_expected": true
  }
]
```

Showing 3 of 12 elements.

`config`:

```json
{
  "max_source_event_age_ms": 1200,
  "max_transport_age_ms": 250,
  "unchanged_threshold_ms": 3000,
  "heartbeat_timeout_ms": 1200
}
```

### Call

```ts
detectStaleQuotes(events, config)
```

### Returns

array of 12 objects

```json
[
  {
    "event_id": "E01",
    "kind": "quote",
    "source_event_ts": "2026-07-22T09:30:00.000Z",
    "observed_ts": "2026-07-22T09:30:00.100Z",
    "bid": 100,
    "ask": 100.02,
    "clock_sync_ok": true,
    "session_id": "S1",
    "session_state": "ACTIVE",
    "activity_expected": true,
    "source_event_age_ms": 100,
    "last_transport_age_ms": 100,
    "unchanged_duration_ms": 0,
    "heartbeat_age_ms": 0
  },
  {
    "event_id": "E02",
    "kind": "heartbeat",
    "observed_ts": "2026-07-22T09:30:00.900Z",
    "session_id": "S1",
    "session_state": "ACTIVE",
    "activity_expected": true,
    "source_event_age_ms": 900,
    "last_transport_age_ms": 100,
    "unchanged_duration_ms": 800,
    "heartbeat_age_ms": 0,
    "source_event_age_exceeded": false,
    "transport_late": false,
    "unchanged_threshold_reached": false,
    "heartbeat_lost": false
  },
  {
    "event_id": "E03",
    "kind": "check",
    "observed_ts": "2026-07-22T09:30:01.200Z",
    "session_id": "S1",
    "session_state": "ACTIVE",
    "activity_expected": true,
    "source_event_age_ms": 1200,
    "last_transport_age_ms": 100,
    "unchanged_duration_ms": 1100,
    "heartbeat_age_ms": 300,
    "source_event_age_exceeded": false,
    "transport_late": false,
    "unchanged_threshold_reached": false,
    "heartbeat_lost": false
  }
]
```

Showing 3 of 12 elements.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

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-data-engineering/cleaning-and-validation/stale-quote-detector/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/cleaning-and-validation/stale-quote-detector/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Stale-Quote-Detector-Data-Quality-algorithm
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
