Skip to main content

Embeddable pre-trade risk SDK

Project description

Pit (Pre-trade Integrity Toolkit) for Python

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.

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.0.tar.gz (68.3 kB view details)

Uploaded Source

Built Distribution

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

openpit-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (267.8 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: openpit-0.1.0.tar.gz
  • Upload date:
  • Size: 68.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for openpit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3814655a0064892185ecf042de6b0c1fe7148e6b82f8e2f8531ec32ee1ede9ab
MD5 d7deb9253884f7a4a9142ffe0e063fcd
BLAKE2b-256 57198a9e5d0955be9348fe48ac8c7a2b75df7e0fc001bc05c5dce7cf5846c8df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openpit-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2c52782e73214d5c23e21332fa8bdec5fc23c209d2c9f29da821e2f4f0909eba
MD5 cfbf6b576573b10c22bf4bbb922bd6a9
BLAKE2b-256 f2c424d3fac0c309f08369807d519cca5b6e2fefbae59e7ee605a209675be4c5

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