# Crossed/Locked Market Detector

`D01-F02-A06` · 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/crossed-locked-market-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 { classifyMarkets } from "fintech-algorithms/market-data-engineering/cleaning-and-validation/crossed-locked-market-detector";
```

## Signature

```ts
classifyMarkets(quotes, options)
```

Classifies a quote as normal, locked (bid equals ask) or crossed (bid above ask). Crossed markets are usually a stale or misordered feed rather than a real arbitrage, which is exactly why they must be caught before anything downstream trusts the spread.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `quotes` | `Quote[]` | yes | Quotes carrying `instrument`, `market_scope`, `feed`, `event_time`, `receive_time`, `sequence`, bid and ask. |
| `options` | `{ tolerance_ticks?: number; relative_tolerance_ppm?: number }` | no | Absolute tolerance in ticks and relative tolerance in parts per million, so a one-tick rounding artefact is not reported as a crossed book. |

## Returns

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

One classification per quote with the margin by which it locked or crossed. 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 a tolerance 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

`quotes`:

```json
[
  {
    "instrument": "SYNTH",
    "market_scope": "SYNTHETIC_CONSOLIDATED_TOP",
    "feed": "SYNTH-SIP",
    "event_time": "2026-07-20T13:30:00.000Z",
    "receive_time": "2026-07-20T13:30:00.003Z",
    "sequence": 1001,
    "bid_source": "VENUE-A",
    "ask_source": "VENUE-B",
    "bid": 100,
    "ask": 100.02,
    "tick_size": 0.01
  },
  {
    "instrument": "SYNTH",
    "market_scope": "SYNTHETIC_CONSOLIDATED_TOP",
    "feed": "SYNTH-SIP",
    "event_time": "2026-07-20T13:30:00.100Z",
    "receive_time": "2026-07-20T13:30:00.104Z",
    "sequence": 1002,
    "bid_source": "VENUE-A",
    "ask_source": "VENUE-B",
    "bid": 100.01,
    "ask": 100.01,
    "tick_size": 0.01
  },
  {
    "instrument": "SYNTH",
    "market_scope": "SYNTHETIC_CONSOLIDATED_TOP",
    "feed": "SYNTH-SIP",
    "event_time": "2026-07-20T13:30:00.200Z",
    "receive_time": "2026-07-20T13:30:00.207Z",
    "sequence": 1003,
    "bid_source": "VENUE-A",
    "ask_source": "VENUE-C",
    "bid": 100.03,
    "ask": 100.02,
    "tick_size": 0.01
  }
]
```

Showing 3 of 8 elements.

`options`:

```json
{
  "tolerance_ticks": 0,
  "relative_tolerance_ppm": 0
}
```

### Call

```ts
classifyMarkets(quotes, options)
```

### Returns

array of 8 objects

```json
[
  {
    "instrument": "SYNTH",
    "market_scope": "SYNTHETIC_CONSOLIDATED_TOP",
    "feed": "SYNTH-SIP",
    "event_time": "2026-07-20T13:30:00.000Z",
    "receive_time": "2026-07-20T13:30:00.003Z",
    "sequence": 1001,
    "bid_source": "VENUE-A",
    "ask_source": "VENUE-B",
    "bid": 100,
    "ask": 100.02,
    "tick_size": 0.01,
    "index": 0,
    "spread": 0.01999999999999602,
    "spread_ticks": 1.999999999999602
  },
  {
    "instrument": "SYNTH",
    "market_scope": "SYNTHETIC_CONSOLIDATED_TOP",
    "feed": "SYNTH-SIP",
    "event_time": "2026-07-20T13:30:00.100Z",
    "receive_time": "2026-07-20T13:30:00.104Z",
    "sequence": 1002,
    "bid_source": "VENUE-A",
    "ask_source": "VENUE-B",
    "bid": 100.01,
    "ask": 100.01,
    "tick_size": 0.01,
    "index": 1,
    "spread": 0,
    "spread_ticks": 0
  },
  {
    "instrument": "SYNTH",
    "market_scope": "SYNTHETIC_CONSOLIDATED_TOP",
    "feed": "SYNTH-SIP",
    "event_time": "2026-07-20T13:30:00.200Z",
    "receive_time": "2026-07-20T13:30:00.207Z",
    "sequence": 1003,
    "bid_source": "VENUE-A",
    "ask_source": "VENUE-C",
    "bid": 100.03,
    "ask": 100.02,
    "tick_size": 0.01,
    "index": 2,
    "spread": -0.010000000000005116,
    "spread_ticks": -1.0000000000005116
  }
]
```

Showing 3 of 8 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/crossed-locked-market-detector/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/cleaning-and-validation/crossed-locked-market-detector/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Crossed-Locked-Market-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
