Skip to main content

nautilus_execution/engine/
position.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Position types the [`ExecutionEngine`](super::ExecutionEngine) publishes or carries between
17//! the stages of a fill correction.
18
19use std::rc::Rc;
20
21use nautilus_common::cache::CacheSnapshotRef;
22use nautilus_core::UnixNanos;
23use nautilus_model::{
24    position::Position,
25    types::{Money, Quantity},
26};
27
28/// Position state snapshot published to the `snapshots.position.{position_id}` topic.
29#[derive(Debug, Clone)]
30pub struct PositionStateSnapshot {
31    /// The position state at the time of the snapshot.
32    pub position: Position,
33    /// The unrealized PnL for the position, when a current quote is available.
34    pub unrealized_pnl: Option<Money>,
35    /// UNIX timestamp (nanoseconds) when the snapshot was taken.
36    pub ts_snapshot: UnixNanos,
37}
38
39/// Callback that anchors cache snapshot metadata in an external store.
40pub type SnapshotAnchorer = Rc<dyn Fn(CacheSnapshotRef) -> anyhow::Result<()>>;
41
42/// A position rebuilt by a fill void, with the quantity that correction removed.
43///
44/// `absorbed_prior_cycles` is set when the voided fill sits outside the current NETTING cycle
45/// or, with replay retention enabled, the rebuild rolls back to fills from an earlier cycle.
46/// The archive frames then no longer match the corrected history. `closed_cycles_pnl` holds the PnL of
47/// whatever cycles the corrected history does close before the current one, which settles those
48/// frames. It is `None` when no closed cycle precedes the current one, including when the
49/// corrected history never goes flat or its only closed cycle is the current one.
50///
51/// Known limitation: a second correction to the same trade that revises only the voided
52/// commission without changing cycle membership leaves the flag unset, and the settled frame keeps
53/// the realized PnL banked by the first. No in-tree emitter produces that shape, since
54/// reconciliation always advances the quantity and the adapters void a fill once.
55pub(super) struct CorrectedPosition {
56    pub(super) position: Position,
57    pub(super) corrected_qty: Quantity,
58    pub(super) absorbed_prior_cycles: bool,
59    pub(super) closed_cycles_pnl: Option<Money>,
60}
61
62pub(super) fn requires_snapshot_settlement(
63    prior: &Position,
64    rebuilt: &Position,
65    voided_qty: Quantity,
66    current_cycle_qty: Quantity,
67    carry_replay_events_on_reopen: bool,
68) -> bool {
69    voided_qty > current_cycle_qty
70        || (carry_replay_events_on_reopen
71            && rebuilt.events.first().is_some_and(|opening| {
72                !prior
73                    .events
74                    .iter()
75                    .any(|fill| fill.event_id == opening.event_id)
76            }))
77}