Skip to content

fastboxex.market

Market receives live market data from the exchange over the TCP market relay. Obtained from p.market() (live assets) or p.arena_market() (arena).


Getting a Market instance

# Live exchange market data
market = p.market()

# Arena market data
arena_market = p.arena_market()

get_latest(asset_id) → dict | None

Latest market data snapshot for the given asset, or None if not yet received.

md = market.get_latest(asset_id=1)

if md:
    bid = md["bid"]      # best bid (raw ticks)
    ask = md["ask"]      # best ask (raw ticks)
    mid = (bid + ask) // 2

    bid_qty = md["bid_qty"]   # volume at best bid
    ask_qty = md["ask_qty"]   # volume at best ask

    # Full depth (list of (price, qty) tuples)
    bids = md["bids"]    # [(price, qty), ...]  top-of-book first
    asks = md["asks"]    # [(price, qty), ...]

Full key reference

Key Type Description
asset_id int Asset ID
timestamp int Nanosecond timestamp
seq_id int Monotone update sequence number
lifecycle_state int Engine lifecycle state
bid int Best bid price (raw ticks)
ask int Best ask price (raw ticks)
bid_qty int Quantity at best bid
ask_qty int Quantity at best ask
bids list[tuple[int,int]] All bid levels (price, qty)
asks list[tuple[int,int]] All ask levels (price, qty)
nlb int Number of live bid orders
nls int Number of live ask orders
volume_buy int Cumulative buy volume (session)
volume_sell int Cumulative sell volume (session)
ntb int Number of buy trades (session)
nts int Number of sell trades (session)

No mid key

There is no "mid" field. Compute it: mid = (md["bid"] + md["ask"]) // 2.


to_polars(asset_id=None)

Export full snapshot history as a Polars DataFrame (22 columns, LOB depth included).

df = market.to_polars(asset_id=1)     # single asset
df = market.to_polars()               # all assets interleaved

trades_to_polars(asset_id=None)

Export public trade tape as a Polars DataFrame.

ticks_to_polars(asset_id=None)

Export tick event history as a Polars DataFrame.

as_tensor()

Zero-copy PyMemoryView over the raw market ring buffer.

import numpy as np

mv  = market.as_tensor()
arr = np.frombuffer(mv, dtype=np.uint64).reshape(-1, 58)

Use to_polars() for consistent snapshots — as_tensor() is updated concurrently.


Status

market.is_connected()   # True if data received within last 2 seconds
market.history_len()    # number of snapshots in ring buffer