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=1000)
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 |
required |
trade_data
|
dict
|
Trade data mapping |
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 |
None
|
latencies
|
dict
|
Latency mapping in ms for each message channel. Defaults to |
{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 |
None
|
exchange_latencies
|
dict
|
The exchange latencies specify latencies |
None
|
stamp_cycle
|
int
|
The interval in ms used for stamping backtest statistics such as portfolio equity, inventory. Defaults to 1000. |
1000
|
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 |
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 |
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
|
augment_forward_prices(df, forward_intervals=[0, 1, 10, 30, 60, 120, 300])
Augment the dataframe with forward prices for each interval. df
should contain columns
ts,exc,ticker
. to match forward prices.
declare(key, value, exc=None, ticker=None, group=None)
Declare a value for logging purposes. Can later be used for tick-data modelling and analysis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key. |
required |
value
|
Any
|
The value. |
required |
exc
|
str
|
The exchange. Defaults to None. |
None
|
ticker
|
str
|
The ticker. Defaults to None. |
None
|
group
|
str
|
The group this key belongs to. Defaults to None. |
None
|
df_actions(exc=None, ticker=None)
Get a dataframe of all order fills, with columns ts,price,size,dir,is_maker,exc,ticker
.
df_counterparty_pnl(exc_tickers, plot=True, show=True, save=None)
Get the normalized counterparty pnl comparison for mapping between exchange and list of tickers. This simulates being the maker to all market orders that are traded on the exchange's market with zero fees.
df_declarations(key=None, exc=None, ticker=None, group=None)
Retrieve the declarations logs as a dataframe.
df_exchange(exc, plot=True, show=True, save=None)
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
|
show
|
bool
|
Show the plot. Defaults to True. |
True
|
save
|
str
|
Saves the plot to specified filename if provided. Defaults to None. |
None
|
df_fairprices(group, key=None, exc=None, ticker=None, forward_intervals=[0, 1, 10, 30, 60, 120, 300], plot=False, show=True, save=None)
If a group declared in Replayer.declare
is a fairprice estimator, this method performs a fairprice analysis and metrics comparison
by common objectives such as MSE, MAD, etc by comparing the declared fairprice with the actual price at different forward intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group
|
str
|
The group. |
required |
key
|
str
|
The key. Defaults to None. |
None
|
exc
|
str
|
The exchange. Defaults to None. |
None
|
ticker
|
str
|
The ticker. Defaults to None. |
None
|
forward_intervals
|
list
|
The forward intervals in seconds. Defaults to |
[0, 1, 10, 30, 60, 120, 300]
|
plot
|
bool
|
Plot the results. Defaults to False. |
False
|
show
|
bool
|
Show the plot. Defaults to True. |
True
|
save
|
str
|
Saves the plot to specified filename if provided. Defaults to None. |
None
|
df_fills(ticker, exc, plot=True, ax=None, show=True, save=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
|
show
|
bool
|
Show the plot. Defaults to True. |
True
|
save
|
str
|
Saves the plot to specified filename if provided. Defaults to None. |
None
|
df_markouts(ticker=None, exc=None, plot=True, show=True, save=None, intervals=list(np.linspace(1, 151, 31) * 1000))
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 markouts. Defaults to True. |
True
|
show
|
bool
|
Show the plot. Defaults to True. |
True
|
save
|
str
|
Saves the plot to specified filename if provided. Defaults to None. |
None
|
intervals
|
list
|
The intervals in ms. Defaults to |
list(linspace(1, 151, 31) * 1000)
|
df_portfolio(plot=True, show=True, save=None)
Get the portfolio pnl breakdown by exchange.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plot
|
bool
|
Plot the portfolio pnl breakdown. Defaults to True. |
True
|
show
|
bool
|
Show the plot. Defaults to True. |
True
|
save
|
str
|
Saves the plot to specified filename if provided. Defaults to None. |
None
|
df_prices(ticker, exc, plot=True, ax=None, with_fill=True, with_inventory=None, key=None, group=None, share_index=False, show=True, save=None)
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
|
with_inventory
|
bool
|
Plot inventory on the price plot. Defaults to truth value of |
None
|
key
|
str
|
If group is provided, the values of corresponding key are overlayed on the price plot. Defaults to None. |
None
|
group
|
str
|
The group. Defaults to None. |
None
|
share_index
|
bool
|
Share the x-axis index with the price plot for plotted key. Defaults to False. |
False
|
show
|
bool
|
Show the plot. Defaults to True. |
True
|
save
|
str
|
Saves the plot to specified filename if provided. Defaults to None. |
None
|
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.
statistics()
Get a dictionary of statistics for each exchange after the replay.
statistics_exc(exc)
Get a dictionary of statistics for a specific exchange after the replay.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc
|
str
|
The exchange. |
required |
time()
Get millisecond timestamp of the replayer is at during play
.