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