nautilus_model/python/account/
mod.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
16pub mod cash;
17pub mod margin;
18pub mod transformer;
19
20use nautilus_core::python::to_pyvalue_err;
21use pyo3::{IntoPy, PyObject, PyResult, Python};
22
23use crate::{
24    accounts::{any::AccountAny, cash::CashAccount, margin::MarginAccount},
25    enums::AccountType,
26};
27
28pub fn convert_pyobject_to_account_any(py: Python, account: PyObject) -> PyResult<AccountAny> {
29    let account_type = account
30        .getattr(py, "account_type")?
31        .extract::<AccountType>(py)?;
32    if account_type == AccountType::Cash {
33        let cash = account.extract::<CashAccount>(py)?;
34        Ok(AccountAny::Cash(cash))
35    } else if account_type == AccountType::Margin {
36        let margin = account.extract::<MarginAccount>(py)?;
37        Ok(AccountAny::Margin(margin))
38    } else {
39        Err(to_pyvalue_err("Unsupported account type"))
40    }
41}
42
43pub fn convert_account_any_to_pyobject(py: Python, account: AccountAny) -> PyResult<PyObject> {
44    match account {
45        AccountAny::Cash(account) => Ok(account.into_py(py)),
46        AccountAny::Margin(account) => Ok(account.into_py(py)),
47    }
48}