Skip to main content

nautilus_model/accounts/
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//! Account types such as `CashAccount` and `MarginAccount`.
17
18pub mod any;
19pub mod base;
20pub mod cash;
21pub mod margin;
22
23#[cfg(any(test, feature = "stubs"))]
24pub mod stubs;
25
26use ahash::AHashMap;
27use enum_dispatch::enum_dispatch;
28use nautilus_core::UnixNanos;
29
30// Re-exports
31pub use crate::accounts::{
32    any::AccountAny, base::BaseAccount, cash::CashAccount, margin::MarginAccount,
33};
34use crate::{
35    enums::{AccountType, LiquiditySide, OrderSide},
36    events::{AccountState, OrderFilled},
37    identifiers::AccountId,
38    instruments::InstrumentAny,
39    position::Position,
40    types::{AccountBalance, Currency, Money, Price, Quantity},
41};
42
43#[enum_dispatch]
44pub trait Account: 'static + Send {
45    fn id(&self) -> AccountId;
46    fn account_type(&self) -> AccountType;
47    fn base_currency(&self) -> Option<Currency>;
48    fn is_cash_account(&self) -> bool;
49    fn is_margin_account(&self) -> bool;
50    fn calculated_account_state(&self) -> bool;
51    fn balance_total(&self, currency: Option<Currency>) -> Option<Money>;
52    fn balances_total(&self) -> AHashMap<Currency, Money>;
53    fn balance_free(&self, currency: Option<Currency>) -> Option<Money>;
54    fn balances_free(&self) -> AHashMap<Currency, Money>;
55    fn balance_locked(&self, currency: Option<Currency>) -> Option<Money>;
56    fn balances_locked(&self) -> AHashMap<Currency, Money>;
57    fn balance(&self, currency: Option<Currency>) -> Option<&AccountBalance>;
58    fn last_event(&self) -> Option<AccountState>;
59    fn events(&self) -> Vec<AccountState>;
60    fn event_count(&self) -> usize;
61    fn currencies(&self) -> Vec<Currency>;
62    fn starting_balances(&self) -> AHashMap<Currency, Money>;
63    fn balances(&self) -> AHashMap<Currency, AccountBalance>;
64    /// Applies an account state event to update the account.
65    ///
66    /// # Errors
67    ///
68    /// Returns an error if the account state cannot be applied (e.g., negative balance
69    /// when borrowing is not allowed for a cash account).
70    fn apply(&mut self, event: AccountState) -> anyhow::Result<()>;
71    fn purge_account_events(&mut self, ts_now: UnixNanos, lookback_secs: u64);
72
73    /// Calculates locked balance for the order parameters.
74    ///
75    /// # Errors
76    ///
77    /// Returns an error if calculating locked balance fails.
78    fn calculate_balance_locked(
79        &mut self,
80        instrument: InstrumentAny,
81        side: OrderSide,
82        quantity: Quantity,
83        price: Price,
84        use_quote_for_inverse: Option<bool>,
85    ) -> anyhow::Result<Money>;
86
87    /// Calculates PnLs for the fill and position.
88    ///
89    /// # Errors
90    ///
91    /// Returns an error if calculating PnLs fails.
92    fn calculate_pnls(
93        &self,
94        instrument: InstrumentAny,
95        fill: OrderFilled,
96        position: Option<Position>,
97    ) -> anyhow::Result<Vec<Money>>;
98
99    /// Calculates commission for the order fill parameters.
100    ///
101    /// # Errors
102    ///
103    /// Returns an error if calculating commission fails.
104    fn calculate_commission(
105        &self,
106        instrument: InstrumentAny,
107        last_qty: Quantity,
108        last_px: Price,
109        liquidity_side: LiquiditySide,
110        use_quote_for_inverse: Option<bool>,
111    ) -> anyhow::Result<Money>;
112}