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 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 |
required |
Returns:
| Type | Description |
|---|---|
CaptureRecord
|
What the capture recorded. Empty when there is no |
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
|
feed_type |
FeedType or None
|
What that source declares about crossing (see
:class: |
trade_attribution |
TradeAttribution or None
|
Which orders of a trade that source's feed names (see
:class: |
sequence_kind |
SequenceKind or None
|
What the venue |
clocks |
Clocks or None
|
Which clocks the capture's rows carry (see
:class: |
trade_order |
TradeOrder or None
|
Which clock orders the capture's trades against its order events (see
:class: |
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: |
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
|
|
level |
Level or None
|
The capture's resolution (see :class: |
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: |
reading_source ¶
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 ¶
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 ¶
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: |
required |
Returns:
| Type | Description |
|---|---|
dict of str to Any
|
The recorded |
read_format ¶
Return how the files of the capture at path are written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Any
|
As for :func: |
required |
Returns:
| Type | Description |
|---|---|
CaptureFormat
|
The recorded format version, venue and symbol. Version 1 when there
is no |
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
¶
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: |
venue, symbol |
str or None
|
The instrument the capture recorded; |
time_unit
property
¶
The unit of the integer epoch times in the CSV files: "ms" or "ns".
run_declarations ¶
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: |
required |
source
|
Any
|
The source that reads it, or |
required |
Returns:
| Type | Description |
|---|---|
FeedDeclarations
|
With the record's |
write_record ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
The book file in a capture folder: orders.csv or depth.csv.
None when the folder has neither.
record_format ¶
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 ¶
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 |
orders_closed |
int
|
The |
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
¶
Frames raw.jsonl skipped because JSON cannot hold them.
format_fields ¶
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 ¶
What raw.jsonl wrote as text or skipped, as meta.json fields.
Empty when raw.jsonl is off.
bytes_written ¶
Bytes written so far across every open file (what a size roll reads).
finalize ¶
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.