nautilus_model/events/account/
state.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
16use std::fmt::{Display, Formatter};
17
18use nautilus_core::{UnixNanos, UUID4};
19use serde::{Deserialize, Serialize};
20
21use crate::{
22    enums::AccountType,
23    identifiers::AccountId,
24    types::{AccountBalance, Currency, MarginBalance},
25};
26
27/// Represents an event which includes information on the state of the account.
28#[repr(C)]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[cfg_attr(
31    feature = "python",
32    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
33)]
34pub struct AccountState {
35    /// The account ID associated with the event.
36    pub account_id: AccountId,
37    /// The type of the account (e.g., margin, spot, etc.).
38    pub account_type: AccountType,
39    /// The base currency for the account, if applicable.
40    pub base_currency: Option<Currency>,
41    /// The balances in the account.
42    pub balances: Vec<AccountBalance>,
43    /// The margin balances in the account.
44    pub margins: Vec<MarginBalance>,
45    /// Indicates if the account state is reported by the exchange
46    /// (as opposed to system-calculated).
47    pub is_reported: bool,
48    /// The unique identifier for the event.
49    pub event_id: UUID4,
50    /// UNIX timestamp (nanoseconds) when the event occurred.
51    pub ts_event: UnixNanos,
52    /// UNIX timestamp (nanoseconds) when the event was initialized.
53    pub ts_init: UnixNanos,
54}
55
56impl AccountState {
57    /// Creates a new [`AccountState`] instance.
58    #[allow(clippy::too_many_arguments)]
59    pub fn new(
60        account_id: AccountId,
61        account_type: AccountType,
62        balances: Vec<AccountBalance>,
63        margins: Vec<MarginBalance>,
64        is_reported: bool,
65        event_id: UUID4,
66        ts_event: UnixNanos,
67        ts_init: UnixNanos,
68        base_currency: Option<Currency>,
69    ) -> Self {
70        Self {
71            account_id,
72            account_type,
73            base_currency,
74            balances,
75            margins,
76            is_reported,
77            event_id,
78            ts_event,
79            ts_init,
80        }
81    }
82}
83
84impl Display for AccountState {
85    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
86        write!(
87            f,
88            "{}(account_id={}, account_type={}, base_currency={}, is_reported={}, balances=[{}], margins=[{}], event_id={})",
89            stringify!(AccountState),
90            self.account_id,
91            self.account_type,
92            self.base_currency.map_or_else(|| "None".to_string(), |base_currency | format!("{}", base_currency.code)),
93            self.is_reported,
94            self.balances.iter().map(|b| format!("{b}")).collect::<Vec<String>>().join(","),
95            self.margins.iter().map(|m| format!("{m}")).collect::<Vec<String>>().join(","),
96            self.event_id
97        )
98    }
99}
100
101impl PartialEq for AccountState {
102    fn eq(&self, other: &Self) -> bool {
103        self.account_id == other.account_id
104            && self.account_type == other.account_type
105            && self.event_id == other.event_id
106    }
107}
108
109////////////////////////////////////////////////////////////////////////////////
110// Tests
111////////////////////////////////////////////////////////////////////////////////
112#[cfg(test)]
113mod tests {
114    use rstest::rstest;
115
116    use crate::events::{
117        account::stubs::{cash_account_state, margin_account_state},
118        AccountState,
119    };
120
121    #[rstest]
122    fn test_equality() {
123        let cash_account_state_1 = cash_account_state();
124        let cash_account_state_2 = cash_account_state();
125        assert_eq!(cash_account_state_1, cash_account_state_2);
126    }
127
128    #[rstest]
129    fn test_display_cash_account_state(cash_account_state: AccountState) {
130        let display = format!("{cash_account_state}");
131        assert_eq!(
132            display,
133            "AccountState(account_id=SIM-001, account_type=CASH, base_currency=USD, is_reported=true, \
134            balances=[AccountBalance(total=1525000.00 USD, locked=25000.00 USD, free=1500000.00 USD)], \
135            margins=[], event_id=16578139-a945-4b65-b46c-bc131a15d8e7)"
136        );
137    }
138
139    #[rstest]
140    fn test_display_margin_account_state(margin_account_state: AccountState) {
141        let display = format!("{margin_account_state}");
142        assert_eq!(
143            display,
144            "AccountState(account_id=SIM-001, account_type=MARGIN, base_currency=USD, is_reported=true, \
145            balances=[AccountBalance(total=1525000.00 USD, locked=25000.00 USD, free=1500000.00 USD)], \
146            margins=[MarginBalance(initial=5000.00 USD, maintenance=20000.00 USD, instrument_id=BTCUSDT.COINBASE)], \
147            event_id=16578139-a945-4b65-b46c-bc131a15d8e7)"
148        );
149    }
150}