Skip to main content

Inspectable execution simulator for order-book replay and latency-aware fill simulation

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.

Most backtests collapse execution into a final PnL line. ordersim exists to inspect the path between order intent and actual fills.

Same replay, same strategy, different latency

It 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.

Scope

ordersim focuses on execution replay and execution-aware simulation. Areas of interest include order-book replay, market replay, latency modeling, queue-position effects, fill simulation, execution modeling, execution-aware backtesting, and market microstructure research.

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

Aggregate PnL 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?

The editorial argument for this kind of inspection lives at Trading Reality — Markets in Production

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 is the compiled implementation the project intends to keep equivalent and scale over time. The direct C++ batch-ingest path is already substantially faster for callers that own the event loop; ordinary audited Replay(...) currently remains event-by-event so it can record per-event valuation marks. 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),
)

For a visual latency comparison, run examples/latency_demo.py.

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

0.1.x is live on PyPI. The current public line includes the Python reference engine, packaged C++ default, canonical connector -> Parquet -> replay workflow, latency models, economics, and public execution-equivalence fixtures.

Planned next milestones:

  • 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
  • Benchmarks: docs/benchmarks.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.3.tar.gz (52.6 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.3-cp312-cp312-win_amd64.whl (144.4 kB view details)

Uploaded CPython 3.12Windows x86-64

ordersim-0.1.3-cp312-cp312-win32.whl (133.7 kB view details)

Uploaded CPython 3.12Windows x86

ordersim-0.1.3-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.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (178.1 kB view details)

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

ordersim-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (154.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ordersim-0.1.3-cp312-cp312-macosx_10_13_x86_64.whl (159.7 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

ordersim-0.1.3-cp311-cp311-win_amd64.whl (143.3 kB view details)

Uploaded CPython 3.11Windows x86-64

ordersim-0.1.3-cp311-cp311-win32.whl (133.2 kB view details)

Uploaded CPython 3.11Windows x86

ordersim-0.1.3-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.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (176.1 kB view details)

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

ordersim-0.1.3-cp311-cp311-macosx_11_0_arm64.whl (153.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

ordersim-0.1.3-cp311-cp311-macosx_10_9_x86_64.whl (159.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: ordersim-0.1.3.tar.gz
  • Upload date:
  • Size: 52.6 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.3.tar.gz
Algorithm Hash digest
SHA256 c5d7b431b90229d1666725c35f9deba43c4e1015c77aa27e1e5ca82b43121754
MD5 a07b1b6bac93eeaf416a762a4f58e7fc
BLAKE2b-256 a1e5d3d1eb1b863e92290a4506c32e02a0d3e29dde783b5e26f3f60001ab4dcd

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 144.4 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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d6fbc197ac206c189b321cc27e41d996729c83ab65d73a679da81f206f7239c5
MD5 15dea4fab585219c1d411fcb42f39e0c
BLAKE2b-256 c743fc732f39345c2caa13a78b392db019e5addb130c6350477e21c4385565d7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.3-cp312-cp312-win32.whl
  • Upload date:
  • Size: 133.7 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.3-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 22abdfb9da7379886124b70aeed1397ee47f5250f23d0eabbd3d81dac9c306ce
MD5 4660edca6805a4a5c1d65b088aa0e2bd
BLAKE2b-256 193893ec9cfb4489b8d4521b6d5be41ab42e0845d365cc62e38b8998df3fbd23

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5f875ceade97214b97360649db5c8cd42049885155dbe261171332325b5be59b
MD5 43abde90ab0003809088c95d28f312de
BLAKE2b-256 e0fd4f8499428650d987cdfb037d82113a5fa693ecfe44f5f9f6ad62f2ca33aa

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a3cf42981d15b2f051d8a747b9d4b34cfdb258fd681006e465a2d7ba07688164
MD5 77954124876b7036ef006477b686a559
BLAKE2b-256 91c6ad6188b39e96a2522fd81fac9edd0c1d9b258aef6f803a0a6c359e137dfd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a882cf4adda53cb6ca02d6d945bfa6247ebf8250e222fa55ef5d56e2b2a591a7
MD5 3dbdd3f8fa9f7dda4c548abbb18e5409
BLAKE2b-256 410e60090b70c74bc828aa3a41fd8636e9475cf6ec0e733436d9df248d3ff15f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5f7274f66dad2431ff5a328a74b5fb4ac5e711060abf4aa91f3d971028c97232
MD5 3d1983f81289191495d8bea504051546
BLAKE2b-256 53e9a0cbfa268e228597dbb94c8adf7a9334b0bb108223d1ba0cbac5e0e7512d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 143.3 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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 408f961e3c4ed44af7a2984441f8215f4829fea8b6250bd7d687a8fd20010869
MD5 af5657f3e2a76602c3d4e8c0c475312a
BLAKE2b-256 482d55883023fceec8027d5685fcb780fd5f24a133c77992e680c00a2cb4fc30

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.3-cp311-cp311-win32.whl
  • Upload date:
  • Size: 133.2 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.3-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 0bbe4e0e19547c93591d2ceb8130f655342e033a3456139ea93f7a2e709e702c
MD5 03bd60f187d2a0c3ac9e2255045186e6
BLAKE2b-256 71074e678bba02259308af02db1a233dd95befab07f8ac7091d119a0207a542d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9ac4c8078311eee61d02c47bffea84de09e823601208629b4c34a7f4a09a9252
MD5 343be00b7b31295ce6bb77d27b8c53dd
BLAKE2b-256 ca3389b591c018948d26b569126c3f5ca103869f583c8b5196cd5152374198e4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b98975a6306b3de4bc878e2711e9e34e16d9c2891d43a69ccd079eda55efa137
MD5 ec3265172fe81cfb91d51a0c784c7462
BLAKE2b-256 9a21d34eb8aa7e3f0315bc624a97ac08d807bab8a307df7f2562807989616a96

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5554ab0b6df56f91429c4c8694b7f08de0677d94e83d69a449277963351e95ad
MD5 a652876ad2a4557390cc42c96664c1f8
BLAKE2b-256 40c98772c43c669cab7918e82f3005a24c8be326e225b3571742b8dd7ae8b5fb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f4070d2cdcbe80c1e53d14c7f3181f5ea6f7225ba11ccb34133edb667cc7ae49
MD5 8ad4891848330d716b11a75f0ce4a626
BLAKE2b-256 5a65de9149dee35751f7eed633ea1620144289b127d15aab8d54e51c00dc56e9

See more details on using hashes here.

Provenance

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