Skip to content

Feed

Feed is the tick data layer above quantpylib.gateway.master.Gateway. It subscribes through the gateway executor, receives standardized native market-data events from wrapper subscriptions, stores the latest event for each feed id, and forwards each event to configured sinks and optional on_msg callback.

Feed Path

exchange message
    |
    v
wrapper subscription
    |
    v
gateway executor
    |
    v
Feed
    |
    +--> FeedSink.write(feed_id, event)
    |
    +--> on_msg(event)
    |
    +--> feed.feeds[feed_id] / feed.get_feed(feed_id)

add_*_feed methods return a quantpylib.standards.models.FeedId. Use that id with get_feed(feed_id) to read the latest object or buffer held by the feed.

Event Objects

BBA, L2 book, and trades feed methods use standardize_schema=2, so the event passed to sinks and callbacks is a native quantpylib model object.

Feed method Feed type Event received by sinks and callbacks Feed storage
add_bba_feed(...) bba quantpylib.standards.models.BBAUpdate latest BBAUpdate
add_l2_book_feed(...) l2book quantpylib.standards.models.BookUpdate latest BookUpdate
add_l2_book_feeds(...) l2book quantpylib.standards.models.BookUpdate per ticker latest BookUpdate per returned FeedId
add_trades_feed(...) trades quantpylib.standards.models.TradeUpdate latest TradeUpdate
add_trades_feeds(...) trades quantpylib.standards.models.TradeUpdate per ticker latest TradeUpdate per returned FeedId
add_oracle_feed(...) mid numeric oracle value quantpylib.utilities.cringbuffer.RingBuffer

Sink Contract

A sink is a quantpylib.hft.archival.FeedSink instance. Feed calls:

sink.write(feed_id, event)

for every event routed to that sink. write() may be synchronous or asynchronous; if it returns an awaitable, Feed awaits it. The return value is sink-specific and is ignored by Feed.

Feed.cleanup() calls sink.close() for configured sinks. close() may also be synchronous or asynchronous and should be safe to call more than once. Sinks that buffer work may expose flush(), but Feed does not require custom sinks to implement flushing semantics if the default FeedSink base method is sufficient.

Sink routing is explicit:

Where sinks are passed Meaning
Feed(gateway, sinks=[...]) Global sinks used by feeds that do not override sinks.
add_*_feed(..., sinks=None) Use the global sinks.
add_*_feed(..., sinks=[...]) Use these sinks for this feed.
add_*_feed(..., sinks=[]) Disable sink fanout for this feed.

Order Book Materialization

Use quantpylib.hft.materializers.OrderbookMaterializerSink when a strategy needs live order-book state from BookUpdate events:

from quantpylib.hft import OrderbookMaterializerSink
from quantpylib.hft.feed import Feed

book_sink = OrderbookMaterializerSink()
feed = Feed(gateway=gateway)

book_feed = await feed.add_l2_book_feed(
    exc="binance",
    ticker="BTCUSDT",
    sinks=[book_sink],
)

orderbook = book_sink.orderbook #in memory orderbook
latest_update = feed.get_feed(book_feed)

L0/QBN Capture

Use quantpylib.hft.l0.L0ArchiveSink when you want to persist feed events as L0/QBN records:

from quantpylib.hft.feed import Feed
from quantpylib.hft.l0 import L0ArchiveSink

archive_sink = L0ArchiveSink(root="./archives")
feed = Feed(gateway=gateway, sinks=[archive_sink])

await feed.add_bba_feed(exc="binance", ticker="BTCUSDT")
await feed.add_l2_book_feed(exc="binance", ticker="BTCUSDT")
await feed.add_trades_feed(exc="binance", ticker="BTCUSDT")

See L0 Data Capture / QBN for file layout, writers, readers, and read-back examples.

See quantpylib.hft.archival for the base sink interfaces and queue sinks. See quantpylib.hft.materializers for materializer sinks.

API Reference

Feed

Gateway-backed market-data fanout.

Feed subscribes through a gateway executor, stores the latest event or buffer for each FeedId, writes each event to configured (if any) FeedSink objects, and then calls the optional on_msg callback. Native market-data feed methods use standardize_schema=2, so BBA, L2 book, and trade feeds receive BBAUpdate, BookUpdate, and TradeUpdate objects.

Feed does not materialize order-book state or persist records by itself. Use a materializer sink, such as OrderbookMaterializerSink, for live derived state, or L0ArchiveSink for L0/QBN capture.

__init__(gateway, exchanges=None, sinks=None)

Initialize the feed object.

Parameters:

Name Type Description Default
gateway Gateway

Initialized gateway.

required
exchanges list

Exchanges this feed may subscribe to. If None, use the clients configured on the gateway.

None
sinks FeedSink or iterable[FeedSink]

Global sinks used by feed subscriptions that do not provide a per-feed override.

None

add_bba_feed(exc, ticker, on_msg=None, sinks=None, feed_cls=FeedCls.PERPETUAL, **kwargs) async

Add a best bid/ask feed.

Sinks and on_msg receive quantpylib.standards.models.BBAUpdate objects. get_feed(feed_id) returns the latest received BBAUpdate.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
ticker str

Ticker symbol.

required
on_msg callable

Callback for BBAUpdate events after sink fanout.

None
sinks FeedSink or iterable[FeedSink]

Per-feed sink override. None uses global sinks; [] disables sinks.

None
feed_cls str

Feed market class. Defaults to FeedCls.PERPETUAL.

PERPETUAL

Returns:

Type Description
FeedId

quantpylib.standards.models.FeedId: Feed id for the latest event.

add_bba_feeds(exc, tickers, on_msg=None, sinks=None, feed_cls=FeedCls.PERPETUAL, **kwargs) async

Add multiple best bid/ask feeds as one batched subscription request.

Each returned feed receives quantpylib.standards.models.BBAUpdate objects for the corresponding ticker.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
tickers list

Ticker symbols.

required
on_msg callable or list

Shared callback or one callback per ticker. Each callback receives BBAUpdate events after sink fanout.

None
sinks optional

Per-feed sinks, broadcast or per-ticker. Broadcast (default): None (global sinks), a FeedSink, or a flat list of FeedSink, applied to every ticker. Per-ticker: a list of sink lists (or None), one entry per ticker (len(sinks) == len(tickers)); entry i is assigned to tickers[i], None falls back to global sinks, and [] disables sinks for that ticker.

None
feed_cls str

Feed market class. Defaults to FeedCls.PERPETUAL.

PERPETUAL

Returns:

Type Description
list[FeedId]

list[quantpylib.standards.models.FeedId]: Feed ids in ticker order.

add_l2_book_feed(exc, ticker, on_msg=None, sinks=None, depth=20, feed_cls=FeedCls.PERPETUAL, **kwargs) async

Add a level 2 order book feed.

Sinks and on_msg receive quantpylib.standards.models.BookUpdate objects. get_feed(feed_id) returns the latest received BookUpdate. Use OrderbookMaterializerSink when downstream code needs live order-book state.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
ticker str

Ticker symbol.

required
on_msg callable

Callback for BookUpdate events after sink fanout.

None
sinks FeedSink or iterable[FeedSink]

Per-feed sink override. None uses global sinks; [] disables sinks.

None
depth int

Feed identity depth parameter. Defaults to 20.

20
feed_cls str

Feed market class. Defaults to FeedCls.PERPETUAL.

PERPETUAL

Returns:

Type Description
FeedId

quantpylib.standards.models.FeedId: Feed id for the latest event.

add_l2_book_feeds(exc, tickers, on_msg=None, sinks=None, depth=20, feed_cls=FeedCls.PERPETUAL, **kwargs) async

Add multiple level 2 order book feeds as one batched subscription request.

Each returned feed receives quantpylib.standards.models.BookUpdate objects for the corresponding ticker. Use OrderbookMaterializerSink when downstream code needs live order-book state.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
tickers list

Ticker symbols.

required
on_msg callable or list

Shared callback or one callback per ticker. Each callback receives BookUpdate events after sink fanout.

None
sinks optional

Per-feed sinks, broadcast or per-ticker. Broadcast (default): None (global sinks), a FeedSink, or a flat list of FeedSink, applied to every ticker. Per-ticker: a list of sink lists (or None), one entry per ticker (len(sinks) == len(tickers)); entry i is assigned to tickers[i], None falls back to global sinks, and [] disables sinks for that ticker.

None
depth int

Feed identity depth parameter. Defaults to 20.

20
feed_cls str

Feed market class. Defaults to FeedCls.PERPETUAL.

PERPETUAL

Returns:

Type Description
list[FeedId]

list[quantpylib.standards.models.FeedId]: Feed ids in ticker order.

add_oracle_feed(exc, ticker, on_msg=None, buffer=100, **kwargs) async

Add a numeric oracle/mid feed.

The received values are appended to a RingBuffer and passed to on_msg when provided. This feed does not use the FeedSink fanout path.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
ticker str

Ticker symbol.

required
on_msg callable

Callback for numeric oracle values.

None
buffer int

Ring buffer capacity.

100

Returns:

Type Description
FeedId

quantpylib.standards.models.FeedId: Feed id for the buffer.

add_sampling_bars_feed(exc, ticker, buffer=100, feed_cls=FeedCls.PERPETUAL, bar_cls=None, **kwargs) async

Add a sampling bars feed.

Bars remain available as local aggregators in quantpylib.hft.bars. The Feed-level derived stream needs a cleaner materializer/sink contract before it is restored, so this API is intentionally inactive.

add_trades_feed(exc, ticker, on_msg=None, sinks=None, feed_cls=FeedCls.PERPETUAL, **kwargs) async

Add a trades feed.

Sinks and on_msg receive quantpylib.standards.models.TradeUpdate objects. get_feed(feed_id) returns the latest received TradeUpdate.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
ticker str

Ticker symbol.

required
on_msg callable

Callback for TradeUpdate events after sink fanout.

None
sinks FeedSink or iterable[FeedSink]

Per-feed sink override. None uses global sinks; [] disables sinks.

None
feed_cls str

Feed market class. Defaults to FeedCls.PERPETUAL.

PERPETUAL

Returns:

Type Description
FeedId

quantpylib.standards.models.FeedId: Feed id for the latest event.

add_trades_feeds(exc, tickers, on_msg=None, sinks=None, feed_cls=FeedCls.PERPETUAL, **kwargs) async

Add multiple trades feeds as one batched subscription request.

Each returned feed receives quantpylib.standards.models.TradeUpdate objects for the corresponding ticker.

Parameters:

Name Type Description Default
exc str

Exchange name.

required
tickers list

Ticker symbols.

required
on_msg callable or list

Shared callback or one callback per ticker. Each callback receives TradeUpdate events after sink fanout.

None
sinks optional

Per-feed sinks, broadcast or per-ticker. Broadcast (default): None (global sinks), a FeedSink, or a flat list of FeedSink, applied to every ticker. Per-ticker: a list of sink lists (or None), one entry per ticker (len(sinks) == len(tickers)); entry i is assigned to tickers[i], None falls back to global sinks, and [] disables sinks for that ticker.

None
feed_cls str

Feed market class. Defaults to FeedCls.PERPETUAL.

PERPETUAL

Returns:

Type Description
list[FeedId]

list[quantpylib.standards.models.FeedId]: Feed ids in ticker order.

cleanup() async

Cleanup the feeder object.

get_feed(feed_id)

Get the feed object or buffer associated with feed_id.

Parameters:

Name Type Description Default
feed_id str

The feed id.

required

get_feed_id(exc, feed_cls, feed_type, ticker, **kwargs) staticmethod

Get the feed id.

Parameters:

Name Type Description Default
exc str

The exchange.

required
feed_cls str

The asset class.

required
feed_type str

The feed type.

required

get_feed_ids()

Get list of all feed ids.

FeedCls

Asset classes.

Attributes:

Name Type Description
PERPETUAL str

Perpetual futures.

FUTURES str

Futures.

SPOT str

Spot.

OPTIONS str

Options.

FeedType

Types of feeds.

Attributes:

Name Type Description
L1BOOK str

Level 1 order book feed.

L2BOOK str

Level 2 order book feed.

L2DELTA str

Level 2 order book delta feed.

TRADES str

Trades feed.

MIDS str

Mid prices