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 position's current
45/// NETTING cycle, so the rebuild spans earlier cycles and the archive frames describing them no
46/// longer match the corrected history. `closed_cycles_pnl` then holds the realized PnL of
47/// whatever cycles the corrected history does close before the current one, which settles those
48/// frames. It is `None` when the corrected history never goes flat, leaving no archived cycle.
49///
50/// Known limitation: the flag is decided from quantities, so a second correction to the same
51/// trade that revises only the voided commission leaves it unset, and the settled frame keeps
52/// the realized PnL banked by the first. No in-tree emitter produces that shape, since
53/// reconciliation always advances the quantity and the adapters void a fill once.
54pub(super) struct CorrectedPosition {
55 pub(super) position: Position,
56 pub(super) corrected_qty: Quantity,
57 pub(super) absorbed_prior_cycles: bool,
58 pub(super) closed_cycles_pnl: Option<Money>,
59}