nautilus_model/accounts/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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    fn apply(&mut self, event: AccountState);
65    fn purge_account_events(&mut self, ts_now: UnixNanos, lookback_secs: u64);
66
67    /// Calculates locked balance for the order parameters.
68    ///
69    /// # Errors
70    ///
71    /// Returns an error if calculating locked balance fails.
72    fn calculate_balance_locked(
73        &mut self,
74        instrument: InstrumentAny,
75        side: OrderSide,
76        quantity: Quantity,
77        price: Price,
78        use_quote_for_inverse: Option<bool>,
79    ) -> anyhow::Result<Money>;
80
81    /// Calculates PnLs for the fill and position.
82    ///
83    /// # Errors
84    ///
85    /// Returns an error if calculating PnLs fails.
86    fn calculate_pnls(
87        &self,
88        instrument: InstrumentAny,
89        fill: OrderFilled,
90        position: Option<Position>,
91    ) -> anyhow::Result<Vec<Money>>;
92
93    /// Calculates commission for the order fill parameters.
94    ///
95    /// # Errors
96    ///
97    /// Returns an error if calculating commission fails.
98    fn calculate_commission(
99        &self,
100        instrument: InstrumentAny,
101        last_qty: Quantity,
102        last_px: Price,
103        liquidity_side: LiquiditySide,
104        use_quote_for_inverse: Option<bool>,
105    ) -> anyhow::Result<Money>;
106}