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.

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.

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?

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),
)

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

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.2.tar.gz (44.0 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.2-cp312-cp312-win_amd64.whl (130.4 kB view details)

Uploaded CPython 3.12Windows x86-64

ordersim-0.1.2-cp312-cp312-win32.whl (121.6 kB view details)

Uploaded CPython 3.12Windows x86

ordersim-0.1.2-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.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (161.9 kB view details)

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

ordersim-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (139.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ordersim-0.1.2-cp312-cp312-macosx_10_13_x86_64.whl (144.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

ordersim-0.1.2-cp311-cp311-win_amd64.whl (129.4 kB view details)

Uploaded CPython 3.11Windows x86-64

ordersim-0.1.2-cp311-cp311-win32.whl (121.2 kB view details)

Uploaded CPython 3.11Windows x86

ordersim-0.1.2-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.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (160.5 kB view details)

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

ordersim-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (139.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

ordersim-0.1.2-cp311-cp311-macosx_10_9_x86_64.whl (143.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: ordersim-0.1.2.tar.gz
  • Upload date:
  • Size: 44.0 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.2.tar.gz
Algorithm Hash digest
SHA256 7918cf03bc59dd02d346974231d261cf8f4e06f1bac9f234a91e7aa8cc0f086f
MD5 e5f538c940e25b7a81591f0a4ab5870a
BLAKE2b-256 afb2506200fc76da2937b903a91143e7f03985841ed22d2c5a2fd3656972bb91

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 130.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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4f373eb4d3fae9bf2436cc209a92310bbaa41463aeea5ea9c4b8bad521698a5f
MD5 b4e8a1d7dd11c0c0d2ff47314c4f6076
BLAKE2b-256 8b686da6fc640bdef28fa076246ce6cc31bf7e65ef2be7277e84c8b92a21694f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.2-cp312-cp312-win32.whl
  • Upload date:
  • Size: 121.6 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.2-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 ea8da91b195484ede472655a0b9e0b379fe85b73dc951c74eac393241c90f127
MD5 c29e1e1387f91d06b69ff05170caf869
BLAKE2b-256 4b246cb5f056b8fd063aee7a52418c99650278373deb467bcebbd75f7a685605

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 bbcb5e098363c37e2b4485710b935b9a50f7d38952275ed2e955c41d02fa25d9
MD5 0214f280b3180abd3861be185edc30f4
BLAKE2b-256 e0231ce13763597c377af92c07719bf2ba0c171d99029fe26225f16dcc446b9f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 51d742769ea3e0a59eade876c16930ff5e3f78e3d6f31fdfa5fa6ccd8612486b
MD5 e3ce287abdf4ebaf940fe6c0306098b0
BLAKE2b-256 5c26b1ce92d5affbb575b7740ae0a89d8b21cec2eb3adcac5919e1a1eeb27537

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a9be9f1a9cdf53f1835b47a2f9d9275af9563326c769640a9c504712c0a4944
MD5 5ead989a76c1ed14bce574b63a592397
BLAKE2b-256 f053f1e08c5b6957ee965ea1373b01815c52b7a2ab1cd052ae47868c21584b0b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 92cd96c8a35fa6c4fa14c33ec979fc564763cb3c8cca049b54c849b0da568da2
MD5 dc50f930a5fba87d046dd9f8afae29a9
BLAKE2b-256 4aaa38ffc70a9fa942448b91801deba66bfc6ca9e541fb32cf05f825bddae98c

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 129.4 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ce796110048172223a315843157234fad744c9e362fc52a1d3c601a3b676aaea
MD5 a6f661a4a7eb735835f67e3e9bf32807
BLAKE2b-256 1e1cf1542c5a808aece9b67181e5fff4c930dee8dcb6a0190b317c72cba13706

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: ordersim-0.1.2-cp311-cp311-win32.whl
  • Upload date:
  • Size: 121.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.2-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 14cef66139495c77faee18166770ae1b98631146dcd0740c3d35f67aafa1d9f7
MD5 568f627cc8f4971c06b39870560df12e
BLAKE2b-256 b7a23f7a4de2515066b2754144414bde26e71a4f7607fe664d6f2676f1a60d26

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5e37b087313742afde977212c27527e7809414f536dfa7faf16dd4da1b02a014
MD5 7d1d69971004b9b44395bcbf18000216
BLAKE2b-256 399ea0aedb5a268eb69c553f00a7a25b4997feb78b14c529f70a549ea3cf8975

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 15ea5f6b7f4294413cca4b65acad62639b335970ef2a124de02c93bc494a842e
MD5 16efb084e7ddb49a13c6816f601ec4d6
BLAKE2b-256 a3db402dbd6854a52f06dbdc595260eb7dde63aff51591c195d12a36d8c947b5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4777103cdeadcecf1f4d3579a6dd998eeb39562ee17fa81e485dddf97c543099
MD5 a7679fbf1482e0e1a5bdae236fdcc0ce
BLAKE2b-256 a4e38a846b39275d6a3600be227cea6d1ca8a3920c000b180b2756f7186c34a6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for ordersim-0.1.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1ad0f36658bceee1cc693733fb42b5e7528a26fe00e945081db6f1ca9e21f9d8
MD5 e696ccbc0b36d612287fdefc479f94f4
BLAKE2b-256 88a24752c36e8bdf29c5ea769d15c20d8026fb1982d62e6f87c8d2f14a8da1c1

See more details on using hashes here.

Provenance

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