Skip to content

quantpylib.hft.replay_exchange

quantpylib.hft.replay_exchange.ReplayExchange

Owns authoritative market, execution, and account state for one venue.

Public QBN events mutate this object at exchange time. Requests from ReplayWrapper requests arrive after simulated TX latency and are subject to matching engine rules against ReplayOrderBook. Private updates are emitted to wrapper callbacks; the wrapper owns their RX latency.

Parameters:

Name Type Description Default
venue str

Gateway alias represented by this exchange.

required
clock ReplayClock

Clock domain for exchange-time authority.

required
impact_policy object

Public/private depth attribution policy for L2 to L3 queue projection in the matching engine orderbook.

None
queue_policy object

L2 add and cancellation policy for queue positions in the L2 to L3 queue projection.

None
maker_fee_bps Decimal or float

Maker fee in bps.

0
taker_fee_bps Decimal or float

Taker fee in bps.

0
fee_ccy str

Fee currency on fills, defaults to USDC.

'USDC'

Attributes:

Name Type Description
books dict[str, ReplayOrderBook]

Matching books by ticker.

orders dict[str, dict]

Complete venue order history by order ID.

fills dict[str, dict]

Fill history by fill ID.

positions dict[str, dict]

Current non-zero positions by ticker.

__init__(venue, clock, impact_policy=None, queue_policy=None, maker_fee_bps=0, taker_fee_bps=0, fee_ccy='USDC')

Initialize empty venue state and private-update subscriber lists.

api_cancel_open_orders(ticker=None)

Cancel every live order, optionally restricted to one ticker.

Parameters:

Name Type Description Default
ticker str

Ticker filter.

None

Returns:

Type Description

list[dict or None]: Cancellation results in venue insertion order.

api_cancel_order(specs)

Cancel one live order by exchange or client order ID.

Parameters:

Name Type Description Default
specs dict

Normalized cancellation specifications containing oid or cloid.

required

Returns:

Type Description

dict or None: Current order state, or None when no order

matches the supplied identity.

api_fill_updates_subscribe(handler)

Register a callback for venue fill updates.

Parameters:

Name Type Description Default
handler callable

Callback accepting one normalized fill dict.

required

api_fills_get(start_ns=None, end_ns=None)

Return fills in an exchange-time half-open interval.

Parameters:

Name Type Description Default
start_ns int

Inclusive exchange-time lower bound.

None
end_ns int

Exclusive exchange-time upper bound.

None

Returns:

Type Description

list[dict]: Copied normalized fills in insertion order.

api_order_query(oid=None, cloid=None)

Query complete order history by exchange or client order ID.

Parameters:

Name Type Description Default
oid str

Exchange order ID, preferred when supplied.

None
cloid str

Client order ID fallback.

None

Returns:

Type Description

dict or None: Copied order state when found.

api_order_updates_subscribe(handler)

Register a callback for venue order-state updates.

Parameters:

Name Type Description Default
handler callable

Callback accepting one normalized order dict.

required

api_orders_get()

Return copied authoritative states for all currently open orders.

Returns:

Type Description

dict[str, dict]: Open orders keyed by exchange order ID.

api_position_updates_subscribe(handler)

Register a callback for venue position updates.

Parameters:

Name Type Description Default
handler callable

Callback accepting one normalized position mutation dict.

required

api_positions_get()

Return copied current non-zero positions keyed by ticker.

Returns:

Type Description

dict[str, dict]: Normalized position snapshots.

api_submit_order(specs)

Submit one normalized limit order to venue truth.

A new exchange order ID is assigned before post-only validation. ALO orders that would cross are rejected; accepted orders are inserted into the matching book and may fill immediately.

Parameters:

Name Type Description Default
specs dict

Normalized order specifications.

required

Returns:

Name Type Description
dict

Current authoritative order state after matching.

dispatch_market(replay_event)

Apply one QBN event to authoritative venue state.

L2 events reconcile public liquidity. BBA and trade events may match owned resting orders; matching updates account state and emits private updates that can be subscribed to.

Parameters:

Name Type Description Default
replay_event ReplayEvent

Exchange-route public event.

required