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

PositionClosed

PositionClosed records the final snapshot of a position. The ExecutionEngine emits it when a fill flattens the position or a fill correction leaves the corrected position closed. See From fill to position. Handler: on_position_closed.

Fields

See Position event fields for the complete field matrix. The fields that describe the close and final result are:

FieldPython typeDescription
closing_order_idClientOrderId or NoneThe client order ID that closed the position, if available.
peak_quantityQuantityThe largest directional quantity reached by the position.
peak_qtyQuantityCompatibility alias for peak_quantity.
avg_px_closefloat or NoneThe average close price, if available.
realized_returnfloatThe final realized return for the position.
realized_pnlMoney or NoneThe final realized PnL, if available.
unrealized_pnlMoneySet to zero by the engine.
durationintThe total open duration in nanoseconds.
ts_openedintUNIX timestamp (nanoseconds) when the position opened.
ts_closedint or NoneUNIX 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