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