Skip to content

quantpylib.hft.mocks

The mocks module contains classes that replicate the quantpylib.hft.feed.Feed and quantpylib.hft.oms.OMS trading agents, as well as the Replayer class that drives these mock classes. The Replayer has auxiliary statistical and graphical utility functions.

Latencies

Latency types/constants.

MockFeed

Bases: Feed

add_l2_book_feed(exc, ticker, handler=None, depth=20, buffer=100, **kwargs) async

Add a level 2 book feed.

Parameters:

Name Type Description Default
exc str

The exchange.

required
ticker str

The ticker.

required
handler coroutine

The handler to add. Defaults to None.

None
depth int

The depth of the order book. Defaults to 20.

20
buffer int

The buffer size. Defaults to 100.

100

add_trades_feed(exc, ticker, handler=None, buffer=100, **kwargs) async

Add a trades feed.

Parameters:

Name Type Description Default
exc str

The exchange.

required
ticker str

The ticker.

required
handler coroutine

The handler to add. Defaults to None.

None
buffer int

The buffer size. Defaults to 100.

100

MockOMS

Bases: OMS

add_clock_callback(callback, name='CLOCK', interval_ms=500) async

Add a callback to the event queue that is triggered every interval_ms. Can be used for clock-based trading agents.

Parameters:

Name Type Description Default
callback coroutine

The callback to trigger.

required
name str

The name of the callback. Defaults to 'CLOCK'.

'CLOCK'
interval_ms int

The interval in milliseconds. Defaults to 500.

500

get_positions_value(exc)

Get the net signed value of all open positions for an exchange at replay time.

lagged_price(exc, ticker)

Get the price for a given exchange and ticker at current replay time.

Parameters:

Name Type Description Default
exc str

The exchange.

required
ticker str

The ticker.

required

ReplayEvents

Replay event types/constants.

Replayer

__init__(l2_data, trade_data, maker_fees=0.0001, taker_fees=0.0003, gateway=None, exchanges=None, latencies={Latencies.REQ_PUBLIC: 100, Latencies.REQ_PRIVATE: 50, Latencies.ACK_PUBLIC: 100, Latencies.ACK_PRIVATE: 50, Latencies.FEED_PUBLIC: 100, Latencies.FEED_PRIVATE: 50}, exchange_fees=None, exchange_latencies=None, stamp_cycle=5000)

A replayer for backtesting. This class replays orderboook depth data, trade data and provides a mock feed and oms classes to replay agent-based communications between the trading logic and the exchange. Simulates data feed, order submissions, matching, latencies and more.

Parameters:

Name Type Description Default
l2_data dict

Level 2 data mapping {exc:{ticker:[{ts,b,a}]}}.

required
trade_data dict

Trade data mapping {exc:{ticker:[(ts,price,sz,dir)]}}.

required
maker_fees float

The maker fees. Defaults to 0.0001.

0.0001
taker_fees float

The taker fees. Defaults to 0.0003.

0.0003
gateway `quantpylib.gateway.master.Gateway`

The gateway used to instantiate contract rules. Defaults to None.

None
exchanges list

The list of exchanges. Defaults to keys inside the l2_data dict.

None
latencies dict

Latency mapping in ms for each message channel. Defaults to 50 for all private and 100 for public communications. Keys are Latencies.REQ_PUBLIC, Latencies.REQ_PRIVATE, Latencies.ACK_PUBLIC, Latencies.ACK_PRIVATE, Latencies.FEED_PUBLIC, Latencies.FEED_PRIVATE.

{REQ_PUBLIC: 100, REQ_PRIVATE: 50, ACK_PUBLIC: 100, ACK_PRIVATE: 50, FEED_PUBLIC: 100, FEED_PRIVATE: 50}
exchange_fees dict

The exchange fees specify fees dict by exchange. Defaults to maker_fees and taker_fees for each exchange.

None
exchange_latencies dict

The exchange latencies specify latencies dict by exchange. Defaults to latencies for each exchange.

None
stamp_cycle int

The internval in ms used for stamping backtest statistics such as portfolio equity, inventory. Defaults to 500.

5000

add_callback(event_id, callback)

Add a callback to an event.

Parameters:

Name Type Description Default
event_id str

The event id.

required
callback coroutine

The callback for events belonging to event_id.

required

add_events(event_id, timestamps, events, exc, latency=Latencies.NULL)

Add events to the replayer queue.

Parameters:

Name Type Description Default
event_id str

The event id.

required
timestamps list

The timestamps of event occurence.

required
events list

The events. Note len(timestamps) == len(events).

required
exc str

The exchange.

required
latency str

The latency for event arrival added to the timestamps of event occurence. Defaults to Latencies.NULL, zero latency.

NULL

add_to_queue(event_id, timestamp, msg, exc, latency=Latencies.NULL)

Add an event to the replayer queue.

Parameters:

Name Type Description Default
event_id str

The event id.

required
timestamp int

The timestamp of event occurence.

required
msg Any

The message/event.

required
exc str

The exchange.

required
latency str

The latency for event arrival added to the timestamps of event occurence. Defaults to Latencies.NULL, zero latency.

NULL

df_actions()

Get a dataframe of all order fills, with columns ts,price,size,dir,is_maker,exc,ticker.

df_exchange(exc, plot=True)

Get the equity and inventory breakdown for an exchange.

Parameters:

Name Type Description Default
exc str

The exchange.

required
plot bool

Plot the pnl and inventory breakdown. Defaults to True.

True

df_fills(ticker, exc, plot=True, ax=None)

Get fills for a ticker and exchange.

Parameters:

Name Type Description Default
ticker str

The ticker.

required
exc str

The exchange.

required
plot bool

Plot the fills. Defaults to True.

True
ax Axes

The axes to plot on. Defaults to None.

None

df_markouts(ticker=None, exc=None, plot=True)

Get markouts for a ticker and exchange.

Parameters:

Name Type Description Default
ticker str

The ticker. Defaults to None. If None, all tickers are aggregated.

None
exc str

The exchange. Defaults to None. If None, all exchanges are aggregated.

None
plot bool

Plot the percentage markouts from next tick to 30 seconds out. Defaults to True.

True

df_portfolio(plot=True)

Get the portfolio pnl breakdown by exchange.

Parameters:

Name Type Description Default
plot bool

Plot the portfolio pnl breakdown. Defaults to True.

True

df_prices(ticker, exc, plot=True, ax=None, with_fill=True)

Get tick prices for a ticker and exchange.

Parameters:

Name Type Description Default
ticker str

The ticker.

required
exc str

The exchange.

required
plot bool

Plot the prices. Defaults to True.

True
ax Axes

The axes to plot on. Defaults to None.

None
with_fill bool

Plot fills on the price plot. Defaults to True.

True

get_feed()

Get a MockFeed instance that can be used like a quantpylib.hft.feed.Feed instance.

get_mid(exc, ticker)

Get the mid price known at current replay time.

Parameters:

Name Type Description Default
exc str

The exchange.

required
ticker str

The ticker.

required

get_oms()

Get a MockOMS instance that can be used like a quantpylib.hft.oms.OMS instance.

play() async

Play the replayer. To be used in place of await asyncio.sleep(x) that sleeps while the trading logic runs.

time()

Get millisecond timestamp of the replayer is at during play.