Skip to main content

nautilus_execution/reconciliation/
mod.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//! Execution state reconciliation.
17//!
18//! Pure functions for bringing the engine's local order, fill, and position state
19//! into line with what the venue reports. Called at startup (mass status) and
20//! continuously at runtime (open-order and position checks).
21//!
22//! Public entry points:
23//! - [`process_mass_status_for_reconciliation`] - partial-window fill reconstruction
24//! - [`process_mass_status_for_reconciliation_without_synthetic_reports`] - reconstruction that
25//!   preserves the input report set when adjustment would require synthetic reports
26//! - [`generate_reconciliation_order_events`] - venue-temporal event sequence for a report
27//! - [`reconcile_order_report`] - core order-state reconciliation
28//! - [`reconcile_fill_report`] - apply a venue fill to a cached order, with dedup
29//! - [`generate_external_order_status_events`] - synthesize events for an external order
30//! - [`check_position_reconciliation`] - final qty and avg-px tolerance check
31//!
32//! Invariants maintained across all paths:
33//! 1. When enabled, synthetic adjustment aligns position quantity with the venue within instrument
34//!    precision.
35//! 2. When enabled, synthetic adjustment aligns average price with the venue within tolerance
36//!    (default 0.01%).
37//! 3. All generated fills preserve correct unrealized PnL.
38//! 4. Synthetic `trade_id` and `venue_order_id` values are deterministic
39//!    functions of the logical event, so restart replays dedupe.
40//!
41//! See `docs/concepts/execution/reconciliation.md` for the operator-facing description.
42
43use nautilus_core::correctness::CorrectnessResult;
44use nautilus_model::{
45    instruments::{Instrument, InstrumentAny},
46    types::{Price, fixed::FIXED_PRECISION},
47};
48use rust_decimal::Decimal;
49
50mod ids;
51mod orders;
52mod positions;
53mod types;
54
55#[cfg(test)]
56mod proptests;
57
58#[cfg(test)]
59mod tests;
60
61pub use ids::{
62    create_inferred_reconciliation_trade_id, create_position_reconciliation_venue_order_id,
63    inferred_reconciliation_trade_ids, is_inferred_reconciliation_trade_id_format,
64};
65pub use orders::{
66    create_incremental_inferred_fill, create_inferred_fill_for_qty, create_reconciliation_rejected,
67    create_reconciliation_triggered, generate_external_order_status_events,
68    generate_external_order_status_events_with_commission, generate_reconciliation_order_events,
69    generate_reconciliation_order_pre_fill_events, generate_reconciliation_order_snapshot_events,
70    generate_reconciliation_order_snapshot_events_with_commission,
71    incremental_inferred_fill_price_and_liquidity, inferred_fill_price_and_liquidity,
72    reconcile_fill_report, reconcile_order_report, reconcile_order_report_with_commission,
73    should_reconciliation_update,
74};
75pub use positions::{
76    calculate_reconciliation_price, check_position_reconciliation,
77    fill_precedes_snapshot_reconciled_position, position_prices_match,
78    process_mass_status_for_reconciliation,
79    process_mass_status_for_reconciliation_without_synthetic_reports,
80};
81pub use types::ReconciliationResult;
82
83/// Order tag marking a synthetic order that reconciliation creates to align a position.
84pub const RECONCILIATION_ORDER_TAG: &str = "RECONCILIATION";
85
86fn reconciliation_price(value: Decimal, instrument: &InstrumentAny) -> CorrectnessResult<Price> {
87    // Reconstructed averages need not lie on price ticks, keep their representable precision
88    let precision = instrument
89        .price_precision()
90        .max(value.normalize().scale().min(u32::from(FIXED_PRECISION)) as u8);
91    Price::from_decimal_dp(value, precision)
92}