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, BettingAccount, CashAccount, MarginAccount, WalletAccount},
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 an event cannot be converted or the input `events` list is empty.
29#[pyfunction]
30#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
31#[pyo3(signature = (events, calculate_account_state, allow_borrowing = false))]
32pub fn cash_account_from_account_events(
33    events: Vec<Bound<'_, PyDict>>,
34    calculate_account_state: bool,
35    allow_borrowing: bool,
36) -> PyResult<CashAccount> {
37    let account_events = events
38        .into_iter()
39        .map(|obj| AccountState::py_from_dict(&obj))
40        .collect::<PyResult<Vec<AccountState>>>()?;
41
42    if account_events.is_empty() {
43        return Err(to_pyvalue_err("No account events"));
44    }
45    let init_event = account_events[0].clone();
46    let mut cash_account = CashAccount::new(init_event, calculate_account_state, allow_borrowing);
47    for event in account_events.iter().skip(1) {
48        cash_account
49            .apply(event.clone())
50            .map_err(to_pyruntime_err)?;
51    }
52    Ok(cash_account)
53}
54
55/// Constructs a `BettingAccount` from a list of Python dict events.
56///
57/// # Errors
58///
59/// Returns a `PyErr` if the input `events` list is empty.
60#[pyfunction]
61#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
62pub fn betting_account_from_account_events(
63    events: Vec<Bound<'_, PyDict>>,
64    calculate_account_state: bool,
65) -> PyResult<BettingAccount> {
66    let account_events = events
67        .into_iter()
68        .map(|obj| AccountState::py_from_dict(&obj))
69        .collect::<PyResult<Vec<AccountState>>>()?;
70
71    if account_events.is_empty() {
72        return Err(to_pyvalue_err("No account events"));
73    }
74    let init_event = account_events[0].clone();
75    let mut betting_account = BettingAccount::new(init_event, calculate_account_state);
76    for event in account_events.iter().skip(1) {
77        betting_account
78            .apply(event.clone())
79            .map_err(to_pyruntime_err)?;
80    }
81    Ok(betting_account)
82}
83
84/// Constructs a `WalletAccount` from a list of Python dict events.
85///
86/// # Errors
87///
88/// Returns a `PyErr` if an event cannot be converted or the input `events` list is empty.
89#[pyfunction]
90#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
91#[pyo3(signature = (events, calculate_account_state))]
92pub fn wallet_account_from_account_events(
93    events: Vec<Bound<'_, PyDict>>,
94    calculate_account_state: bool,
95) -> PyResult<WalletAccount> {
96    let account_events = events
97        .into_iter()
98        .map(|obj| AccountState::py_from_dict(&obj))
99        .collect::<PyResult<Vec<AccountState>>>()?;
100
101    let Some((init_event, remaining_events)) = account_events.split_first() else {
102        return Err(to_pyvalue_err("No account events"));
103    };
104
105    let mut wallet_account =
106        WalletAccount::new_checked(init_event.clone(), calculate_account_state)
107            .map_err(to_pyvalue_err)?;
108
109    for event in remaining_events {
110        wallet_account
111            .apply(event.clone())
112            .map_err(to_pyruntime_err)?;
113    }
114
115    Ok(wallet_account)
116}
117
118/// Constructs a `MarginAccount` from a list of Python dict events.
119///
120/// # Errors
121///
122/// Returns a `PyErr` if an event cannot be converted or the input `events` list is empty.
123#[pyfunction]
124#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
125pub fn margin_account_from_account_events(
126    events: Vec<Bound<'_, PyDict>>,
127    calculate_account_state: bool,
128) -> PyResult<MarginAccount> {
129    let account_events = events
130        .into_iter()
131        .map(|obj| AccountState::py_from_dict(&obj))
132        .collect::<PyResult<Vec<AccountState>>>()?;
133
134    if account_events.is_empty() {
135        return Err(to_pyvalue_err("No account events"));
136    }
137    let init_event = account_events[0].clone();
138    let mut margin_account = MarginAccount::new(init_event, calculate_account_state);
139    for event in account_events.iter().skip(1) {
140        margin_account
141            .apply(event.clone())
142            .map_err(to_pyruntime_err)?;
143    }
144    Ok(margin_account)
145}