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