Skip to main content

nautilus_model/python/events/portfolio/
snapshot.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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 nautilus_core::{UUID4, python::IntoPyObjectNautilusExt};
17use pyo3::{basic::CompareOp, prelude::*};
18
19use crate::{
20    enums::AccountType,
21    events::PortfolioSnapshot,
22    identifiers::{AccountId, InstrumentId},
23    types::{AccountBalance, Currency, MarginBalance, Money},
24};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl PortfolioSnapshot {
29    /// Represents a point-in-time snapshot of portfolio state for a single account.
30    ///
31    /// Unlike `AccountState`, which fires only on
32    /// balance or margin changes, `PortfolioSnapshot` carries a continuous
33    /// mark-to-market view by folding open-position valuations into the totals.
34    /// The default equity curve records one snapshot at account registration, at every
35    /// UTC midnight, and at shutdown, including while the account is flat. An optional
36    /// fine-grained stream records additional snapshots while positions are open.
37    /// Totals span every venue the account holds positions on, so multi-venue
38    /// accounts (e.g., a prime broker routing across exchanges) produce a single
39    /// account-wide snapshot rather than per-venue slices.
40    #[expect(clippy::too_many_arguments)]
41    #[new]
42    #[pyo3(signature = (account_id, account_type, balances, margins, unrealized_pnls, realized_pnls, total_equity, event_id, ts_event, ts_init, base_currency=None, base_currency_equity=None, is_stale=false, stale_instruments=None, stale_currencies=None, unpriced_instruments=None))]
43    fn py_new(
44        account_id: AccountId,
45        account_type: AccountType,
46        balances: Vec<AccountBalance>,
47        margins: Vec<MarginBalance>,
48        unrealized_pnls: Vec<Money>,
49        realized_pnls: Vec<Money>,
50        total_equity: Vec<Money>,
51        event_id: UUID4,
52        ts_event: u64,
53        ts_init: u64,
54        base_currency: Option<Currency>,
55        base_currency_equity: Option<Money>,
56        is_stale: bool,
57        stale_instruments: Option<Vec<InstrumentId>>,
58        stale_currencies: Option<Vec<Currency>>,
59        unpriced_instruments: Option<Vec<InstrumentId>>,
60    ) -> Self {
61        Self::new(
62            account_id,
63            account_type,
64            base_currency,
65            balances,
66            margins,
67            unrealized_pnls,
68            realized_pnls,
69            total_equity,
70            base_currency_equity,
71            is_stale,
72            stale_instruments.unwrap_or_default(),
73            stale_currencies.unwrap_or_default(),
74            unpriced_instruments.unwrap_or_default(),
75            event_id,
76            ts_event.into(),
77            ts_init.into(),
78        )
79    }
80
81    #[getter]
82    fn account_id(&self) -> AccountId {
83        self.account_id
84    }
85
86    #[getter]
87    fn account_type(&self) -> AccountType {
88        self.account_type
89    }
90
91    #[getter]
92    fn base_currency(&self) -> Option<Currency> {
93        self.base_currency
94    }
95
96    #[getter]
97    fn balances(&self) -> Vec<AccountBalance> {
98        self.balances.clone()
99    }
100
101    #[getter]
102    fn margins(&self) -> Vec<MarginBalance> {
103        self.margins.clone()
104    }
105
106    #[getter]
107    fn unrealized_pnls(&self) -> Vec<Money> {
108        self.unrealized_pnls.clone()
109    }
110
111    #[getter]
112    fn realized_pnls(&self) -> Vec<Money> {
113        self.realized_pnls.clone()
114    }
115
116    #[getter]
117    fn total_equity(&self) -> Vec<Money> {
118        self.total_equity.clone()
119    }
120
121    #[getter]
122    fn base_currency_equity(&self) -> Option<Money> {
123        self.base_currency_equity
124    }
125
126    #[getter]
127    fn is_stale(&self) -> bool {
128        self.is_stale
129    }
130
131    #[getter]
132    fn stale_instruments(&self) -> Vec<InstrumentId> {
133        self.stale_instruments.clone()
134    }
135
136    #[getter]
137    fn stale_currencies(&self) -> Vec<Currency> {
138        self.stale_currencies.clone()
139    }
140
141    #[getter]
142    fn unpriced_instruments(&self) -> Vec<InstrumentId> {
143        self.unpriced_instruments.clone()
144    }
145
146    #[getter]
147    fn event_id(&self) -> UUID4 {
148        self.event_id
149    }
150
151    #[getter]
152    fn ts_event(&self) -> u64 {
153        self.ts_event.as_u64()
154    }
155
156    #[getter]
157    fn ts_init(&self) -> u64 {
158        self.ts_init.as_u64()
159    }
160
161    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
162        match op {
163            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
164            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
165            _ => py.NotImplemented(),
166        }
167    }
168
169    fn __repr__(&self) -> String {
170        format!("{self:?}")
171    }
172
173    fn __str__(&self) -> String {
174        self.to_string()
175    }
176}