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::{UUID4, python::to_pyruntime_err};
18use pyo3::prelude::*;
19use rust_decimal::Decimal;
20use ustr::Ustr;
21
22use crate::{
23    enums::{
24        ContingencyType, OrderSide, OrderStatus, OrderType, PositionSide, TimeInForce,
25        TrailingOffsetType, TriggerType,
26    },
27    events::order::initialized::OrderInitialized,
28    identifiers::{
29        ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, StrategyId, TraderId,
30    },
31    orders::{Order, OrderCore, TrailingStopLimitOrder, str_indexmap_to_ustr},
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    #[staticmethod]
109    #[pyo3(name = "create")]
110    fn py_create(init: OrderInitialized) -> PyResult<Self> {
111        Ok(TrailingStopLimitOrder::from(init))
112    }
113
114    #[staticmethod]
115    #[pyo3(name = "opposite_side")]
116    fn py_opposite_side(side: OrderSide) -> OrderSide {
117        OrderCore::opposite_side(side)
118    }
119
120    #[staticmethod]
121    #[pyo3(name = "closing_side")]
122    fn py_closing_side(side: PositionSide) -> OrderSide {
123        OrderCore::closing_side(side)
124    }
125
126    #[getter]
127    #[pyo3(name = "status")]
128    fn py_status(&self) -> OrderStatus {
129        self.status
130    }
131
132    #[getter]
133    #[pyo3(name = "order_type")]
134    fn py_order_type(&self) -> OrderType {
135        self.order_type
136    }
137
138    #[getter]
139    #[pyo3(name = "events")]
140    fn py_events(&self, py: Python<'_>) -> PyResult<Vec<PyObject>> {
141        self.events()
142            .into_iter()
143            .map(|event| order_event_to_pyobject(py, event.clone()))
144            .collect()
145    }
146
147    #[pyo3(name = "signed_decimal_qty")]
148    fn py_signed_decimal_qty(&self) -> Decimal {
149        self.signed_decimal_qty()
150    }
151
152    #[pyo3(name = "would_reduce_only")]
153    fn py_would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
154        self.would_reduce_only(side, position_qty)
155    }
156
157    #[pyo3(name = "apply")]
158    fn py_apply(&mut self, event: PyObject, py: Python<'_>) -> PyResult<()> {
159        let event_any = pyobject_to_order_event(py, event).unwrap();
160        self.apply(event_any).map(|_| ()).map_err(to_pyruntime_err)
161    }
162}