Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Nautechsystems Nautilus trader Strategy Shutdown

From Leeroopedia
Revision as of 17:57, 16 February 2026 by Admin (talk | contribs) (Auto-imported from principles/Nautechsystems_Nautilus_trader_Strategy_Shutdown.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Field Value
sources https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/
domains algorithmic trading, graceful shutdown, order management, position management, risk management
last_updated 2026-02-10 12:00 GMT

Overview

Strategy shutdown defines the disciplined process of gracefully canceling all open orders and closing all open positions when a trading strategy ceases active participation, ensuring no unmanaged risk remains in the market.

Description

When a trading strategy stops -- whether by user command, end of trading session, system maintenance, or error recovery -- it must not simply halt execution and leave orders and positions unattended. Abandoned open orders can fill at unexpected times, and abandoned open positions expose the account to unmanaged market risk.

Graceful shutdown addresses this by enforcing a structured teardown sequence:

  1. Cancel all open orders for each instrument the strategy manages. This includes:
    • Orders at the venue (open orders).
    • Orders managed by the local order emulator (emulated orders).
    • Orders that are in-flight (submitted but not yet acknowledged).
    • Orders managed by execution algorithms.
  2. Close all open positions for each instrument. This is done by submitting closing market orders opposite to the current position side, for the full remaining quantity.
  3. Verify completion by checking that no residual orders or positions remain after the shutdown commands have been processed.

The shutdown process must handle edge cases:

  • Partial fills during shutdown: An order may fill partially while a cancel is in-flight. The shutdown logic must handle the resulting position.
  • Reject during shutdown: A cancel or close order may itself be rejected. The shutdown logic should retry or log a warning.
  • Timeout: If orders and positions do not reach their final state within a configurable time window, the shutdown should complete with a warning rather than blocking indefinitely.

Usage

Apply graceful shutdown when:

  • Stopping a strategy at end of trading day (flat overnight policy).
  • Decommissioning a strategy permanently.
  • Responding to a risk limit breach that requires immediate position exit.
  • Transitioning from live trading to a maintenance window.
  • Implementing the on_stop lifecycle hook in a strategy.

Theoretical Basis

Strategy shutdown implements a two-phase cleanup protocol similar to distributed transaction cleanup:

Phase 1: Order Cancellation

All outstanding orders are cancelled. This is a fire-and-forget phase where cancel commands are sent for all open, emulated, and in-flight orders. The system does not wait for individual cancel confirmations before proceeding.

Phase 2: Position Closure

After cancellation commands are sent, closing market orders are submitted for all remaining open positions. Market orders are used because they provide the highest probability of execution during shutdown.

FUNCTION graceful_shutdown(strategy, instrument_id):
    // Phase 1: Cancel orders
    open_orders = cache.get_open_orders(instrument_id, strategy_id)
    emulated_orders = cache.get_emulated_orders(instrument_id, strategy_id)
    inflight_orders = cache.get_inflight_orders(instrument_id, strategy_id)

    FOR EACH order IN open_orders + emulated_orders + inflight_orders:
        SEND cancel_order(order)

    // Phase 2: Close positions
    open_positions = cache.get_open_positions(instrument_id, strategy_id)

    FOR EACH position IN open_positions:
        closing_side = opposite_side(position.side)
        closing_order = market_order(
            instrument_id = position.instrument_id,
            side = closing_side,
            quantity = position.quantity,
            reduce_only = True,
        )
        SUBMIT closing_order

    // Phase 3: Verify (optional, with timeout)
    WAIT until no open orders AND no open positions
        OR timeout exceeded
    IF timeout:
        LOG warning "Shutdown incomplete: residual orders or positions remain"

Key invariants:

  • Idempotency: Calling shutdown multiple times is safe. Canceling an already-canceled order is a no-op, and closing an already-closed position is a no-op.
  • Order of operations: Orders are canceled before positions are closed to prevent new fills from reopening positions during shutdown.
  • Reduce-only: Closing orders use the reduce-only flag (where supported) to prevent accidentally opening a new position in the opposite direction.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment