nautilus_model/python/account/
transformer.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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::python::to_pyvalue_err;
17use pyo3::{prelude::*, types::PyDict};
18
19use crate::{
20    accounts::{Account, CashAccount, MarginAccount},
21    events::AccountState,
22};
23
24/// Constructs a `CashAccount` from a list of Python dict events.
25///
26/// # Errors
27///
28/// Returns a `PyErr` if the input `events` list is empty.
29///
30/// # Panics
31///
32/// Panics if event conversion (`py_from_dict`) unwrap fails.
33#[pyfunction]
34pub fn cash_account_from_account_events(
35    events: Vec<Bound<'_, PyDict>>,
36    calculate_account_state: bool,
37) -> PyResult<CashAccount> {
38    let account_events = events
39        .into_iter()
40        .map(|obj| AccountState::py_from_dict(&obj))
41        .collect::<PyResult<Vec<AccountState>>>()
42        .unwrap();
43    if account_events.is_empty() {
44        return Err(to_pyvalue_err("No account events"));
45    }
46    let init_event = account_events[0].clone();
47    let mut cash_account = CashAccount::new(init_event, calculate_account_state);
48    for event in account_events.iter().skip(1) {
49        cash_account.apply(event.clone());
50    }
51    Ok(cash_account)
52}
53
54/// Constructs a `MarginAccount` from a list of Python dict events.
55///
56/// # Errors
57///
58/// Returns a `PyErr` if the input `events` list is empty.
59///
60/// # Panics
61///
62/// Panics if event conversion (`py_from_dict`) unwrap fails.
63#[pyfunction]
64pub fn margin_account_from_account_events(
65    events: Vec<Bound<'_, PyDict>>,
66    calculate_account_state: bool,
67) -> PyResult<MarginAccount> {
68    let account_events = events
69        .into_iter()
70        .map(|obj| AccountState::py_from_dict(&obj))
71        .collect::<PyResult<Vec<AccountState>>>()
72        .unwrap();
73    if account_events.is_empty() {
74        return Err(to_pyvalue_err("No account events"));
75    }
76    let init_event = account_events[0].clone();
77    let mut margin_account = MarginAccount::new(init_event, calculate_account_state);
78    for event in account_events.iter().skip(1) {
79        margin_account.apply(event.clone());
80    }
81    Ok(margin_account)
82}