# Refresh-Time Sampling

`D01-F03-A03` · Market Data Engineering → Time Synchronization · archetype `record-transform` · difficulty 4/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/time-synchronization/refresh-time-sampling/
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 { refreshTimeSample } from "fintech-algorithms/market-data-engineering/time-synchronization/refresh-time-sampling";
```

## Signature

```ts
refreshTimeSample(observations, requiredInstruments, maxStalenessMs)
```

Builds a common clock for several instruments by advancing only when every one of them has refreshed. The standard remedy for the bias that non-synchronous trading introduces into correlations.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `observations` | `Observation[]` | yes | Observations across all instruments, each with `partition`, `instrument`, `event_time` and `available_at`. |
| `requiredInstruments` | `string[]` | yes | The instruments that must all have refreshed before a refresh time is emitted. |
| `maxStalenessMs` | `number` | yes | How old any instrument's value may be at a refresh time. · min: 0 |

## Returns

`{ rows, partitions }`

The synchronised rows, plus per-partition diagnostics showing which instrument was the binding constraint at each step.

## Errors

- When requiredInstruments is empty — 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

`observations`:

```json
[
  {
    "partition": "P1",
    "instrument": "A",
    "record_id": "a1",
    "revision": 0,
    "event_time": "2026-01-01T00:00:01.000Z",
    "available_at": "2026-01-01T00:00:01.000Z",
    "value": 1
  },
  {
    "partition": "P1",
    "instrument": "A",
    "record_id": "a3",
    "revision": 0,
    "event_time": "2026-01-01T00:00:03.000Z",
    "available_at": "2026-01-01T00:00:03.000Z",
    "value": 3
  },
  {
    "partition": "P1",
    "instrument": "A",
    "record_id": "a4",
    "revision": 0,
    "event_time": "2026-01-01T00:00:04.000Z",
    "available_at": "2026-01-01T00:00:04.000Z",
    "value": 4
  }
]
```

Showing 3 of 5 elements.

`requiredInstruments`:

```json
["A", "B"]
```

`maxStalenessMs`:

```json
5000
```

### Call

```ts
refreshTimeSample(observations, requiredInstruments, maxStalenessMs)
```

### Returns

object with 2 fields: rows, partitions

```json
{
  "rows": [
    {
      "partition": "P1",
      "sequence": 1,
      "previous_refresh_available_at": null,
      "refresh_available_at": "2026-01-01T00:00:02.000Z",
      "controller_instruments": ["B"],
      "status": "accepted",
      "values": {
        "A": 1,
        "B": 20
      },
      "sources": {
        "A": {
          "record_id": "a1",
          "revision": 0,
          "event_time": "2026-01-01T00:00:01.000Z",
          "available_at": "2026-01-01T00:00:01.000Z"
        },
        "B": {
          "record_id": "b2",
          "revision": 0,
          "event_time": "2026-01-01T00:00:02.000Z",
          "available_at": "2026-01-01T00:00:02.000Z"
        }
      },
      "event_age_ms": {
        "A": 1000,
        "B": 0
      },
      "arrivals_by_instrument": {
        "A": 1,
        "B": 1
      },
      "discarded_updates": 0
    },
    {
      "partition": "P1",
      "sequence": 2,
      "previous_refresh_available_at": "2026-01-01T00:00:02.000Z",
      "refresh_available_at": "2026-01-01T00:00:04.000Z",
      "controller_instruments": ["B"],
      "status": "accepted",
      "values": {
        "A": 4,
        "B": 40
      },
      "sources": {
        "A": {
          "record_id": "a4",
          "revision": 0,
          "event_time": "2026-01-01T00:00:04.000Z",
          "available_at": "2026-01-01T00:00:04.000Z"
        },
        "B": {
          "record_id": "b4",
          "revision": 0,
          "event_time": "2026-01-01T00:00:04.000Z",
          "available_at": "2026-01-01T00:00:04.000Z"
        }
      },
      "event_age_ms": {
        "A": 0,
        "B": 0
      },
      "arrivals_by_instrument": {
        "A": 2,
        "B": 1
      },
      "discarded_updates": 1
    }
  ],
  "partitions": [
    {
      "partition": "P1",
      "input_updates": 5,
      "refresh_candidates": 2,
      "accepted_rows": 2,
      "stale_rows": 0,
      "discarded_updates": 1,
      "unmatched_tail_updates": 0,
      "loss_fraction": {
        "numerator": 1,
        "denominator": 5
      }
    }
  ]
}
```

## 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/time-synchronization/refresh-time-sampling/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/time-synchronization/refresh-time-sampling/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Refresh-Time-Sampling-Time-Synchronization-algorithm
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
