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(feature = "stubs")]
24pub mod stubs;
25
26use std::collections::HashMap;
27
28use enum_dispatch::enum_dispatch;
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) -> HashMap<Currency, Money>;
53    fn balance_free(&self, currency: Option<Currency>) -> Option<Money>;
54    fn balances_free(&self) -> HashMap<Currency, Money>;
55    fn balance_locked(&self, currency: Option<Currency>) -> Option<Money>;
56    fn balances_locked(&self) -> HashMap<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) -> HashMap<Currency, Money>;
63    fn balances(&self) -> HashMap<Currency, AccountBalance>;
64    fn apply(&mut self, event: AccountState);
65    fn calculate_balance_locked(
66        &mut self,
67        instrument: InstrumentAny,
68        side: OrderSide,
69        quantity: Quantity,
70        price: Price,
71        use_quote_for_inverse: Option<bool>,
72    ) -> anyhow::Result<Money>;
73    fn calculate_pnls(
74        &self,
75        instrument: InstrumentAny,
76        fill: OrderFilled,
77        position: Option<Position>,
78    ) -> anyhow::Result<Vec<Money>>;
79    fn calculate_commission(
80        &self,
81        instrument: InstrumentAny,
82        last_qty: Quantity,
83        last_px: Price,
84        liquidity_side: LiquiditySide,
85        use_quote_for_inverse: Option<bool>,
86    ) -> anyhow::Result<Money>;
87}