Skip to main content

nautilus_model/types/
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//! Value types for the trading domain model.
17//!
18//! This module provides immutable value types that represent fundamental trading concepts:
19//! [`Price`], [`Quantity`], and [`Money`]. These types use fixed-point arithmetic internally
20//! for deterministic calculations while providing a natural numeric interface.
21//!
22//! # Immutability
23//!
24//! All value types are **immutable** - once constructed, their values cannot change.
25//! Arithmetic operations return new instances rather than modifying existing ones.
26//! This design ensures thread safety and predictable behavior in concurrent trading systems.
27//!
28//! # Arithmetic operations
29//!
30//! Value types implement Rust's standard arithmetic traits (`Add`, `Sub`, `Mul`) for
31//! same-type operations. When operating on two values of the same type, the result
32//! preserves that type.
33//!
34//! | Operation             | Result     | Notes                                     |
35//! |-----------------------|------------|-------------------------------------------|
36//! | `Quantity + Quantity` | `Quantity` | Precision is max of both operands.        |
37//! | `Quantity - Quantity` | `Quantity` | Panics if result would be negative.       |
38//! | `Price + Price`       | `Price`    | Precision is max of both operands.        |
39//! | `Price - Price`       | `Price`    | Precision is max of both operands.        |
40//! | `Money + Money`       | `Money`    | Panics if currencies don't match.         |
41//! | `Money - Money`       | `Money`    | Panics if currencies don't match.         |
42//!
43//! For Python bindings with mixed-type operations (e.g., `Quantity + int`), see the
44//! Python API documentation.
45//!
46//! # Precision
47//!
48//! Each value type stores a precision field indicating the number of decimal places.
49//! The maximum precision is defined by [`fixed::FIXED_PRECISION`]. When performing
50//! arithmetic between values with different precisions, the result uses the maximum
51//! precision of the operands.
52//!
53//! # Constraints
54//!
55//! - [`Quantity`]: Non-negative values only. Subtracting a larger quantity from a smaller
56//!   one raises an error rather than producing a negative result.
57//! - [`Price`]: Signed values allowed (can represent negative prices for spreads, etc.).
58//! - [`Money`]: Signed values allowed. Operations between different currencies raise an error.
59
60pub mod balance;
61pub mod currency;
62pub mod fixed;
63pub mod money;
64pub mod price;
65pub mod quantity;
66
67#[cfg(any(test, feature = "stubs"))]
68pub mod stubs;
69
70// Re-exports
71pub use balance::{AccountBalance, MarginBalance};
72pub use currency::Currency;
73pub use money::{MONEY_MAX, MONEY_MIN, Money};
74pub use price::{
75    ERROR_PRICE, PRICE_ERROR, PRICE_MAX, PRICE_MIN, PRICE_RAW_MAX, PRICE_RAW_MIN, PRICE_UNDEF,
76    Price,
77};
78pub use quantity::{QUANTITY_MAX, QUANTITY_MIN, QUANTITY_UNDEF, Quantity};