# OHLC Consistency Validator

`D01-F02-A01` · 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/ohlc-consistency-validator/
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 { validateBars } from "fintech-algorithms/market-data-engineering/cleaning-and-validation/ohlc-consistency-validator";
```

## Signature

```ts
validateBars(bars, config)
```

Checks each bar against the invariants an OHLC bar must satisfy — high is the maximum, low is the minimum, open and close lie between them — with a tick-size tolerance so representable rounding is not reported as corruption.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bars` | `OhlcRow[]` | yes | Bars to check, each carrying `bar_id`, `source`, `symbol`, `timestamp` and the four prices. |
| `config` | `{ tickSize: number; toleranceTicks: number; priceScale: number }` | yes | `tickSize` is the instrument's minimum increment and `toleranceTicks` how many of them a value may be out before it is a violation. `priceScale` fixes the decimal scale used for comparison so floating-point representation does not create phantom failures. |

## Returns

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

One verdict per bar, naming which invariant failed and by how much. 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 tickSize is not positive — 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

`bars`:

```json
[
  {
    "bar_id": "B01",
    "source": "SYNTHETIC",
    "symbol": "DEMO",
    "timestamp": "2026-07-20T09:30:00Z",
    "open": 100,
    "high": 102,
    "low": 99,
    "close": 101,
    "volume": 1000
  },
  {
    "bar_id": "B02",
    "source": "SYNTHETIC",
    "symbol": "DEMO",
    "timestamp": "2026-07-20T09:31:00Z",
    "open": 100.01,
    "high": 100,
    "low": 99.5,
    "close": 99.9,
    "volume": 900
  },
  {
    "bar_id": "B03",
    "source": "SYNTHETIC",
    "symbol": "DEMO",
    "timestamp": "2026-07-20T09:32:00Z",
    "open": 100.0101,
    "high": 100,
    "low": 99.5,
    "close": 99.9,
    "volume": 800
  }
]
```

Showing 3 of 7 elements.

`config`:

```json
{
  "tickSize": 0.01,
  "toleranceTicks": 1,
  "priceScale": 1
}
```

### Call

```ts
validateBars(bars, config)
```

### Returns

array of 7 objects

```json
[
  {
    "index": 0,
    "timestamp": "2026-07-20T09:30:00Z",
    "valid": true,
    "issues": [],
    "tolerancePriceUnits": 0.01,
    "normalizedPrices": {
      "open": 100,
      "high": 102,
      "low": 99,
      "close": 101
    },
    "provenance": {
      "source": "SYNTHETIC",
      "symbol": "DEMO",
      "bar_id": "B01"
    },
    "rawBar": {
      "bar_id": "B01",
      "source": "SYNTHETIC",
      "symbol": "DEMO",
      "timestamp": "2026-07-20T09:30:00Z",
      "open": 100,
      "high": 102,
      "low": 99,
      "close": 101,
      "volume": 1000
    }
  },
  {
    "index": 1,
    "timestamp": "2026-07-20T09:31:00Z",
    "valid": true,
    "issues": [],
    "tolerancePriceUnits": 0.01,
    "normalizedPrices": {
      "open": 100.01,
      "high": 100,
      "low": 99.5,
      "close": 99.9
    },
    "provenance": {
      "source": "SYNTHETIC",
      "symbol": "DEMO",
      "bar_id": "B02"
    },
    "rawBar": {
      "bar_id": "B02",
      "source": "SYNTHETIC",
      "symbol": "DEMO",
      "timestamp": "2026-07-20T09:31:00Z",
      "open": 100.01,
      "high": 100,
      "low": 99.5,
      "close": 99.9,
      "volume": 900
    }
  },
  {
    "index": 2,
    "timestamp": "2026-07-20T09:32:00Z",
    "valid": false,
    "issues": ["HIGH_BELOW_BODY"],
    "tolerancePriceUnits": 0.01,
    "normalizedPrices": {
      "open": 100.0101,
      "high": 100,
      "low": 99.5,
      "close": 99.9
    },
    "provenance": {
      "source": "SYNTHETIC",
      "symbol": "DEMO",
      "bar_id": "B03"
    },
    "rawBar": {
      "bar_id": "B03",
      "source": "SYNTHETIC",
      "symbol": "DEMO",
      "timestamp": "2026-07-20T09:32:00Z",
      "open": 100.0101,
      "high": 100,
      "low": 99.5,
      "close": 99.9,
      "volume": 800
    }
  }
]
```

Showing 3 of 7 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/ohlc-consistency-validator/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/cleaning-and-validation/ohlc-consistency-validator/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-OHLC-Consistency-Validator-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
