nautilus_model/accounts/
mod.rs1#[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
36pub 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 fn apply(&mut self, event: AccountState) -> anyhow::Result<()>;
82 fn purge_account_events(&mut self, ts_now: UnixNanos, lookback_secs: u64);
83
84 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 fn calculate_pnls(
104 &self,
105 instrument: &InstrumentAny,
106 fill: &OrderFilled,
107 position: Option<Position>,
108 ) -> anyhow::Result<Vec<Money>>;
109
110 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}