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