NautilusTrader
ConceptsExecution
These docs track the unreleased nightly build and may change without notice. Switch to the latest stable docs.

Execution

NautilusTrader coordinates order submission, risk checks, venue execution, reconciliation, and position updates across multiple strategies and venues. This page explains the components and message flows that support execution.

Use the execution guides according to the question you need to answer:

QuestionGuide
Which components handle an order command?This page: Execution flow.
Which statuses and transitions can an order have?Orders: order state flow.
Which policies govern venue-boundary execution?Execution policies.
How do execution algorithms split and manage orders?Execution algorithms.
How does live state recover and remain consistent?Execution reconciliation.
How do I configure a live node?Live node configuration.
How does a live node schedule and monitor execution?Live trading.

The main execution-related components include:

  • Strategy
  • ExecutionAlgorithm
  • OrderEmulator
  • RiskEngine
  • ExecutionEngine
  • ExecutionClient

Execution flow

A Strategy builds on data actor capabilities and adds methods for managing orders and execution:

  • submit_order(...)
  • submit_order_list(...)
  • modify_order(...)
  • cancel_order(...)
  • cancel_orders(...)
  • cancel_all_orders(...)
  • close_position(...)
  • close_all_positions(...)
  • query_account(...)
  • query_order(...)

These methods send point-to-point execution commands over the message bus. Order creation also publishes events such as OrderInitialized.

Commands follow different routes:

  • submit_order(...) routes to OrderEmulator for emulated orders, to an ExecutionAlgorithm when exec_algorithm_id is set, and to the RiskEngine otherwise.
  • submit_order_list(...) follows the same branching behavior based on emulation and exec_algorithm_id.
  • modify_order(...) routes to the OrderEmulator for emulated orders, to an ExecutionAlgorithm when the order has an exec_algorithm_id and is still active within the local system, and to the RiskEngine otherwise.
  • Cancel and query commands can route directly to the OrderEmulator, ExecutionAlgorithm, or ExecutionEngine, depending on the command and order state.

New orders typically enter one of these paths:

Strategy -> OrderEmulator or ExecutionAlgorithm or RiskEngine

The downstream flow is:

OrderEmulator -> ExecutionAlgorithm or ExecutionEngine

ExecutionAlgorithm -> RiskEngine -> ExecutionEngine -> ExecutionClient

Execution paths branch by emulation and algorithm routing before reaching the execution engine and client.

Order management system (OMS)

An order management system (OMS) type determines how orders map to positions for an instrument. Strategies and venues, whether simulated or live, each use an OMS type defined by the OmsType enum.

The OmsType enum has three variants:

  • UNSPECIFIED: The strategy uses the venue's OMS type.
  • NETTING: Positions combine into one position per instrument and strategy.
  • HEDGING: Multiple positions per instrument and strategy can remain open.

When the strategy and venue OMS types differ, the ExecutionEngine assigns or overrides position_id values on OrderFilled events. A virtual position exists in NautilusTrader but not as a separate venue position.

Strategy OMSVenue OMSResult
NETTINGNETTINGOne position per instrument and strategy.
HEDGINGHEDGINGMultiple positions per instrument and strategy.
NETTINGHEDGINGOne virtual position across the venue positions.
HEDGINGNETTINGMultiple virtual positions against the venue's single net position.

If a fill resolves to a cached position for a different instrument, the ExecutionEngine logs an error and drops the fill. The order remains non-terminal so a subsequent valid fill can be applied.

OMS configuration

When a strategy omits oms_type or uses UNSPECIFIED, the ExecutionEngine follows the venue's OMS type without overriding venue position_id values. Configure a backtest venue with the OMS type used by the venue being modeled.

Venue position modes may require adapter-specific configuration. For example, see Binance Futures hedge mode.

Custom position IDs and NETTING

Custom position IDs are only valid under HEDGING OMS. NETTING has one position per instrument and strategy, with a deterministic ID of the form {instrument_id}-{strategy_id}.

The ExecutionEngine enforces this at submit time. If the effective OMS resolves to NETTING and submit_order (or submit_order_list) is called with a position_id that does not match {instrument_id}-{strategy_id}, the order is denied with an OrderDenied event explaining the mismatch.

This rule still permits the common closing idiom: Strategy.close_position(position) forwards position.id, which under NETTING is exactly the deterministic ID, so it is accepted. To label or partition positions with arbitrary IDs, configure the strategy with oms_type=HEDGING.

For submit_order_list, the engine additionally denies any mixed-instrument list when a position_id is supplied, regardless of OMS. A position belongs to a single instrument, so the combination is rejected with an explicit OrderDenied reason. See Order lists for the broader set of mixed-instrument caveats.

Position replay across NETTING cycles

Under NETTING the engine reuses one position ID across close and reopen cycles, so a position's replay log can accumulate every fill ever applied to that ID. The ExecutionEngineConfig.carry_replay_events_on_reopen option controls whether that log survives a reopen:

carry_replay_events_on_reopenBehavior
False (default)Keeps only current-cycle state, bounding the per-fill cost.
TrueKeeps earlier fills correctable while position state can grow.

Live trading pins the option True: LiveExecutionEngineConfig always carries the replay log, so a venue OrderFillVoided referencing an earlier cycle still resolves. The simulated venue never emits fill voids, so backtests take the bounded default. Enable it explicitly for a custom or external execution client that can correct a fill from a prior cycle; without the carried log the engine finds no matching position fragment and rejects the correction.

Realized-PnL snapshots follow the correction. A fill void that reaches an earlier cycle rebuilds the position across the cycle boundary, moving the boundaries its archived snapshots describe, so the engine settles those snapshots into the corrected history's own closed cycles and realized PnL counts each cycle once. A void confined to the current cycle leaves the archive intact. See Position snapshotting.

Risk engine

The RiskEngine is a component of every Nautilus system, including backtest, sandbox, and live environments. It sits on the submit and modify path, and it also receives order events such as OrderReleased from the OrderEmulator. Cancel and query commands route directly to other execution components and do not pass through the RiskEngine.

Unless bypassed in RiskEngineConfig, the engine validates:

  • Price and trigger-price precision for the instrument.
  • Positive prices, unless the instrument allows negative prices (options, futures spreads, option spreads, and spot commodities).
  • Quantity precision and base-quantity minimum and maximum bounds.
  • GTD orders have not already expired.
  • reduce_only orders do not increase the referenced position.
  • Engine-level max_notional_per_order limits and the instrument's min_notional and max_notional fields.
  • Cash-account balance impact for non-margin accounts.
  • Submit and modify rate limits.
  • Trading-state restrictions (ACTIVE, HALTED, REDUCING).

If a submit-time risk check fails, the system generates an OrderDenied event with a standardized reason code. If a modify-time risk check fails, it generates an OrderModifyRejected event.

Whole-position conditional exits

Some execution clients support conditional exits whose venue determines the closing quantity from the open position when the trigger fires. Nautilus orders still carry a placeholder quantity for local validation. The full_position_exit_venues setting on RiskEngineConfig and LiveRiskEngineConfig identifies venues whose execution clients enforce these semantics. It defaults to empty.

An order qualifies for the placeholder exemption only when all of these conditions hold:

  • The order is submitted individually, not in an order list.
  • Its venue is listed in full_position_exit_venues.
  • It uses a supported futures or perpetual instrument.
  • It is a StopMarket or MarketIfTouched order with a trigger price and close_position=true.
  • It has a positive placeholder quantity and sets reduce_only=true.
  • The command, order, and linked cached position use the same instrument and position ID.
  • The linked position is open, the order side closes it, and the placeholder quantity does not exceed the position quantity.

For a qualifying exit, the risk engine treats checks as follows:

Risk checkTreatment
Quantity precision and positivityEnforced.
Price and trigger-price precision and positivityEnforced.
GTD expiration, trading-state restrictions, and submission rate limitEnforced.
Position exposure, margin, and balanceTreated as position-reducing.
Instrument minimum and maximum quantitySkipped for the placeholder quantity.
Instrument minimum and maximum notionalSkipped for the placeholder notional.
Configured max_notional_per_orderSkipped for the placeholder notional.
Non-qualifying ordersAll ordinary risk checks continue to apply.

Only allowlist a venue when its downstream execution client enforces whole-position closing. See Binance Futures close-position orders for a supported configuration.

The simulated exchange does not interpret close_position or replace the placeholder with the open position quantity. Leave simulated backtest venues out of full_position_exit_venues; model a backtest exit with an explicit quantity and reduce_only instead.

Trading state

The states become progressively more restrictive:

StateNumeric valuePermitted commands
ACTIVE1Submit, modify, cancel, and query commands operate normally.
REDUCING2Eligible individual reduce-only submissions, cancels, and queries.
HALTED3Cancels and queries only. New submissions and modifications are not allowed.

In REDUCING, an individual SubmitOrder is eligible only when the order sets reduce_only=true, the command and order identify the same instrument, and the supplied position ID matches the order's cached open position. The order side must oppose the position, and the submitted quantity must not exceed the cached position quantity. Order lists and modifications are denied.

The risk engine applies these rules before forwarding commands to execution.

Bypassing trading-state checks

When RiskEngineConfig.bypass is enabled, trading state is not enforced. Execution clients still follow the reduce-only send-or-reject contract.

See the RiskEngineConfig API reference for configuration details.

Execution algorithms

An ExecutionAlgorithm receives primary orders selected by exec_algorithm_id and can split them into smaller spawned orders. NautilusTrader supports custom algorithms and includes a native Rust TWAP implementation.

See Execution algorithms for TWAP configuration, custom algorithms, spawned-order behavior, and cache queries.

Cancel-all routing

Strategy.cancel_all_orders(...) supports strategy-scoped and broad cancellation:

strategy_onlyStrategy outputScopeDownstream routing
TrueOne CancelOrder per matching order.Matching orders associated with the calling strategy.Each order follows its normal cancel route.
FalseOne root CancelAllOrders, even without local matches.Matching orders for one resolved execution client and account.The execution engine creates the required children.

Broad mode delegates before the strategy inspects its cache. The command therefore reaches the resolved execution client even when NautilusTrader has no matching local order. This allows a venue bulk-cancel endpoint to remove an order that exists at the venue but is missing from the local cache. When the adapter provides such an endpoint, the single command can also reduce cancel request volume.

For a local execution client, the ExecutionEngine resolves the root command to exactly one client in this order:

  1. The explicit client_id, when it identifies a registered local client.
  2. The client registered for the instrument's venue.
  3. The default execution client.

The engine then creates fresh child commands for the selected client and its account:

  • One CancelAllOrders for the execution client, covering matching venue orders.
  • One CancelAllOrders for the OrderEmulator, covering matching emulated orders.
  • One CancelOrder per eligible active-local execution-algorithm order.

Broad mode selects one client before fan-out; it never broadcasts across all execution clients. Call cancel_all_orders(...) once per client to cancel across several clients.

Every child has a new command ID, copies the root parameters, correlates to the root operation, and records the root command as its cause. Instrument and optional side filters apply to every local route. The selected execution account also bounds matching-engine cancellation, including orders in SUBMITTED and other cancelable in-flight states.

Client ownership applies to local emulated and execution-algorithm orders:

  • Orders already assigned to another client remain untouched.
  • When the root omits client_id, matching unassigned orders are claimed by the client selected by the engine before local cancellation.
  • When the root supplies client_id, unassigned orders remain untouched because the engine cannot infer that they belong to the explicit client.
  • An emulated order matches its traded instrument, even when another instrument supplies its trigger.

An explicitly configured external execution client receives the original root command unchanged. The external client owns any fan-out needed behind that boundary.

Command outcomes

Execution commands distinguish definitive local failures, definitive venue results, and unknown live outcomes. An unknown outcome remains in flight for stream updates, polling, queries, or reconciliation. Retry exhaustion can later apply a synthetic terminal reconciliation event.

See Execution policies for the evidence classes, delivery and retry limits, persistence boundary, and terminal reconciliation provenance. See Runtime checks for the continuous reconciliation procedure.

Order denied reasons

A local denial (OrderDenied) carries a standardized CATEGORY_CONDITION reason code and may include a diagnostic suffix. Only the leading code is canonical. Messages use these forms:

  • CODE when the denial needs no diagnostic suffix.
  • CODE: value for one typed value or a free-text diagnostic.
  • CODE: key=value, key=value when multiple typed values need disambiguation.
  • CODE: value; free text when one typed value precedes a free-text diagnostic.

The table covers local denials emitted by execution algorithms and clients as well as the risk and execution engines. These codes are the source of truth for locally denied orders. Venue-confirmed OrderRejected events instead carry the venue-provided meaning, while synthetic reconciliation rejections use the reasons documented under Terminal reconciliation provenance. Adapters remove protocol wrappers and bound untrusted venue text before emission without replacing it with a standardized local denial code.

Price and quantity checks can also emit these code-led reasons on OrderModifyRejected:

  • PRICE_PRECISION_EXCEEDS_MAXIMUM
  • PRICE_NOT_POSITIVE
  • QUANTITY_PRECISION_EXCEEDS_MAXIMUM
  • QUANTITY_EXCEEDS_MAXIMUM
  • QUANTITY_BELOW_MINIMUM

For price reasons, field is PRICE or TRIGGER_PRICE and names the rejected command field. Other modification rejection reasons remain free-form; OrderDeniedCode does not classify them.

OrderRejected.due_post_only is true only when venue evidence proves that a post-only order would cross or immediately match. Other venue rejections leave it false.

CodeDescription
PRICE_PRECISION_EXCEEDS_MAXIMUMThe price precision exceeds the instrument maximum.
PRICE_NOT_POSITIVEThe price is not positive.
QUANTITY_PRECISION_EXCEEDS_MAXIMUMThe quantity precision exceeds the instrument maximum.
QUANTITY_CONVERSION_FAILEDThe order quantity could not be converted for risk checks.
QUANTITY_EXCEEDS_MAXIMUMThe effective order quantity exceeds the instrument maximum.
QUANTITY_BELOW_MINIMUMThe effective order quantity is below the instrument minimum.
INVALID_MAX_NOTIONAL_PER_ORDERThe configured maximum notional per order is invalid.
MISSING_EXPIRE_TIMEA GTD order is missing its expire time.
EXPIRE_TIME_IN_PASTThe order's expire time is in the past.
MISSING_TRAILING_OFFSET_TYPEThe order is missing a required trailing offset type.
UNSUPPORTED_TRAILING_OFFSET_TYPEThe order's trailing offset type is not supported.
MISSING_TRIGGER_TYPEThe order is missing a required trigger type.
MISSING_TRAILING_OFFSETThe order is missing a required trailing offset.
INSTRUMENT_NOT_FOUNDThe instrument was not found in the cache.
POSITION_NOT_FOUNDThe position for a reduce-only order was not found.
MARKET_PRICE_UNAVAILABLENo market price is available for the order risk check.
TRAILING_STOP_CALCULATION_FAILEDThe trailing stop trigger price could not be calculated.
NOTIONAL_CALCULATION_FAILEDThe order notional value could not be calculated.
NOTIONAL_BELOW_MINIMUMThe order notional is below the instrument minimum.
NOTIONAL_EXCEEDS_MAXIMUMThe order notional exceeds the instrument maximum.
NOTIONAL_EXCEEDS_MAX_PER_ORDERThe order notional exceeds the configured maximum per order.
NOTIONAL_EXCEEDS_FREE_BALANCEThe order notional exceeds the account free balance.
INITIAL_MARGIN_CALCULATION_FAILEDThe order initial margin could not be calculated.
INITIAL_MARGIN_EXCEEDS_FREE_BALANCEThe order initial margin exceeds the account free balance.
BETTING_BALANCE_LOCKED_CALCULATION_FAILEDThe balance to lock for the betting order could not be calculated.
CUMULATIVE_NOTIONAL_EXCEEDS_FREE_BALANCEThe cumulative order notional exceeds the account free balance.
CUMULATIVE_INITIAL_MARGIN_CALCULATION_FAILEDThe cumulative initial margin could not be calculated.
CUMULATIVE_INITIAL_MARGIN_EXCEEDS_FREE_BALANCEThe cumulative initial margin exceeds the account free balance.
REDUCE_ONLY_WOULD_INCREASE_POSITIONA reduce-only order would increase the position.
ORDER_LIST_INCOMPLETEThe order list is missing orders in the cache.
ORDER_LIST_DENIEDThe order was denied because its order list failed risk checks.
TRADING_HALTEDTrading is halted; new submissions and modifications are denied.
TRADING_STATE_REDUCINGTrading is reducing; only eligible reduce-only submissions are permitted.
RATE_LIMIT_EXCEEDEDThe order submission rate limit was exceeded.
STREAM_RECONCILINGThe execution stream is unavailable or recovering; retry after recovery.
NO_EXECUTION_CLIENTNo execution client was found for the routed command.
CLIENT_VENUE_MISMATCHThe execution client does not handle the order venue.
SUBMIT_FAILEDSubmitting the order to the execution client failed.
INVALID_CLIENT_ORDER_IDThe client order ID is invalid for the venue.
INVALID_POSITION_IDThe supplied position ID is invalid for the order submission.
UNSUPPORTED_ORDER_LISTThe venue does not support the requested order list.
UNSUPPORTED_ORDER_TYPEThe order type is not supported.
UNSUPPORTED_REDUCE_ONLYThe execution client or venue does not support the requested reduce-only instruction.
UNSUPPORTED_TIME_IN_FORCEThe order's time in force is not supported.
UNSUPPORTED_TP_SLThe venue does not support the requested take-profit/stop-loss parameters.
VALIDATION_FAILEDThe order failed validation before submission.

Own order books

When manage_own_order_books is enabled, the ExecutionEngine maintains a market-by-order (MBO/L3) view of your working orders for each instrument. Strategies can subtract these orders from the public book to estimate net available liquidity. See Own order book for lifecycle, queries, filtering, and auditing.

Safe cancellation queries

When querying an own order book for cancellation candidates, exclude PENDING_CANCEL from the status filter.

Including PENDING_CANCEL can issue duplicate cancel requests and repeatedly select orders that already await confirmation.

Overfills

An overfill occurs when an order's cumulative filled quantity exceeds its original quantity. For example, fills totaling 110 units overfill a 100-unit order by 10 units.

How overfills occur

The engine observes an overfill when reported quantities exceed the order quantity. This can represent a genuine venue result, duplicate delivery under different trade IDs, or inconsistent venue reporting. Quantity alone does not identify the cause.

Live fills can arrive through two channels:

  • Real-time fill events arriving via WebSocket.
  • Periodic reconciliation polling the venue for fill history and position status.

Stable trade_id values let the engine deduplicate the same fill across both channels. If the logical fill arrives with different IDs, the engine treats the reports as distinct. See Continuous reconciliation for configuration details.

System behavior

The ExecutionEngine checks for potential overfills before applying each fill event by comparing the order's current filled_qty plus the incoming last_qty against the original quantity.

The allow_overfills configuration option (default: False) controls how overfills are handled:

allow_overfillsBehavior
FalseLogs and rejects the fill, preserving the order's current state.
TrueLogs a warning, applies the fill, and tracks the excess in overfill_qty.

When overfills are allowed, the order's overfill_qty field tracks the excess quantity. The order transitions to FILLED status and leaves_qty is clamped to zero.

Duplicate fill detection

The Order model enforces one applied fill per trade_id. Order.apply() returns an error when the same ID already exists on the order.

Core engine path

Before applying a fill, the ExecutionEngine calls Order.is_duplicate_fill(), which compares:

  • trade_id
  • order_side
  • last_px
  • last_qty

An exact match is skipped with a warning. If the trade_id matches but another field differs, the four-field check does not classify the fill as an exact duplicate. Order.apply() then rejects the reused ID, and the engine logs and drops the fill.

Reconciliation path

The reconciliation path checks trade_id before generating an OrderFilled event. It drops a report when that ID already exists on the order, regardless of its price or quantity.

Synthetic and inferred reconciliation fills use deterministic IDs. Replaying the same inputs after a restart therefore produces the same trade_id and is deduplicated.

Configuration

For live trading, enable overfill tolerance in the LiveExecutionEngineConfig:

from nautilus_trader.config import LiveExecutionEngineConfig

config = LiveExecutionEngineConfig(
    allow_overfills=True,
)

Choose this setting from the venue's execution contract. The default False protects local state but can leave a discrepancy after a legitimate venue overfill. True applies the excess quantity and is not a substitute for duplicate-fill detection. Use execution reconciliation to detect discrepancies.

Fill corrections

Some venues can later reduce or invalidate a fill. Nautilus records this as an OrderFillVoided event, never as an opposite-side fill. The event identifies the original trade and carries the cumulative voided quantity and fee correction.

The execution engine rebuilds the affected order and positions and refreshes portfolio position and PnL caches before publishing the correction to strategies and execution algorithms. Adapters that support fill corrections request an authoritative account refresh after a void.

Adapters must publish the referenced fill before a reopened correction or a partial correction that leaves the order executable. Without a local fill, a non-reopened correction makes the whole order terminal, even when voided_qty is less than the order quantity. A later working status report does not reopen VOIDED. See the complete OrderFillVoided contract.

How voided fills occur

A void is a venue action on a trade it already reported. The causes recur across asset classes:

  • Erroneous execution review: the venue nullifies a print that is substantially inconsistent with the market at the time of execution, or one caused by an exchange system fault.
  • Settlement failure: a matched trade fails to settle, so the fill never takes economic effect.
  • Event invalidation: the underlying event is abandoned or a competitor is withdrawn, so matched positions carry no exposure.
  • Post-trade restatement: the venue restates the quantity or fees of a trade during clearing.

The event does not restate the fill price, so a venue price adjustment is not expressible as a single correction.

A break reaches the client differently by venue. FIX venues signal one through ExecType <150> values H (trade cancel) and G (trade correct). Venues that notify out of band leave the break to surface through execution reconciliation.

Venue references

Each venue publishes the conditions under which it acts:

VenueMechanismReference
NasdaqClearly erroneous transactions (Rule 11890).Clearly erroneous transactions policy.
NYSEClearly erroneous executions (Rule 7.10).Clearly erroneous execution review.
Cboe US equitiesClearly erroneous executions (BZX Rule 11.17).Clearly erroneous execution form.
CME GroupTrade cancellations and price adjustments (Rule 588).CME rulebook chapter 5.
BetfairVoided bets, reported as cumulative size voided (sv).Void bets on the Stream API.
PolymarketFAILED trade status after an on-chain revert or reorg.User channel.

Nautilus adapters emit OrderFillVoided where the venue publishes the void on a stream the adapter consumes: Betfair from the order change message sv field, and Polymarket from the user channel trade status.

On this page