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 fees::MakerTakerFeeRates,
45 identifiers::AccountId,
46 instruments::InstrumentAny,
47 position::Position,
48 types::{AccountBalance, Currency, Money, Price, Quantity},
49};
50
51#[enum_dispatch]
52pub trait Account: 'static + Send {
53 fn id(&self) -> AccountId;
54 fn account_type(&self) -> AccountType;
55 fn base_currency(&self) -> Option<Currency>;
56 fn is_cash_account(&self) -> bool;
57 fn is_margin_account(&self) -> bool;
58 fn calculated_account_state(&self) -> bool;
59 fn balance_total(&self, currency: Option<Currency>) -> Option<Money>;
60 fn balances_total(&self) -> IndexMap<Currency, Money>;
61 fn balance_free(&self, currency: Option<Currency>) -> Option<Money>;
62 fn balances_free(&self) -> IndexMap<Currency, Money>;
63 fn balance_locked(&self, currency: Option<Currency>) -> Option<Money>;
64 fn balances_locked(&self) -> IndexMap<Currency, Money>;
65 fn balance(&self, currency: Option<Currency>) -> Option<&AccountBalance>;
66 fn last_event(&self) -> Option<AccountState>;
67 fn events(&self) -> Vec<AccountState>;
68 fn event_count(&self) -> usize;
69 fn currencies(&self) -> Vec<Currency>;
70 fn starting_balances(&self) -> IndexMap<Currency, Money>;
71 fn balances(&self) -> IndexMap<Currency, AccountBalance>;
72 fn apply(&mut self, event: AccountState) -> anyhow::Result<()>;
83 fn purge_account_events(&mut self, ts_now: UnixNanos, lookback_secs: u64);
84
85 fn calculate_balance_locked(
91 &self,
92 instrument: &InstrumentAny,
93 side: OrderSide,
94 quantity: Quantity,
95 price: Price,
96 use_quote_for_inverse: Option<bool>,
97 ) -> anyhow::Result<Money>;
98
99 fn calculate_pnls(
105 &self,
106 instrument: &InstrumentAny,
107 fill: &OrderFilled,
108 position: Option<Position>,
109 ) -> anyhow::Result<Vec<Money>>;
110
111 fn calculate_commission(
117 &self,
118 instrument: &InstrumentAny,
119 last_qty: Quantity,
120 last_px: Price,
121 liquidity_side: LiquiditySide,
122 fee_rates: MakerTakerFeeRates,
123 use_quote_for_inverse: Option<bool>,
124 ) -> anyhow::Result<Money>;
125}