Backtester export¶
Writers for two backtesting engines: hftbacktest and Nautilus Trader. Neither
engine is a dependency. save_data(..., fmt="hftbacktest") and
save_data(..., fmt="nautilus") call these writers; the two to_* functions
return the same data without writing a file. See
Export to a backtester.
HftbacktestWriter ¶
Write a run's events as an hftbacktest .npz feed file.
Satisfies the :class:~ob_analytics.protocols.DataWriter protocol; reached
as save_data(..., fmt="hftbacktest"). It writes one file.
write ¶
Write data["events"] to dest as a compressed npz.
dest is a file, written at exactly that path (.npz is the usual
extension). The array lands under the "data" member, which is the
one hftbacktest loads. Raises :class:~ob_analytics.exceptions.ConfigError
when the events have no rows: an L2 run has no per-order events.
to_hftbacktest_array ¶
Return events as an hftbacktest feed array, on two timelines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
DataFrame
|
Canonical events. |
required |
tick_size
|
float
|
The run's tick size, used to scale the integer |
required |
lot_size
|
float
|
The run's lot size, used to scale the integer |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
A structured array of :data: |
Notes
hftbacktest replays two clocks: an event reaches the exchange at
exch_ts and the strategy at local_ts, which is how it models feed
latency. ev carries a flag for each timeline, and its
validate_event_order requires the records flagged for a timeline to be
non-decreasing in that timeline's clock.
The two orders are not the same order. On the bundled Bitstamp sample the
exchange stamp leads the receive stamp by 0.08 to 2.7 seconds and the two
disagree on the order of 14,966 of 314,057 events, so no single sequence of
one record per event can be sorted for both. An event whose clocks disagree
is therefore written twice, once per timeline, which is the same shape
the engine's own correct_event_order produces. An event whose clocks
agree stays one record carrying both flags.
NautilusWriter ¶
Write a run's events as a Nautilus order-book delta file.
Satisfies the :class:~ob_analytics.protocols.DataWriter protocol; reached
as save_data(..., fmt="nautilus"). The output is Parquet, so
pd.read_parquet hands OrderBookDeltaDataWrangler.process the frame it
wants, index and dtypes intact. It writes one file.
write ¶
Write data["events"] to dest as one Parquet file of deltas.
Raises :class:~ob_analytics.exceptions.ConfigError when the events
have no rows: an L2 run has no per-order events.
to_nautilus_deltas ¶
Return events in the frame shape Nautilus' delta wrangler takes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
DataFrame
|
Canonical events. |
required |
tick_size
|
float
|
The run's tick size, used to scale the integer |
required |
lot_size
|
float
|
The run's lot size, used to scale the integer |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The seven :data: |
Notes
flags is zero throughout: it carries Nautilus' record flags, of which
F_SNAPSHOT is the one that matters, and a canonical event stream is a
stream of deltas rather than a snapshot. sequence uses the venue's own
sequence when the source published one and falls back to the dense
event_id, so the column is always a usable order.