Skip to content

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

HftbacktestWriter(config: PipelineConfig | None = None)

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: dict[str, DataFrame],
    dest: str | Path,
    **kwargs: Any,
) -> Path

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

to_hftbacktest_array(
    events: DataFrame, *, tick_size: float, lot_size: float
) -> np.ndarray

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 price back to the quote currency, which is what the engine reads.

required
lot_size float

The run's lot size, used to scale the integer volume back to the base asset, which is what the engine reads.

required

Returns:

Type Description
ndarray

A structured array of :data:HFT_EVENT_DTYPE, ordered by the time each record enters its own timeline. ival and fval are left at zero: they carry per-venue extras the canonical schema does not keep.

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

NautilusWriter(config: PipelineConfig | None = None)

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: dict[str, DataFrame],
    dest: str | Path,
    **kwargs: Any,
) -> Path

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

to_nautilus_deltas(
    events: DataFrame, *, tick_size: float, lot_size: float
) -> pd.DataFrame

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 price back to the quote currency.

required
lot_size float

The run's lot size, used to scale the integer volume back to the base asset.

required

Returns:

Type Description
DataFrame

The seven :data:NAUTILUS_DELTA_COLUMNS, indexed by the exchange clock as a UTC DatetimeIndex — the wrangler reads the index as the event time and passes it through as_utc_index.

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.