nautilus_model/python/orders/
market_to_limit.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 indexmap::IndexMap;
17use nautilus_core::{
18    UUID4,
19    python::{to_pyruntime_err, to_pyvalue_err},
20};
21use pyo3::prelude::*;
22use rust_decimal::Decimal;
23use ustr::Ustr;
24
25use crate::{
26    enums::{ContingencyType, OrderSide, OrderStatus, OrderType, PositionSide, TimeInForce},
27    events::order::initialized::OrderInitialized,
28    identifiers::{
29        ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, StrategyId, TraderId,
30    },
31    orders::{MarketToLimitOrder, Order, OrderCore, str_indexmap_to_ustr},
32    python::events::order::{order_event_to_pyobject, pyobject_to_order_event},
33    types::Quantity,
34};
35
36#[pymethods]
37impl MarketToLimitOrder {
38    #[new]
39    #[allow(clippy::too_many_arguments)]
40    #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, order_side, quantity, time_in_force, post_only, reduce_only, quote_quantity, init_id, ts_init, expire_time=None, display_qty=None, contingency_type=None, order_list_id=None, linked_order_ids=None, parent_order_id=None, exec_algorithm_id=None, exec_algorithm_params=None, exec_spawn_id=None, tags=None))]
41    fn py_new(
42        trader_id: TraderId,
43        strategy_id: StrategyId,
44        instrument_id: InstrumentId,
45        client_order_id: ClientOrderId,
46        order_side: OrderSide,
47        quantity: Quantity,
48        time_in_force: TimeInForce,
49        post_only: bool,
50        reduce_only: bool,
51        quote_quantity: bool,
52        init_id: UUID4,
53        ts_init: u64,
54        expire_time: Option<u64>,
55        display_qty: Option<Quantity>,
56        contingency_type: Option<ContingencyType>,
57        order_list_id: Option<OrderListId>,
58        linked_order_ids: Option<Vec<ClientOrderId>>,
59        parent_order_id: Option<ClientOrderId>,
60        exec_algorithm_id: Option<ExecAlgorithmId>,
61        exec_algorithm_params: Option<IndexMap<String, String>>,
62        exec_spawn_id: Option<ClientOrderId>,
63        tags: Option<Vec<String>>,
64    ) -> PyResult<Self> {
65        Self::new_checked(
66            trader_id,
67            strategy_id,
68            instrument_id,
69            client_order_id,
70            order_side,
71            quantity,
72            time_in_force,
73            expire_time.map(std::convert::Into::into),
74            post_only,
75            reduce_only,
76            quote_quantity,
77            display_qty,
78            contingency_type,
79            order_list_id,
80            linked_order_ids,
81            parent_order_id,
82            exec_algorithm_id,
83            exec_algorithm_params.map(str_indexmap_to_ustr),
84            exec_spawn_id,
85            tags.map(|vec| vec.into_iter().map(|s| Ustr::from(s.as_str())).collect()),
86            init_id,
87            ts_init.into(),
88        )
89        .map_err(to_pyvalue_err)
90    }
91
92    #[staticmethod]
93    #[pyo3(name = "create")]
94    fn py_create(init: OrderInitialized) -> PyResult<Self> {
95        Ok(Self::from(init))
96    }
97
98    #[staticmethod]
99    #[pyo3(name = "opposite_side")]
100    fn py_opposite_side(side: OrderSide) -> OrderSide {
101        OrderCore::opposite_side(side)
102    }
103
104    #[staticmethod]
105    #[pyo3(name = "closing_side")]
106    fn py_closing_side(side: PositionSide) -> OrderSide {
107        OrderCore::closing_side(side)
108    }
109
110    #[getter]
111    #[pyo3(name = "status")]
112    fn py_status(&self) -> OrderStatus {
113        self.status
114    }
115
116    #[getter]
117    #[pyo3(name = "order_type")]
118    fn py_order_type(&self) -> OrderType {
119        self.order_type
120    }
121
122    #[getter]
123    #[pyo3(name = "events")]
124    fn py_events(&self, py: Python<'_>) -> PyResult<Vec<Py<PyAny>>> {
125        self.events()
126            .into_iter()
127            .map(|event| order_event_to_pyobject(py, event.clone()))
128            .collect()
129    }
130
131    #[pyo3(name = "signed_decimal_qty")]
132    fn py_signed_decimal_qty(&self) -> Decimal {
133        self.signed_decimal_qty()
134    }
135
136    #[pyo3(name = "would_reduce_only")]
137    fn py_would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
138        self.would_reduce_only(side, position_qty)
139    }
140
141    #[pyo3(name = "apply")]
142    fn py_apply(&mut self, event: Py<PyAny>, py: Python<'_>) -> PyResult<()> {
143        let event_any = pyobject_to_order_event(py, event).unwrap();
144        self.apply(event_any).map(|_| ()).map_err(to_pyruntime_err)
145    }
146}