Skip to content

Depth CSV (L2 price-level)

Price-level (L2 / market-by-price) components: the depth loader, trade reader, CSV writer, and DepthCsvSource. For the guide, see Process L2 feeds.

A price-level feed carries [price, quantity] levels and diffs with no order IDs, so the loader yields the depth frame directly and the pipeline skips the per-order stages (see Level).

DepthCsvLoader

DepthCsvLoader(
    config: PipelineConfig | None = None,
    *,
    settings: DepthCsvSettings | None = None,
    venue: str | None = None,
    symbol: str | None = None,
)

Load a price-level depth stream into the canonical depth frame.

Satisfies the :class:~ob_analytics.protocols.DepthLoader protocol.

Parameters:

Name Type Description Default
config PipelineConfig

Pipeline configuration. tick_size quantises the price to integer ticks and lot_size the size to integer lots.

None
settings DepthCsvSettings

How the file encodes its timestamps and prices.

None
venue str

Optional instrument identity. When either is supplied, the loaded depth frame gains per-row venue / symbol columns. A generic price-level CSV carries no venue of its own, so venue is left NA unless supplied. Each one not supplied is taken from the capture's meta.json when it records one, so a replayed capture names its instrument. With neither, the frame is left untagged.

None
symbol str

Optional instrument identity. When either is supplied, the loaded depth frame gains per-row venue / symbol columns. A generic price-level CSV carries no venue of its own, so venue is left NA unless supplied. Each one not supplied is taken from the capture's meta.json when it records one, so a replayed capture names its instrument. With neither, the frame is left untagged.

None

load

load(path: str | Path) -> pd.DataFrame

Read path and return a canonical depth DataFrame.

Parameters:

Name Type Description Default
path str or Path

The L2 CSV file, or a directory containing depth.csv.

required

Returns:

Type Description
DataFrame

Columns timestamp, price, volume (absolute level size), direction (categorical bid/ask), sorted by timestamp — a :func:~ob_analytics.schemas.validate_depth_df frame. exchange_timestamp follows timestamp when the file has it, and origin (which part of a capture wrote the row) is kept when the file has it.

load_windows

load_windows(
    path: str | Path,
    windows: list[
        tuple[Timestamp | None, Timestamp | None]
    ],
    *,
    working: Path | None = None,
) -> Iterator[LoadedWindow]

Yield the depth rows of each time window of path in turn.

The file is read :attr:read_size rows at a time. Each part's rows are sorted into one file per window in working, and each window is then read back on its own, so no more than one part or one window is in memory at once. Each window holds the rows :meth:load gives, cut at the windows and in the same order, so the file need not be in time order.

Parameters:

Name Type Description Default
path str or Path

The L2 CSV file, or a directory containing depth.csv.

required
windows list of tuple

(start, end) per window, tz-aware UTC, None for an open end, covering the whole file: what :func:~ob_analytics._windows.window_bounds returns.

required
working Path

An empty folder for the files, which the caller deletes. By default a temporary folder is made and deleted.

None

Yields:

Type Description
LoadedWindow

The window's depth rows. A price-level row names no order, so no event a trade names is given.

DepthCsvTradeReader

DepthCsvTradeReader(
    config: PipelineConfig | None = None,
    *,
    settings: DepthCsvSettings | None = None,
)

Read raw trade prints from a companion trades.csv.

Projects each print into the canonical trades schema. Price-level feeds have no order IDs, so maker / taker (and their event-id / og columns) are left unset. direction (the taker's aggressor side) is taken from a native direction column (side in older files) when present; otherwise it is left unlabelled and the pipeline classifies it (Lee–Ready against the reconstructed touch — see :func:~ob_analytics.trade_sign.resolve_direction).

Trades are optional for an L2 run: a missing / empty trades.csv yields an empty trades frame (depth analytics still run).

Satisfies the :class:~ob_analytics.protocols.TradeReader protocol.

Parameters:

Name Type Description Default
config PipelineConfig

Pipeline configuration: the instrument's tick_size and lot_size.

None
settings DepthCsvSettings

How the file encodes its timestamps and prices, as for :class:DepthCsvLoader.

None

DepthCsvWriter

DepthCsvWriter(
    config: PipelineConfig | None = None,
    *,
    settings: DepthCsvSettings | None = None,
)

Write a depth frame (and trades) back to the L2 CSV schema.

Round-trips :class:DepthCsvLoader / :class:DepthCsvTradeReader: writes depth.csv (and a companion trades.csv when trades are supplied) into the dest directory.

Satisfies the :class:~ob_analytics.protocols.DataWriter protocol.

Parameters:

Name Type Description Default
config PipelineConfig

Pipeline configuration: the tick_size and lot_size the frames are in.

None
settings DepthCsvSettings

How to encode the timestamps and prices, as the loader reads them.

None

write

write(
    data: dict[str, DataFrame],
    dest: str | Path,
    **kwargs: Any,
) -> Path

Write data['depth'] (and optional data['trades']) to dest.

Parameters:

Name Type Description Default
data dict of str to DataFrame

Must contain a "depth" key (the canonical depth frame). An optional "trades" key triggers a companion trades.csv.

required
dest str or Path

Output directory.

required

Returns:

Type Description
Path

The written depth.csv path.

DepthCsvSource dataclass

DepthCsvSource(
    name: str = "depth_csv",
    level: Level = Level.L2,
    feed_type: FeedType = FeedType.PRICE_LEVELS,
    trade_attribution: TradeAttribution = TradeAttribution.NONE,
    settings: SourceSettings = DepthCsvSettings(),
    grid: InstrumentGrid = InstrumentGrid(),
)

The canonical L2 (price-level) CSV source — offline depth replay.

Declares :attr:~ob_analytics.protocols.Level.L2, so :class:~ob_analytics.pipeline.Pipeline takes the price-level path: depth in, per-order stages skipped. The shared entry point the aggregated venue connectors (Binance, Kalshi, Polymarket) narrow to domain modelling on top of, and the offline replay target for a live L2 capture (e.g. the depth.csv a :class:~ob_analytics.live.ccxt_source.CcxtSource writes).

Conforms structurally to :class:~ob_analytics.protocols.OfflineSource — no inheritance required.

DepthCsvSettings

DepthCsvSettings(**data: Any)

Bases: SourceSettings

How a price-level CSV encodes its timestamps and prices.

The settings of :class:DepthCsvSource, read by :class:DepthCsvLoader, :class:DepthCsvTradeReader and :class:DepthCsvWriter.

Attributes:

Name Type Description
timestamp_unit {'ms', 'us', 'ns'}

The unit of an integer epoch timestamp. A timestamp written as a string is parsed as it is, whatever this says. When it is not set, the unit is the one the folder's meta.json records for a capture (nanoseconds since capture format version 2), else "ms"; the writer then writes nanoseconds and records the capture format.

price_divisor int

What a price in the file is divided by to reach the quote currency. 1 (the default) means it is in the quote currency already.

A capture's meta.json is read by read_record.