Skip to main content

nautilus_model/python/account/
transformer.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::python::{to_pyruntime_err, 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
52            .apply(event.clone())
53            .map_err(to_pyruntime_err)?;
54    }
55    Ok(cash_account)
56}
57
58/// Constructs a `MarginAccount` from a list of Python dict events.
59///
60/// # Errors
61///
62/// Returns a `PyErr` if the input `events` list is empty.
63///
64/// # Panics
65///
66/// Panics if event conversion (`py_from_dict`) unwrap fails.
67#[pyfunction]
68pub fn margin_account_from_account_events(
69    events: Vec<Bound<'_, PyDict>>,
70    calculate_account_state: bool,
71) -> PyResult<MarginAccount> {
72    let account_events = events
73        .into_iter()
74        .map(|obj| AccountState::py_from_dict(&obj))
75        .collect::<PyResult<Vec<AccountState>>>()
76        .unwrap();
77    if account_events.is_empty() {
78        return Err(to_pyvalue_err("No account events"));
79    }
80    let init_event = account_events[0].clone();
81    let mut margin_account = MarginAccount::new(init_event, calculate_account_state);
82    for event in account_events.iter().skip(1) {
83        margin_account
84            .apply(event.clone())
85            .map_err(to_pyruntime_err)?;
86    }
87    Ok(margin_account)
88}