Skip to main content

Embeddable pre-trade risk SDK

Project description

Pit (Pre-trade Integrity Toolkit) for Python

Verify Release Python versions PyPI License

openpit is an embeddable pre-trade risk SDK for integrating policy-driven risk checks into trading systems from Python.

For full project documentation, see the repository README. For conceptual and architectural pages, see the project wiki.

Versioning Policy (Pre‑1.0)

Until Pit reaches a stable 1.0 release, the project follows a relaxed interpretation of Semantic Versioning.

During this phase:

  • PATCH releases are used for bug fixes and small internal corrections.
  • MINOR releases may introduce new features and may also change the public interface.

This means that breaking API changes can appear in minor releases before 1.0. Consumers of the library should take this into account when declaring dependencies and consider using version constraints that tolerate API evolution during the pre‑stable phase.

Getting Started

Visit the PyPI package.

Install

For normal end-user installation, use the published PyPI package:

pip install openpit

If you need local development/debugging, clone this repository and build from source with Maturin:

maturin develop --manifest-path bindings/python/Cargo.toml

Local release build:

maturin develop --release --manifest-path bindings/python/Cargo.toml

Engine

Overview

The engine evaluates an order through a deterministic pre-trade pipeline:

  • engine.start_pre_trade(order=...) runs start-stage policies and makes lightweight check policies
  • request.execute() runs main-stage check policies
  • reservation.commit() applies reserved state
  • reservation.rollback() reverts reserved state
  • engine.apply_execution_report(report=...) updates post-trade policy state

Start-stage policies stop on the first reject. Main-stage policies aggregate rejects and run rollback mutations in reverse order when any reject is produced.

Built-in policies currently include:

  • OrderValidationPolicy
  • PnlKillSwitchPolicy
  • RateLimitPolicy
  • OrderSizeLimitPolicy

There are two types of rejections: a full kill switch for the account and a rejection of only the current request. This is useful in algorithmic trading when automatic order submission must be halted until the situation is analyzed.

Usage

import openpit

# 1. Configure policies.
pnl = openpit.pretrade.policies.PnlKillSwitchPolicy(
    settlement_asset="USD",
    barrier="1000",
)

rate_limit = openpit.pretrade.policies.RateLimitPolicy(
    max_orders=100,
    window_seconds=1,
)

size = openpit.pretrade.policies.OrderSizeLimitPolicy(
    limit=openpit.pretrade.policies.OrderSizeLimit(
        settlement_asset="USD",
        max_quantity="500",
        max_notional="100000",
    ),
)

# 2. Build the engine (one time at the platform initialization).
engine = (
    openpit.Engine.builder()
    .check_pre_trade_start_policy(
        policy=openpit.pretrade.policies.OrderValidationPolicy(),
    )
    .check_pre_trade_start_policy(policy=pnl)
    .check_pre_trade_start_policy(policy=rate_limit)
    .check_pre_trade_start_policy(policy=size)
    .build()
)

# 3. Check an order.
order = openpit.Order(
    underlying_asset="AAPL",
    settlement_asset="USD",
    side="buy",
    quantity=100.0,
    price=185.0,
)

start_result = engine.start_pre_trade(order=order)

if not start_result:
    reject = start_result.reject
    raise RuntimeError(
        f"{reject.policy} [{reject.code}]: {reject.reason}: {reject.details}"
    )

request = start_result.request

# 4. Quick, lightweight checks, such as fat-finger scope or enabled kill
# switch, were performed during pre-trade request creation. The system state
# has not yet changed, except in cases where each request, even rejected ones,
# must be considered. Before the heavy-duty checks, other work on the request
# can be performed simply by holding the request object.

# 5. Real pre-trade and risk control.
execute_result = request.execute()

if not execute_result:
    messages = ", ".join(
        f"{reject.policy} [{reject.code}]: {reject.reason}: {reject.details}"
        for reject in execute_result.rejects
    )
    raise RuntimeError(messages)

reservation = execute_result.reservation

# 6. If the request is successfully sent to the venue, it must be committed.
# The rollback must be called otherwise to revert all performed reservations.
try:
    send_order_to_venue(order)
except Exception:
    reservation.rollback()
    raise

reservation.commit()

# 7. The order goes to the venue and returns with an execution report.
report = openpit.pretrade.ExecutionReport(
    underlying_asset="AAPL",
    settlement_asset="USD",
    pnl=-50.0,
    fee=3.4,
)

result = engine.apply_execution_report(report=report)

# 8. After each execution report is applied, the system may report that it has
# been determined in advance that all subsequent requests will be rejected if
# the account status does not change.
assert result.kill_switch_triggered is False

Errors

Policy rejects from engine.start_pre_trade() and request.execute() are returned as StartPreTradeResult and ExecuteResult.

Input validation errors and API misuse still raise exceptions:

  • ValueError for invalid assets/sides/malformed numeric inputs
  • RuntimeError for lifecycle misuse, for example executing the same request twice or finalizing the same reservation twice

Local Testing

Recommended local flow:

maturin develop --manifest-path bindings/python/Cargo.toml
python -m pytest bindings/python/tests

Run only unit tests:

maturin develop --manifest-path bindings/python/Cargo.toml
python -m pytest bindings/python/tests/unit

Run only integration test:

maturin develop --manifest-path bindings/python/Cargo.toml
python -m pytest bindings/python/tests/integration

For full build/test command matrix (manual and just), see the repository README.

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

openpit-0.1.3.tar.gz (74.6 kB view details)

Uploaded Source

Built Distributions

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

openpit-0.1.3-cp39-abi3-win_amd64.whl (229.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

openpit-0.1.3-cp39-abi3-musllinux_1_2_x86_64.whl (341.4 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

openpit-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (342.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

openpit-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (330.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

openpit-0.1.3-cp39-abi3-macosx_11_0_arm64.whl (290.3 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

openpit-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl (308.6 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: openpit-0.1.3.tar.gz
  • Upload date:
  • Size: 74.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for openpit-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3555316e760eac1cdefcf94c4fc87c7cb4803abc39ae483c1546bc77008f3c30
MD5 444487f3c2e75cdbeb88d24898ec9e09
BLAKE2b-256 91052a621e462e5de3a8e05b042e8da58ab449c171522aae202e1808d906b4ce

See more details on using hashes here.

File details

Details for the file openpit-0.1.3-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: openpit-0.1.3-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 229.2 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for openpit-0.1.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 153767c3f84ce796f561729007d2b7429fc26becc0a8368c61b4e468c4c59132
MD5 f4708f846265655307eccc973dd2920d
BLAKE2b-256 0483b3c7a643be8a4e62c01bdc0f2f00d92ba6bfb3ee3d6b78d6cde9dfa15e6c

See more details on using hashes here.

File details

Details for the file openpit-0.1.3-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for openpit-0.1.3-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 20ce26b4a6cb76a0aafd702c94a5b9c351aeea61d0c4824b22ec34a5b419f0b5
MD5 f01705c424e74b3ae0ab6bb51e36ec7a
BLAKE2b-256 6e3e45150dca06b641f0aad08e3dc0a661ee52a43a978ffe3ae891ea4a988e6c

See more details on using hashes here.

File details

Details for the file openpit-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for openpit-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69eef35a3eaecd3265ade76d2b6e91af102f8d0ba453dcb69e1c2a2b69d83f98
MD5 99856c714e0ab64452c5c03abaee0e01
BLAKE2b-256 ade37116f58375f5b55d582f9bd720b8fba0936b3527fca41164979ac31a9385

See more details on using hashes here.

File details

Details for the file openpit-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for openpit-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 89647837cef052c29afe2f211cf13971d34faa3ac582aeb3ff45490f0914f076
MD5 31d3d9a32a8767a113471c5f97c5bb7b
BLAKE2b-256 6b3f0cd699a293fb2501bce3807ab348b3df4213bf0470d6955052233da959fe

See more details on using hashes here.

File details

Details for the file openpit-0.1.3-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for openpit-0.1.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ae07a74126ee815752a2ad6abe016f64efd26fe118a351c2c21fe323cb90ab1
MD5 4cefc906bcdf95e6503488cb97155b65
BLAKE2b-256 cfa3a4ab5391484d66391a84d1b4827f990a772d741e3a4c9252d480d676dd82

See more details on using hashes here.

File details

Details for the file openpit-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for openpit-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 935dc4773fad3f5d1a2ba702655be1b073cfa09aef994af66929096b89d51c19
MD5 50bcb1fb5d9c5e47a90702d363ca58a3
BLAKE2b-256 4a5ca3cf546a87b674451c2404d4b15060c4f16eace6acccc8c8cb03382f2236

See more details on using hashes here.

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