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    identifiers::AccountId,
45    instruments::InstrumentAny,
46    position::Position,
47    types::{AccountBalance, Currency, Money, Price, Quantity},
48};
49
50#[enum_dispatch]
51pub trait Account: 'static + Send {
52    fn id(&self) -> AccountId;
53    fn account_type(&self) -> AccountType;
54    fn base_currency(&self) -> Option<Currency>;
55    fn is_cash_account(&self) -> bool;
56    fn is_margin_account(&self) -> bool;
57    fn calculated_account_state(&self) -> bool;
58    fn balance_total(&self, currency: Option<Currency>) -> Option<Money>;
59    fn balances_total(&self) -> IndexMap<Currency, Money>;
60    fn balance_free(&self, currency: Option<Currency>) -> Option<Money>;
61    fn balances_free(&self) -> IndexMap<Currency, Money>;
62    fn balance_locked(&self, currency: Option<Currency>) -> Option<Money>;
63    fn balances_locked(&self) -> IndexMap<Currency, Money>;
64    fn balance(&self, currency: Option<Currency>) -> Option<&AccountBalance>;
65    fn last_event(&self) -> Option<AccountState>;
66    fn events(&self) -> Vec<AccountState>;
67    fn event_count(&self) -> usize;
68    fn currencies(&self) -> Vec<Currency>;
69    fn starting_balances(&self) -> IndexMap<Currency, Money>;
70    fn balances(&self) -> IndexMap<Currency, AccountBalance>;
71    /// Applies an account state event to update the account.
72    ///
73    /// Implementations reject the event before mutating any state, so a rejected event leaves
74    /// the account unchanged.
75    ///
76    /// # Errors
77    ///
78    /// Returns an error if `event.account_id` does not match this account's ID, or if the
79    /// account state cannot be applied (e.g., negative balance when borrowing is not allowed
80    /// for a cash account).
81    fn apply(&mut self, event: AccountState) -> anyhow::Result<()>;
82    fn purge_account_events(&mut self, ts_now: UnixNanos, lookback_secs: u64);
83
84    /// Calculates locked balance for the order parameters.
85    ///
86    /// # Errors
87    ///
88    /// Returns an error if calculating locked balance fails.
89    fn calculate_balance_locked(
90        &self,
91        instrument: &InstrumentAny,
92        side: OrderSide,
93        quantity: Quantity,
94        price: Price,
95        use_quote_for_inverse: Option<bool>,
96    ) -> anyhow::Result<Money>;
97
98    /// Calculates PnLs for the fill and position.
99    ///
100    /// # Errors
101    ///
102    /// Returns an error if calculating PnLs fails.
103    fn calculate_pnls(
104        &self,
105        instrument: &InstrumentAny,
106        fill: &OrderFilled,
107        position: Option<Position>,
108    ) -> anyhow::Result<Vec<Money>>;
109
110    /// Calculates commission for the order fill parameters.
111    ///
112    /// # Errors
113    ///
114    /// Returns an error if calculating commission fails.
115    fn calculate_commission(
116        &self,
117        instrument: &InstrumentAny,
118        last_qty: Quantity,
119        last_px: Price,
120        liquidity_side: LiquiditySide,
121        use_quote_for_inverse: Option<bool>,
122    ) -> anyhow::Result<Money>;
123}