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