Skip to main content

Inspectable execution simulator for order-book replay

Project description

ordersim

An inspectable, deterministic execution simulator for replaying real order-book data, with a Python-facing API and equivalent Python/C++ execution engines.

ordersim is built for researchers who need to audit every order intent, compare many strategies on the exact same replay, and let humans or AI agents write strategies against a small gateway API.

What It Does

  • Replays order-book data and simulates order execution with explicit order lifecycle events: place, cancel, fill, and passive fill.
  • Prefers a compiled C++ engine for ordinary replay when it is available, while keeping a plain Python reference engine for inspection and equivalence tests.
  • Exposes own resting orders with visible queue-ahead size during replay.
  • Runs multi-strategy A/B comparisons on the same replay while keeping each strategy's orders, position, and portfolio state isolated.
  • Exposes a small, regular Python API that is easy to read, debug, test, and extend.

What It Is Not

  • It is not a general backtesting framework. You own the strategy loop.
  • It is not a live trading system.
  • It is not a signal library.
  • It is not a speed-first HFT framework.

The design goal is clarity per line of code. Raw event throughput is secondary to understanding why an order did or did not fill, but compiled speed is welcome when it preserves the same observable behavior.

Why This Exists

Most backtests hide execution behavior behind aggregate PnL. That is not enough when the strategy depends on queue position, latency, cancel timing, partial fills, and passive resting orders.

ordersim treats the order-intent log as a first-class artifact. A run should answer:

  • What did the strategy try to do?
  • What did the simulated venue receive?
  • Which orders filled immediately?
  • Which orders rested?
  • How much visible queue remained ahead of a resting order?
  • Which orders were cancelled?
  • Which fills arrived passively later?
  • Do two strategy variants behave differently on the same market replay?

When To Use hftbacktest Instead

Use hftbacktest when you need a mature, speed-oriented HFT backtesting framework with Rust/Numba acceleration, queue-position models, latency models, and crypto-focused examples.

Use ordersim when you want a smaller library with a Python-facing API focused on inspectable execution replay:

Need Better Fit
HFT throughput and optimized hot loops hftbacktest
Crypto live-trading examples hftbacktest
Pure Python debuggability ordersim
Full order-intent audit logs ordersim
Deterministic multi-strategy comparisons on one replay ordersim
A small gateway API for human or AI-written strategies ordersim

The projects serve different workflows.

Engine Design

The pure Python engine is still the reference implementation, because it is the clearest place to inspect queue behavior and prove equivalence. Packaged wheels include the compiled CppMatchingEngine; ordinary Replay(...) runs prefer it because it preserves the same public contract while avoiding the Python hot loop. Source checkouts build the extension during normal installation:

python -m pip install -e ".[dev]"

If the extension is unavailable, Replay(...) falls back to the Python engine. Use the Python engine explicitly when you are debugging fill behavior, teaching the model, developing a new engine, or working in an environment where compiling extensions is not worth the friction:

from ordersim import MatchingEngine, Replay

replay = Replay(
    data=source,
    instrument=spec,
    execution_engine_factory=MatchingEngine,
)

Compiled-engine work is accepted only when it passes the same public equivalence fixtures as the Python engine.

Install

Install the current release from PyPI with:

pip install ordersim

Optional data integrations and file formats are installed separately as extras:

pip install "ordersim[databento]"
pip install "ordersim[parquet]"

Normalized CSV input works without optional dependencies:

from ordersim import CsvSource

source = CsvSource("events.csv")

For repeated research runs, the recommended path is to normalize once, materialize the canonical Parquet form, and replay from that thereafter:

import databento as db

from ordersim import DatabentoMboSource, ParquetSource, write_parquet

store = db.DBNStore.from_file("GLBX.MDP3-ES-20260102.mbo.dbn.zst")
raw_source = DatabentoMboSource(store)
write_parquet(raw_source, "events.parquet")

source = ParquetSource("events.parquet")

Direct raw-source replay is still useful for one-off inspection and connector development:

import databento as db

from ordersim import DatabentoMboSource

store = db.DBNStore.from_file("GLBX.MDP3-ES-20260102.mbo.dbn.zst")
source = DatabentoMboSource(store)

A Tiny Example

This example uses synthetic fixture data shipped with the package, so it does not require a market-data subscription. A runnable version lives in examples/canonical.py.

from decimal import Decimal

from ordersim import Replay
from ordersim.fixtures.synthetic import SyntheticSource
from ordersim.specs import InstrumentSpec


def strategy(gateway):
    gateway.advance_to(1_000_000_100)
    bid, ask = gateway.book_top()

    result = gateway.place_limit(
        side="buy",
        price=bid,
        size=1,
    )

    gateway.advance_to(gateway.now_ns() + 1_000_000_000)

    if gateway.position() == 0:
        if result.order_id is not None:
            gateway.cancel(result.order_id)
        gateway.place_market(side="buy", size=1)


spec = InstrumentSpec(
    symbol="GC",
    tick_size=Decimal("0.10"),
    point_value=Decimal("100"),
    commission_per_contract=Decimal("2.50"),
)

events = []
replay = Replay(
    data=SyntheticSource.small_mbo(),
    instrument=spec,
    record_to=events,
)

result = replay.run(strategy)

print(result.fills)
print(result.order_events)
print(result.execution_summary)
print(result.equity_curve)

The important output is not just final realized PnL. The important output is the fill ledger, equity curve, and event log showing what the strategy tried to do and what the simulated venue did in response.

Strategies advance replay time explicitly with gateway.advance_to(...); the library supplies execution semantics, not a strategy framework.

Replay can also apply entry latency before orders and cancels reach the simulated venue:

from ordersim import ConstantLatency

replay = Replay(
    data=SyntheticSource.small_mbo(),
    instrument=spec,
    latency_model_factory=lambda: ConstantLatency(entry_ns=25_000_000),
)

Multi-Strategy Replay

ordersim can run several strategy variants over the same market replay while keeping private state isolated:

result = replay.run_many(
    {
        "baseline": baseline_strategy,
        "wider_quote": wider_quote_strategy,
        "faster_cancel": faster_cancel_strategy,
    }
)

The intended guarantee is solo-equivalence: the fills for baseline inside run_many() should match the fills from running baseline by itself on the same input.

Status

Planned release sequence:

  • v0.1: Python reference engine, packaged C++ default, and a canonical connector -> Parquet -> replay workflow.
  • v0.2: broaden connector recipes, latency experiments, and notebook-first research ergonomics while keeping the C++ and Python engines equivalent.
  • v1.0: research-grade execution lab with notebook-first workflows, connector recipes, latency model gallery, and public replay-equivalence harness.

Documentation

  • Assumptions: docs/assumptions.md
  • Execution economics: docs/economics.md
  • Execution engines: docs/execution-engines.md
  • Latency models: docs/latency.md
  • Architecture: docs/architecture.md
  • Data guide: docs/data-guide.md
  • Connectors: docs/connectors.md
  • Releasing: docs/releasing.md
  • Engineering standards: docs/engineering-standards.md
  • Example: examples/canonical.py
  • Schema reference: docs/schema.md
  • AI agent guide: AGENTS.md

For execution-engine work, SyntheticSource.execution_equivalence_mbo() gives contributors a tiny public queue-priority fixture to use with the equivalence harness.

Compiled or alternative execution engines should pass ordersim.testing.assert_execution_equivalence_suite(...) before they are trusted.

Contributing

The easiest first contribution is a data connector.

Good connector PRs:

  • implement the DataSource protocol;
  • include a tiny fixture or generator;
  • document source timestamp semantics, UTC normalization, price, size, and order-id semantics;
  • add at least one replay test.

For simple examples, prefer the canonical CsvSource schema before adding a new vendor-specific connector.

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ordersim-0.1.1.tar.gz (43.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ordersim-0.1.1-cp312-cp312-win_amd64.whl (130.1 kB view details)

Uploaded CPython 3.12Windows x86-64

ordersim-0.1.1-cp312-cp312-win32.whl (121.3 kB view details)

Uploaded CPython 3.12Windows x86

ordersim-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

ordersim-0.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (161.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ordersim-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (139.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ordersim-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl (144.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

ordersim-0.1.1-cp311-cp311-win_amd64.whl (129.1 kB view details)

Uploaded CPython 3.11Windows x86-64

ordersim-0.1.1-cp311-cp311-win32.whl (120.9 kB view details)

Uploaded CPython 3.11Windows x86

ordersim-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

ordersim-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (160.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ordersim-0.1.1-cp311-cp311-macosx_11_0_arm64.whl (138.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

ordersim-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl (143.0 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

Details for the file ordersim-0.1.1.tar.gz.

File metadata

  • Download URL: ordersim-0.1.1.tar.gz
  • Upload date:
  • Size: 43.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ordersim-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3f580327718a8f3ffd6a467a95e4be724ff035426ee8d0cacf67b0b230889752
MD5 c416da9e89315773efe615ff0b884965
BLAKE2b-256 a74654ef8641f05e223eb9f59e42d5bcfe18f9814baa26c31a20743afb317fbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1.tar.gz:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: ordersim-0.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 130.1 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ordersim-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 526ce85689f3a0d08e0de26341b9bac0c32940d7b106b91ef5af51b4c9ed7025
MD5 2a97da337d469cbf70a9e5080802663e
BLAKE2b-256 25d2700bf808aa61464721c6ccd77b5b304692eb36a38059f9c525ace4493bc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp312-cp312-win_amd64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp312-cp312-win32.whl.

File metadata

  • Download URL: ordersim-0.1.1-cp312-cp312-win32.whl
  • Upload date:
  • Size: 121.3 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ordersim-0.1.1-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 2dfbd201b6bca0247729775f3810aa03e4c478f6e949297307fb66224075a81f
MD5 cc2abc990dbb4f2f38bd1c018d4c4e2a
BLAKE2b-256 a2f115115d12c030c1efa87c94087241bba696a412f6f857828872d574d6dcdf

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp312-cp312-win32.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fc5cff5f7e1b3610d1f2d0b90f4818ba4594a74963e5f08fd4e75f9910fca657
MD5 1070832273de476dca33ceb9796e880c
BLAKE2b-256 39ed2675064f603a30f0ba50725a58794c6be00a58bf3d019dc860dc18e932c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 531648f8315863103bc476fb1e83da05da9084d568b072b26b0972d1f12af50e
MD5 81403328adf2853c3b7383ec8257cbce
BLAKE2b-256 c27bdf65d68d5f1a19cec12099fadf2c569dbea078ef0342e4039ea8af20789f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 425b3526c915a840c592d268e3eb4977bb3b7c72ecda7f0a0e1322f84b4a2798
MD5 5ba07057e83cf2bf993d57460c1e6e5a
BLAKE2b-256 1fd86389757d479f8bc46ecf22a1ed73122c0abd3520491a34a3f57574a34ade

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 7618a6a0f4c1e67e281449eb3f7dfcb247119c92e1a30d8e837bd72a039170c8
MD5 e1418b173da52919e7811a886d00e143
BLAKE2b-256 14cef6a55c3d11872d731e47cf0852adc6d1fffcae7a1d1be7ab6b4d463209e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: ordersim-0.1.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 129.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ordersim-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f93dd981cddf1568a87495883254428d0c8ce6551e018ac275f2a9616ef0b23b
MD5 d7f83bdff7191b54adbaeb70868e3f22
BLAKE2b-256 249bed719923faf88def2d0341f903fc2a73428b9e4c19e129b32b2d18ec159e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp311-cp311-win_amd64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp311-cp311-win32.whl.

File metadata

  • Download URL: ordersim-0.1.1-cp311-cp311-win32.whl
  • Upload date:
  • Size: 120.9 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ordersim-0.1.1-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 c00b0c11ba9a77e230f8f595be228a73f69a70e4e4bd6df9d9e3e3c6080759bf
MD5 8ae52b753f745dffdacde01904e8fd1b
BLAKE2b-256 c309c48ba09540cf8add68647623fb7b2a6a7eb920e55d0a4528f45a146bb19f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp311-cp311-win32.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d20bdcc4c15c3e7fd46e3964329ba88f4c92afa64e0225462a96063d0aa23f5a
MD5 fcc365a7139bf1b20391d65126156462
BLAKE2b-256 4d1d192895a2d495aeb51b362a5206986c059d4eeb15572903b4daf730b149d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 77c75b7cb227e8b39553ff31871af1bee7ef43cc0a1aed5e240ed7846b4b9d9f
MD5 e9b3553c65b5bd802133fdab6a350d76
BLAKE2b-256 f1df5993443a71aa95f4542b37f28854445ad5ef0b6bc70e60baa19ce216f747

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6c06f15bf20db1a2f9fcd53eb0973bed5d131873282ca3d9cbdefd349a9a48a2
MD5 d02138df283f4504c8b4fd8dd8a7db80
BLAKE2b-256 6ff44469f015561662a2c3a96365b5815c542992470bd6f59225070ed18a097f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ordersim-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 73bdca20342f74910e6c3a7c8e3fad1e24a31aa31af89684c63a829ae3cb8503
MD5 fea0aa7c970105260f13180f13eec404
BLAKE2b-256 76d1076a537b47307cced95a515a72ef5f78dea8cff726ea19b4258285dc1f88

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: release.yml on tradingexpert/ordersim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page