Skip to content

Capture record

Every live capture, and every segment of a long one, writes a meta.json beside its files. Most of its fields are counters. A few say how to read the data back: the source that made the capture and what it declares about its feed, the instrument's tick size and lot size, the version of the file format, and the instrument's venue and symbol. This module writes and reads those fields. It is outside ob_analytics.live, so code that only replays files does not need the live-capture extras.

Pipeline.run reads the record of the capture it is given, and uses the recorded tick size and lot size when your config does not set them. So Pipeline.from_source("depth_csv").run(capture) and ob-analytics process capture give the same result: the record names the source that reads the capture back (replay_source), so the command needs no --source. See Capture live data.

read_record

read_record(path: Any) -> CaptureRecord

Read the record of the capture at path.

Parameters:

Name Type Description Default
path Any

A capture folder, a segment's folder, a file inside one, or the output of ob-analytics process. Anything that is not a path (a frame, say) has no record.

required

Returns:

Type Description
CaptureRecord

What the capture recorded. Empty when there is no meta.json, or it cannot be read. A declaration with a value this version does not know is left out with a warning.

CaptureRecord dataclass

CaptureRecord(
    source: str | None = None,
    feed_type: FeedType | None = None,
    trade_attribution: TradeAttribution | None = None,
    sequence_kind: SequenceKind | None = None,
    clocks: Clocks | None = None,
    trade_order: TradeOrder | None = None,
    sequence_restarts: int = 0,
    tick_size: float | None = None,
    lot_size: float | None = None,
    tick_size_stated: bool | None = None,
    lot_size_stated: bool | None = None,
    level: Level | None = None,
    replay_source: str | None = None,
)

What a capture recorded about how to read its data.

Every field is None (0 for sequence_restarts) when the capture did not record it: input that is not a capture, a capture written before captures recorded the field, or a venue that does not give the value.

Attributes:

Name Type Description
source str or None

The name of the source that made the capture. It can differ from the source that reads the files: a cryptofeed L3 capture is read as bitstamp.

feed_type FeedType or None

What that source declares about crossing (see :class:~ob_analytics.protocols.FeedType).

trade_attribution TradeAttribution or None

Which orders of a trade that source's feed names (see :class:~ob_analytics.protocols.TradeAttribution).

sequence_kind SequenceKind or None

What the venue sequence promises (see :class:~ob_analytics.protocols.SequenceKind). None when the source declares none; then use what the source declares now (:func:~ob_analytics.protocols.sequence_kind_of).

clocks Clocks or None

Which clocks the capture's rows carry (see :class:~ob_analytics.protocols.Clocks). A live source learns this from the venue's books, so it is written when the capture closes.

trade_order TradeOrder or None

Which clock orders the capture's trades against its order events (see :class:~ob_analytics.protocols.TradeOrder). None when the source declares none; then use what the source declares now.

sequence_restarts int

How many times the venue's sequence started again. A source that finds a lost message itself starts again from a new opening book, and on some venues the sequence starts again too. These steps back are not faults (see :func:~ob_analytics.quality.data_quality_summary).

tick_size float or None

The instrument's price step, in the quote currency.

lot_size float or None

The instrument's size step, in the base asset.

tick_size_stated, lot_size_stated bool or None

True when the venue stated the step and every value the capture wrote sat on it. False when the capture fitted the step to the values it saw: it started from the default grid, or from the venue's step, and made it finer until every value sat on it. A fitted tick can be finer than the venue's real tick. A capture written before captures recorded this reads as False, since nothing says the venue stated its step. None when the capture records no step.

level Level or None

The capture's resolution (see :class:~ob_analytics.protocols.Level).

replay_source str or None

The name of the offline source that reads the capture's files back, as the source that made the capture declares it (see :func:replay_source_of).

reading_source

reading_source() -> str | None

Return the name of the offline source that reads the capture.

The recorded replay_source. A capture written before captures recorded it is read by the source that reads its level (bitstamp for L3, depth_csv for L2). None when the capture records neither.

instrument

instrument() -> dict[str, Any]

Return the :class:~ob_analytics.config.PipelineConfig fields recorded.

The recorded tick size comes with the price_decimals that shows one tick, and the lot size with its volume_decimals. A step the capture did not record is left out.

read_instrument

read_instrument(path: Any) -> dict[str, Any]

Return the :class:~ob_analytics.config.PipelineConfig fields a capture recorded.

The same as read_record(path).instrument(), without reading the source's declarations, so a declaration this version does not know is not warned about. :meth:~ob_analytics.pipeline.Pipeline.run reads the config this way, and the declarations with :func:run_declarations.

Parameters:

Name Type Description Default
path Any

As for :func:read_record.

required

Returns:

Type Description
dict of str to Any

The recorded tick_size and lot_size, each with its display precision; empty when the capture records neither.

read_format

read_format(path: Any) -> CaptureFormat

Return how the files of the capture at path are written.

Parameters:

Name Type Description Default
path Any

As for :func:read_record.

required

Returns:

Type Description
CaptureFormat

The recorded format version, venue and symbol. Version 1 when there is no meta.json, or it records no version.

Raises:

Type Description
ConfigError

If the capture was written in a format this version does not know: a later ob-analytics made it.

CaptureFormat dataclass

CaptureFormat(
    version: int = 1,
    venue: str | None = None,
    symbol: str | None = None,
)

How the files of a capture are written, and the instrument they hold.

Attributes:

Name Type Description
version int

The version of the file format: 1 for a capture whose meta.json records none (a capture made before versions were recorded, or input that is not a capture).

venue, symbol str or None

The instrument the capture recorded; None when it recorded none.

time_unit property

time_unit: str

The unit of the integer epoch times in the CSV files: "ms" or "ns".

run_declarations

run_declarations(
    path: Any, source: Any
) -> FeedDeclarations

Return the declarations a run of source on path is held to.

They start as what source declares (:func:declarations_of). A live capture records what its own source declares, and that describes the data exactly, so each declaration the record holds replaces the reading source's. The two sources can differ, because the reading source also says how to read the files: a cryptofeed L3 capture is read as bitstamp, whose feed shows more.

A capture written before captures recorded the sequence kind is held to what the source that made it declares now, when that source can be built here.

Parameters:

Name Type Description Default
path Any

What the run reads, as for :func:read_record.

required
source Any

The source that reads it, or None for a saved result read with no source named.

required

Returns:

Type Description
FeedDeclarations

With the record's sequence_restarts.

write_record

write_record(folder: Path, fields: dict[str, Any]) -> None

Write fields as the record of the capture in folder.

The file is replaced in one step, so a reader never sees half of it, and any API key the capture holds is removed from the text first. A value JSON cannot hold is written as its text.

source_declarations

source_declarations(source: Any) -> dict[str, Any]

Return what a live source declares about its feed, as record fields.

source names the source, level is its resolution, and replay_source the offline source that reads the capture back (:func:replay_source_of). feed_type and trade_attribution are what it declares (see :class:~ob_analytics.protocols.FeedType and :class:~ob_analytics.protocols.TradeAttribution). sequence_kind and trade_order (see :class:~ob_analytics.protocols.TradeOrder) are written only by a source that declares them. clocks (see :class:~ob_analytics.protocols.Clocks) is read when the capture closes, since a live source learns it from the venue's books.

It runs while a capture is closing, so a declaration that cannot be read (a plug-in's value outside the enum, say) is logged and never stops the files being finished: an unreadable feed_type is recorded as unknown, and an unreadable trade_attribution is left out, so audit falls back to --source rather than to a guess.

replay_source_of

replay_source_of(source: Any) -> str

Return the name of the offline source that reads source's captures.

A live source declares it with a replay_source attribute. One that declares none is read by itself when it also replays files (Bitstamp), and otherwise by the source that reads the files the capture writes at its level: bitstamp for orders.csv (L3), depth_csv for depth.csv (L2).

read_fields

read_fields(path: Any) -> dict[str, Any] | None

Return every field of the record at path, as written.

None when there is no record, or it is not a JSON object. Any other error reading the file is raised, so a writer never replaces a record it could not read.

record_path

record_path(path: Any) -> Path

Return where the record of the capture at path is.

path is a capture folder, a segment's folder, a file inside one, or the output of ob-analytics process, which keeps the capture's record.

Capture files

A capture segment's orders.csv or depth.csv, trades.csv and raw.jsonl. These functions find a segment's book file, record the format version of files a writer saved, and complete the files of a capture that stopped without closing them. The files are in format version 2: see the file format.

book_file

book_file(folder: Path) -> Path | None

The book file in a capture folder: orders.csv or depth.csv.

None when the folder has neither.

record_format

record_format(folder: Path) -> None

Record in folder's meta.json that its files are in the current format.

For a writer that saves a run in the capture format: the other fields of a meta.json already there are kept. One is written when there is none.

repair_files

repair_files(folder: Path) -> RepairedFiles

Complete the files of a capture folder that its capture never closed.

The last line of each file may be cut short, or, if the capture stopped before its first flush, a file may be empty. This cuts each file back to its last whole line, puts back a missing header, and closes every L3 order still open at the last recorded time, with the same deleted rows a normal shutdown writes. It reads the format version from meta.json, and does not change it.

RepairedFiles dataclass

RepairedFiles(
    last_timestamp: Timestamp | None,
    orders_closed: int,
    n_order_events: int,
    n_depth_events: int,
    n_trade_events: int,
)

What :func:repair_files found and wrote.

Attributes:

Name Type Description
last_timestamp Timestamp or None

The latest receive timestamp in any CSV file, as tz-aware UTC; None when they hold no rows.

orders_closed int

The deleted rows written for orders still open.

n_order_events, n_depth_events, n_trade_events int

The rows each file holds after the repair.

FileCaptureSink

FileCaptureSink(
    out_dir: Path,
    *,
    keep_raw: bool,
    level: Level = Level.L3,
    raw_warned: set[str] | None = None,
)

Default sink: writes the book file, trades.csv, raw.jsonl, meta.json.

It satisfies :class:~ob_analytics.live.CaptureSink. The book file follows the source's :class:~ob_analytics.protocols.Level: L3 -> orders.csv (one row per order event); L2 -> depth.csv (one row per price-level change). trades.csv is written for both.

When a source's settings hold an API key, every file passes through :func:ob_analytics._secrets.redact, so the key is not written even if a frame or an error message carries it. The keys are registered before the sink is built (:func:~ob_analytics.live._runner.check_credentials); with none, the files are plain.

raw_frames_skipped property

raw_frames_skipped: int

Frames raw.jsonl skipped because JSON cannot hold them.

format_fields

format_fields() -> dict[str, Any]

The meta.json fields that say how to read the files.

The format version, and the venue and symbol of the first row that named them (a source puts them on its events). Each is left out until a row names it.

raw_diagnostics

raw_diagnostics() -> dict[str, Any]

What raw.jsonl wrote as text or skipped, as meta.json fields.

Empty when raw.jsonl is off.

flush

flush() -> None

Push buffered rows to disk, so a crash loses as few as possible.

bytes_written

bytes_written() -> int

Bytes written so far across every open file (what a size roll reads).

finalize

finalize(result: CaptureResult) -> None

Close every file, then write meta.json last.

Each file is closed on its own, so one that fails to flush (a full disk, say) leaves no other open. The first failure is logged and recorded in meta.json as capture_error with the phase close, unless the run already had an error, and kept in :attr:close_error.