nautilus_model/python/account/
transformer.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 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]
34#[pyo3(signature = (events, calculate_account_state, allow_borrowing = false))]
35pub fn cash_account_from_account_events(
36    events: Vec<Bound<'_, PyDict>>,
37    calculate_account_state: bool,
38    allow_borrowing: bool,
39) -> PyResult<CashAccount> {
40    let account_events = events
41        .into_iter()
42        .map(|obj| AccountState::py_from_dict(&obj))
43        .collect::<PyResult<Vec<AccountState>>>()
44        .unwrap();
45    if account_events.is_empty() {
46        return Err(to_pyvalue_err("No account events"));
47    }
48    let init_event = account_events[0].clone();
49    let mut cash_account = CashAccount::new(init_event, calculate_account_state, allow_borrowing);
50    for event in account_events.iter().skip(1) {
51        cash_account.apply(event.clone());
52    }
53    Ok(cash_account)
54}
55
56/// Constructs a `MarginAccount` from a list of Python dict events.
57///
58/// # Errors
59///
60/// Returns a `PyErr` if the input `events` list is empty.
61///
62/// # Panics
63///
64/// Panics if event conversion (`py_from_dict`) unwrap fails.
65#[pyfunction]
66pub fn margin_account_from_account_events(
67    events: Vec<Bound<'_, PyDict>>,
68    calculate_account_state: bool,
69) -> PyResult<MarginAccount> {
70    let account_events = events
71        .into_iter()
72        .map(|obj| AccountState::py_from_dict(&obj))
73        .collect::<PyResult<Vec<AccountState>>>()
74        .unwrap();
75    if account_events.is_empty() {
76        return Err(to_pyvalue_err("No account events"));
77    }
78    let init_event = account_events[0].clone();
79    let mut margin_account = MarginAccount::new(init_event, calculate_account_state);
80    for event in account_events.iter().skip(1) {
81        margin_account.apply(event.clone());
82    }
83    Ok(margin_account)
84}