openpit is an embeddable pre-trade risk SDK for integrating policy-driven
risk checks into trading systems from Python.
For an overview and links to all resources, see the project website openpit.dev. For the Python API guide and generated reference, see openpit.readthedocs.io. For full project documentation, see the repository README. For conceptual and architectural pages, see the project wiki.
Before the 1.0 release OpenPit follows a relaxed Semantic Versioning:
PATCHreleases carry bug fixes and small internal corrections.MINORreleases may introduce new features and may also change the public interface.
Breaking API changes can appear in minor releases before 1.0. Pick
version constraints that tolerate API evolution during the pre-stable
phase.
Visit the PyPI package.
Runnable end-to-end examples: examples/python/.
For normal end-user installation, use the published PyPI package:
pip install openpitIf you need local development/debugging, clone this repository and build from source with Maturin:
maturin develop --manifest-path bindings/python/Cargo.tomlLocal release build:
maturin develop --release --manifest-path bindings/python/Cargo.tomlThe engine evaluates an order through a deterministic pre-trade pipeline:
engine.start_pre_trade(order=...)runs lightweight start-stage checksrequest.execute()runs main-stage check policiesreservation.commit()applies reserved statereservation.rollback()reverts reserved stateengine.apply_execution_report(report=...)updates post-trade policy state
Start-stage checks aggregate rejects from all registered policies. Main-stage checks aggregate rejects and run rollback mutations in reverse order when any reject is produced.
Built-in policies:
OrderValidationPolicyPnlBoundsKillSwitchPolicyRateLimitPolicyOrderSizeLimitPolicy
The primary integration model is to write project-specific policies against the public Python policy API: Custom Python policies.
Two types of rejections are supported: a full kill switch for the account and a rejection of only the current request. Kill switches are intended for algorithmic trading where automatic order submission must be halted until the situation is analyzed.
Canonical contract: Threading Contract.
Public methods acquire the GIL when needed; the SDK does not release the GIL across callback boundaries, so Python policies execute on the calling thread.
Custom policies that need internal state across calls can use the built-in Storage abstraction. In typical Python usage (synchronous code or an asyncio loop pinned to one thread) the no-sync policy is sufficient and the storage compiles down to direct dictionary access. A synchronizing policy is needed only when the engine is genuinely shared across OS threads.
import datetime
import openpit
import openpit.pretrade.policies
# 1. Configure policies.
pnl_policy = (
openpit.pretrade.policies.build_pnl_bounds_killswitch()
.broker_barriers(
openpit.pretrade.policies.PnlBoundsBrokerBarrier(
settlement_asset="USD",
lower_bound=openpit.param.Pnl("-1000"),
),
)
)
rate_limit_policy = (
openpit.pretrade.policies.build_rate_limit()
.broker_barrier(
openpit.pretrade.policies.RateLimitBrokerBarrier(
limit=openpit.pretrade.policies.RateLimit(
max_orders=100,
window=datetime.timedelta(seconds=1),
),
),
)
)
order_size_policy = (
openpit.pretrade.policies.build_order_size_limit()
.asset_barriers(
openpit.pretrade.policies.OrderSizeAssetBarrier(
limit=openpit.pretrade.policies.OrderSizeLimit(
max_quantity=openpit.param.Quantity("500"),
max_notional=openpit.param.Volume("100000"),
),
settlement_asset="USD",
),
)
)
# 2. Build the engine (one time at the platform initialization).
engine = (
openpit.Engine.builder()
.no_sync()
.builtin(openpit.pretrade.policies.build_order_validation())
.builtin(pnl_policy)
.builtin(rate_limit_policy)
.builtin(order_size_policy)
.build()
)
# 3. Check an order.
order = openpit.Order(
operation=openpit.OrderOperation(
instrument=openpit.Instrument("AAPL", "USD"),
account_id=openpit.param.AccountId.from_u64(99224416),
side=openpit.param.Side.BUY,
trade_amount=openpit.param.TradeAmount.quantity(100.0),
price=openpit.param.Price(185.0),
),
)
start_result = engine.start_pre_trade(order=order)
if not start_result:
messages = ", ".join(
f"{r.policy} [{r.code}]: {r.reason}: {r.details}"
for r in start_result.rejects
)
raise RuntimeError(messages)
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()
# Optional shortcut for the same two-stage flow:
# execute_result = engine.execute_pre_trade(order=order)
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.ExecutionReport(
operation=openpit.ExecutionReportOperation(
instrument=openpit.Instrument("AAPL", "USD"),
account_id=openpit.param.AccountId.from_u64(99224416),
side=openpit.param.Side.BUY,
),
financial_impact=openpit.FinancialImpact(
pnl=openpit.param.Pnl("-50"),
fee=openpit.param.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 not result.account_blocksPolicy rejects from engine.start_pre_trade() and request.execute() are
returned as StartResult and ExecuteResult.
Input validation errors and API misuse still raise exceptions:
ValueErrorfor invalid assets/sides/malformed numeric inputsRuntimeErrorfor lifecycle misuse, for example executing the same request twice or finalizing the same reservation twice- Business rejects use stable reject codes such as
openpit.pretrade.RejectCode.ORDER_VALUE_CALCULATION_FAILEDwhen a policy cannot evaluate order value withoutprice
Recommended local flow:
maturin develop --manifest-path bindings/python/Cargo.toml
python -m pytest bindings/python/testsRun only unit tests:
maturin develop --manifest-path bindings/python/Cargo.toml
python -m pytest bindings/python/tests/unitRun only integration test:
maturin develop --manifest-path bindings/python/Cargo.toml
python -m pytest bindings/python/tests/integrationFor full build/test command matrix (manual and just), see
the repository README.