nautilus_model/python/orders/
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
16use nautilus_core::python::to_pyvalue_err;
17use pyo3::{IntoPy, PyObject, PyResult, Python};
18
19use crate::{
20    enums::OrderType,
21    orders::{
22        LimitIfTouchedOrder, LimitOrder, MarketIfTouchedOrder, MarketOrder, MarketToLimitOrder,
23        OrderAny, StopLimitOrder, StopMarketOrder, TrailingStopLimitOrder, TrailingStopMarketOrder,
24    },
25};
26
27pub mod limit;
28pub mod limit_if_touched;
29pub mod market;
30pub mod market_if_touched;
31pub mod market_to_limit;
32pub mod stop_limit;
33pub mod stop_market;
34pub mod trailing_stop_limit;
35pub mod trailing_stop_market;
36
37pub fn convert_pyobject_to_order_any(py: Python, order: PyObject) -> PyResult<OrderAny> {
38    let order_type = order.getattr(py, "order_type")?.extract::<OrderType>(py)?;
39    if order_type == OrderType::Limit {
40        let limit = order.extract::<LimitOrder>(py)?;
41        Ok(OrderAny::Limit(limit))
42    } else if order_type == OrderType::Market {
43        let market = order.extract::<MarketOrder>(py)?;
44        Ok(OrderAny::Market(market))
45    } else if order_type == OrderType::StopLimit {
46        let stop_limit = order.extract::<StopLimitOrder>(py)?;
47        Ok(OrderAny::StopLimit(stop_limit))
48    } else if order_type == OrderType::LimitIfTouched {
49        let limit_if_touched = order.extract::<LimitIfTouchedOrder>(py)?;
50        Ok(OrderAny::LimitIfTouched(limit_if_touched))
51    } else if order_type == OrderType::MarketIfTouched {
52        let market_if_touched = order.extract::<MarketIfTouchedOrder>(py)?;
53        Ok(OrderAny::MarketIfTouched(market_if_touched))
54    } else if order_type == OrderType::MarketToLimit {
55        let market_to_limit = order.extract::<MarketToLimitOrder>(py)?;
56        Ok(OrderAny::MarketToLimit(market_to_limit))
57    } else if order_type == OrderType::StopMarket {
58        let stop_market = order.extract::<StopMarketOrder>(py)?;
59        Ok(OrderAny::StopMarket(stop_market))
60    } else if order_type == OrderType::TrailingStopMarket {
61        let trailing_stop_market = order.extract::<TrailingStopMarketOrder>(py)?;
62        Ok(OrderAny::TrailingStopMarket(trailing_stop_market))
63    } else if order_type == OrderType::TrailingStopLimit {
64        let trailing_stop_limit = order.extract::<TrailingStopLimitOrder>(py)?;
65        Ok(OrderAny::TrailingStopLimit(trailing_stop_limit))
66    } else {
67        Err(to_pyvalue_err("Unsupported order type"))
68    }
69}
70
71pub fn convert_order_any_to_pyobject(py: Python, order: OrderAny) -> PyResult<PyObject> {
72    match order {
73        OrderAny::Limit(limit_order) => Ok(limit_order.into_py(py)),
74        OrderAny::LimitIfTouched(limit_if_touched_order) => Ok(limit_if_touched_order.into_py(py)),
75        OrderAny::Market(market_order) => Ok(market_order.into_py(py)),
76        OrderAny::MarketIfTouched(market_if_touched_order) => {
77            Ok(market_if_touched_order.into_py(py))
78        }
79        OrderAny::MarketToLimit(market_to_limit_order) => Ok(market_to_limit_order.into_py(py)),
80        OrderAny::StopLimit(stop_limit_order) => Ok(stop_limit_order.into_py(py)),
81        OrderAny::StopMarket(stop_market_order) => Ok(stop_market_order.into_py(py)),
82        OrderAny::TrailingStopLimit(trailing_stop_limit_order) => {
83            Ok(trailing_stop_limit_order.into_py(py))
84        }
85        OrderAny::TrailingStopMarket(trailing_stop_market_order) => {
86            Ok(trailing_stop_market_order.into_py(py))
87        }
88    }
89}