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

ordersim is in early public setup and is not published to PyPI yet. The planned install path for the first release is:

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.0.tar.gz (43.4 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.0-cp312-cp312-win_amd64.whl (130.1 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12Windows x86

ordersim-0.1.0-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.0-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.0-cp312-cp312-macosx_11_0_arm64.whl (139.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.13+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11Windows x86

ordersim-0.1.0-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.0-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.0-cp311-cp311-macosx_11_0_arm64.whl (138.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

ordersim-0.1.0-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.0.tar.gz.

File metadata

  • Download URL: ordersim-0.1.0.tar.gz
  • Upload date:
  • Size: 43.4 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.0.tar.gz
Algorithm Hash digest
SHA256 ca5e1d84c52f90816099f9253d9b1fe5bcb7a8f0b7fbddf3ee01edb58cd0aadf
MD5 ab0b8a99909d11fab1b6de001b00dd2a
BLAKE2b-256 00ba97623538121e5fd822089ae8b069fd8e76f956fe2e7b760ab73d1b359476

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0.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.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: ordersim-0.1.0-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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9abe500761821b50a83f190c0a5b53013e69151c4ac8274bd2e5c39ea72db1d6
MD5 972ffef60ed1390f92250480c5e9702f
BLAKE2b-256 54532ce10d65e7d737be5cd12220766cb2f7a0161ef7b2db2ab6cec68405cde9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp312-cp312-win32.whl.

File metadata

  • Download URL: ordersim-0.1.0-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.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 119d635005ba0bd0e605b9e38b0bbcfc699bc9990184c56aff305e683a79a335
MD5 b67dbd0ec9a4c342aed0517b9f2bbf47
BLAKE2b-256 a8e5fffb3ee2e4dc62577c8ec3a15e0b9ce4fc99e2b88cce7e75366eab584a14

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8f6b32350038ed59080df0bcd5b80ffcf4acbde0fd5e8602cfdbb401e66b65cf
MD5 64e0a58e109bae27243d6660e8578dbc
BLAKE2b-256 aa3afe123c7283d3cc4b38886da71012ba3f9e81a06fc234195c7f626df39174

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 db8493301bba21ad6b0d0424f59b2e6aa305da0a09caf911a63551f5d8173ea4
MD5 127f8cc3459da2a0e89d39426f9deffc
BLAKE2b-256 5c37dd1dcbe4fa5270a3fac1b39b7664f26f4d436bcda15abaa41e688db8a4f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b23c8de84a9f0726bc2bdcd388ad1c1611fa87e1eadde413db49f94e30ee24f2
MD5 7638c96383d0ec38994a6d3703721ec7
BLAKE2b-256 35e127d3823bfab776a33a61251365adbdcbdcfa0144600f91a8b37e60857a01

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 07f0eee4505ffb3781bb260c37915009efb1c9ce63aa5683110a3b01aa125ecd
MD5 0f09e62ee1ce1627abdf235cb4ffa1da
BLAKE2b-256 0f49d0417e17f6c8e66053423f81960beabe5fa7ecc507586ca79d499d39671e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: ordersim-0.1.0-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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4206d713b2a29157e686070b712c7f965bc8fbf74fbfd02e20948a10dfb52a0e
MD5 b26ea5565583226eada355e24a26f4ab
BLAKE2b-256 915c18392d13f5405e60f7e1e216737181acf17413509da3fd70a24ef43da569

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp311-cp311-win32.whl.

File metadata

  • Download URL: ordersim-0.1.0-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.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 dcb08f27e75971679cb8f06c8f8ba7d1cd5e677f6c593c44324c78745dd421c8
MD5 b2aa9583cd0c68baf8d63decdd3430e9
BLAKE2b-256 fd05dd70fabff884905fd6ecd89a4492111a03de0d3262c6183a20f40fe5b9d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 068dfba1bbb214f7041f221d1a890ed53e851ac70f1d4f94b9b7eaf6905d3a5c
MD5 9ca90b747f9f741e0e14c9fad5c7587d
BLAKE2b-256 4df5fd178b167b7f32d219676703a7266170b7366887f28b50075308e191eac9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dbf68acc3fa6120be1190586b5b1b71508f9a9fe15447be5baaddb60820ad053
MD5 4dda2c2af62211ca2b45210a4c72921d
BLAKE2b-256 7b7db8ddefdd6922475271d39f6aa46a88ba2f82b2a3c5698c98ac250506d32c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aac56e3a0c214225a85ff87968cdca81aac911897d54d8fc1ed32615af7b5ba3
MD5 e6f1adecda3f5651b65e069543bd5e1e
BLAKE2b-256 4f8e1420a95e7bde706ec2866b3420b1207d8612239cedb5ea8f553333a9190a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ordersim-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7f66b9e5af8f1fa8cd220266e28581156cbf1138b3baeea9c33ac7d3a0a6a7e1
MD5 6337f61c149c04f0700fda635d8a3bc2
BLAKE2b-256 585b867ba1d15220ef6d7eb64cf9e73ec1e9101708d79b0f952e87640e456ea3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ordersim-0.1.0-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