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

PositionClosed

PositionClosed represents a position having been closed. The ExecutionEngine emits it when a fill flattens the position (see From fill to position). Handler: on_position_closed.

Fields

PositionClosed shares the position event field set. See Position event fields for the full matrix across the three events. The fields that distinguish PositionClosed:

FieldPython typeDescription
closing_order_idClientOrderIdThe client order ID that closed the position.
avg_px_closefloatThe average close price.
realized_returnfloatThe realized return for the position.
realized_pnlMoneyThe final realized PnL for the position.
duration_nsintThe total open duration (nanoseconds).
ts_closedintUNIX timestamp (nanoseconds) when the position closed.

On close, side is FLAT and unrealized_pnl is zero.

Example

Reading the event in a strategy handler:

def on_position_closed(self, event: PositionClosed) -> None:
    self.log.info(
        f"Closed {event.instrument_id}: realized={event.realized_pnl} "
        f"return={event.realized_return}",
    )
  • Events - Event categories, dispatch, and the fill-to-position chain.
  • Positions - Position lifecycle, aggregation, and PnL.
  • Orders - Orders whose fills open and close positions.

On this page