# Price-Source Consensus Check

`D01-F04-A03` · Market Data Engineering → Data Quality · archetype `snapshot-evaluate` · difficulty 2/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/data-quality/price-source-consensus-check/
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 { consensus } from "fintech-algorithms/market-data-engineering/data-quality/price-source-consensus-check";
```

## Signature

```ts
consensus(snapshot, policy)
```

Reconciles quotes from several providers into one defensible price, or refuses to. Serious platforms never trust a single vendor, and two feeds never agree exactly — this is the layer that decides who is right.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `snapshot` | `{ as_of: string; quotes: SourceQuote[] }` | yes | One quote per source at a moment in time, each with its own timestamp so staleness is per-source. |
| `policy` | `{ minimum_independent_sources: number; z_threshold: number; absolute_tolerance: number; maximum_tolerance: number; max_age_ms: number; expected_contract?: string }` | yes | `minimum_independent_sources` is the quorum below which no consensus is declared. `z_threshold` sets how far from the robust centre a quote may sit before it is an outlier, bounded by `absolute_tolerance` and `maximum_tolerance` so the test behaves at both very tight and very wide spreads. `max_age_ms` excludes stale sources. |

## Returns

`{ status, reason, consensus_price, inliers, outliers, diagnostics }`

A status rather than a bare number: a refusal to price is a legitimate and important outcome, and `reason` says why.

## Errors

- When the policy is internally inconsistent, such as a negative tolerance — throws

## Complexity

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

`snapshot`:

```json
{
  "as_of": "2026-07-13T13:30:01.000Z",
  "quotes": [
    {
      "source_id": "A",
      "owner_id": "OWNER-A",
      "price": 100,
      "instrument_id": "SYNTH-USD",
      "currency": "USD",
      "price_type": "last_trade",
      "adjustment": "unadjusted",
      "session": "regular",
      "event_time": "2026-07-13T13:30:01.000Z"
    },
    {
      "source_id": "B",
      "owner_id": "OWNER-B",
      "price": 100.01,
      "instrument_id": "SYNTH-USD",
      "currency": "USD",
      "price_type": "last_trade",
      "adjustment": "unadjusted",
      "session": "regular",
      "event_time": "2026-07-13T13:30:01.000Z"
    },
    {
      "source_id": "C",
      "owner_id": "OWNER-C",
      "price": 100.02,
      "instrument_id": "SYNTH-USD",
      "currency": "USD",
      "price_type": "last_trade",
      "adjustment": "unadjusted",
      "session": "regular",
      "event_time": "2026-07-13T13:30:01.000Z"
    }
  ]
}
```

`policy`:

```json
{
  "minimum_independent_sources": 3,
  "z_threshold": 3.5,
  "absolute_tolerance": 0.03,
  "maximum_tolerance": 0.15,
  "max_age_ms": 500,
  "expected_contract": {
    "instrument_id": "SYNTH-USD",
    "currency": "USD",
    "price_type": "last_trade",
    "adjustment": "unadjusted",
    "session": "regular"
  }
}
```

### Call

```ts
consensus(snapshot, policy)
```

### Returns

object with 6 fields: status, reason, consensus_price, inliers, outliers, diagnostics

```json
{
  "status": "consensus",
  "reason": "quorum_met",
  "consensus_price": 100.01,
  "inliers": ["A", "B", "C", "D"],
  "outliers": [],
  "diagnostics": {
    "observed_source_count": 4,
    "eligible_independent_source_count": 4,
    "minimum_independent_sources": 3,
    "excluded": [],
    "initial_center": 100.01,
    "mad": 0.0049999999999954525,
    "robust_scale": 0.007412898443284585,
    "adaptive_tolerance": 0.025945144551496047,
    "absolute_tolerance": 0.03,
    "applied_tolerance": 0.03,
    "maximum_tolerance": 0.15,
    "zero_mad_policy": "not_applicable",
    "comparison_epsilon": 1e-12,
    "inlier_independent_source_count": 4
  }
}
```

## 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/data-quality/price-source-consensus-check/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/data-quality/price-source-consensus-check/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Price-Source-Consensus-Check-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
