ConceptsEvents
Nightly docs
These docs track unreleased changes and may change without notice. Their code examples can use APIs that the latest release lacks, so run them with a development wheel or switch to the latest release 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:
| Field | Python type | Description |
|---|---|---|
closing_order_id | ClientOrderId or None | The client order ID that closed the position, if available. |
peak_quantity | Quantity | The largest directional quantity reached by the position. |
peak_qty | Quantity | Compatibility alias for peak_quantity. |
avg_px_close | float or None | The average close price, if available. |
realized_return | float | The final realized return for the position. |
realized_pnl | Money or None | The final realized PnL, if available. |
unrealized_pnl | Money | Set to zero by the engine. |
duration | int | The total open duration in nanoseconds. |
ts_opened | int | UNIX timestamp (nanoseconds) when the position opened. |
ts_closed | int or None | UNIX 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}",
)