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/reconciliation.md` for the operator-facing description.
42
43mod ids;
44mod orders;
45mod positions;
46mod types;
47
48#[cfg(test)]
49mod proptests;
50#[cfg(test)]
51mod tests;
52
53pub use ids::{
54 create_inferred_reconciliation_trade_id, create_position_reconciliation_venue_order_id,
55};
56pub use orders::{
57 create_incremental_inferred_fill, create_inferred_fill_for_qty, create_reconciliation_rejected,
58 create_reconciliation_triggered, generate_external_order_status_events,
59 generate_external_order_status_events_with_commission, generate_reconciliation_order_events,
60 generate_reconciliation_order_pre_fill_events, generate_reconciliation_order_snapshot_events,
61 generate_reconciliation_order_snapshot_events_with_commission,
62 incremental_inferred_fill_price_and_liquidity, inferred_fill_price_and_liquidity,
63 reconcile_fill_report, reconcile_order_report, reconcile_order_report_with_commission,
64 should_reconciliation_update,
65};
66pub use positions::{
67 calculate_reconciliation_price, check_position_reconciliation,
68 process_mass_status_for_reconciliation,
69 process_mass_status_for_reconciliation_without_synthetic_reports,
70};
71pub use types::ReconciliationResult;