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