nautilus_model/python/orders/
trailing_stop_limit.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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::{
27        ContingencyType, OrderSide, OrderStatus, OrderType, PositionSide, TimeInForce,
28        TrailingOffsetType, TriggerType,
29    },
30    events::order::initialized::OrderInitialized,
31    identifiers::{
32        ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, StrategyId, TraderId,
33    },
34    orders::{Order, OrderCore, TrailingStopLimitOrder, str_indexmap_to_ustr},
35    python::events::order::{order_event_to_pyobject, pyobject_to_order_event},
36    types::{Price, Quantity},
37};
38
39#[pymethods]
40impl TrailingStopLimitOrder {
41    #[new]
42    #[allow(clippy::too_many_arguments)]
43    #[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))]
44    fn py_new(
45        trader_id: TraderId,
46        strategy_id: StrategyId,
47        instrument_id: InstrumentId,
48        client_order_id: ClientOrderId,
49        order_side: OrderSide,
50        quantity: Quantity,
51        price: Price,
52        trigger_price: Price,
53        trigger_type: TriggerType,
54        limit_offset: Decimal,
55        trailing_offset: Decimal,
56        trailing_offset_type: TrailingOffsetType,
57        time_in_force: TimeInForce,
58        post_only: bool,
59        reduce_only: bool,
60        quote_quantity: bool,
61        init_id: UUID4,
62        ts_init: u64,
63        expire_time: Option<u64>,
64        display_qty: Option<Quantity>,
65        emulation_trigger: Option<TriggerType>,
66        trigger_instrument_id: Option<InstrumentId>,
67        contingency_type: Option<ContingencyType>,
68        order_list_id: Option<OrderListId>,
69        linked_order_ids: Option<Vec<ClientOrderId>>,
70        parent_order_id: Option<ClientOrderId>,
71        exec_algorithm_id: Option<ExecAlgorithmId>,
72        exec_algorithm_params: Option<IndexMap<String, String>>,
73        exec_spawn_id: Option<ClientOrderId>,
74        tags: Option<Vec<String>>,
75    ) -> PyResult<Self> {
76        Self::new_checked(
77            trader_id,
78            strategy_id,
79            instrument_id,
80            client_order_id,
81            order_side,
82            quantity,
83            price,
84            trigger_price,
85            trigger_type,
86            limit_offset,
87            trailing_offset,
88            trailing_offset_type,
89            time_in_force,
90            expire_time.map(std::convert::Into::into),
91            post_only,
92            reduce_only,
93            quote_quantity,
94            display_qty,
95            emulation_trigger,
96            trigger_instrument_id,
97            contingency_type,
98            order_list_id,
99            linked_order_ids,
100            parent_order_id,
101            exec_algorithm_id,
102            exec_algorithm_params.map(str_indexmap_to_ustr),
103            exec_spawn_id,
104            tags.map(|vec| vec.into_iter().map(|s| Ustr::from(s.as_str())).collect()),
105            init_id,
106            ts_init.into(),
107        )
108        .map_err(to_pyvalue_err)
109    }
110
111    #[staticmethod]
112    #[pyo3(name = "create")]
113    fn py_create(init: OrderInitialized) -> PyResult<Self> {
114        Ok(TrailingStopLimitOrder::from(init))
115    }
116
117    #[staticmethod]
118    #[pyo3(name = "opposite_side")]
119    fn py_opposite_side(side: OrderSide) -> OrderSide {
120        OrderCore::opposite_side(side)
121    }
122
123    #[staticmethod]
124    #[pyo3(name = "closing_side")]
125    fn py_closing_side(side: PositionSide) -> OrderSide {
126        OrderCore::closing_side(side)
127    }
128
129    #[getter]
130    #[pyo3(name = "status")]
131    fn py_status(&self) -> OrderStatus {
132        self.status
133    }
134
135    #[getter]
136    #[pyo3(name = "order_type")]
137    fn py_order_type(&self) -> OrderType {
138        self.order_type
139    }
140
141    #[getter]
142    #[pyo3(name = "events")]
143    fn py_events(&self, py: Python<'_>) -> PyResult<Vec<PyObject>> {
144        self.events()
145            .into_iter()
146            .map(|event| order_event_to_pyobject(py, event.clone()))
147            .collect()
148    }
149
150    #[pyo3(name = "signed_decimal_qty")]
151    fn py_signed_decimal_qty(&self) -> Decimal {
152        self.signed_decimal_qty()
153    }
154
155    #[pyo3(name = "would_reduce_only")]
156    fn py_would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
157        self.would_reduce_only(side, position_qty)
158    }
159
160    #[pyo3(name = "apply")]
161    fn py_apply(&mut self, event: PyObject, py: Python<'_>) -> PyResult<()> {
162        let event_any = pyobject_to_order_event(py, event).unwrap();
163        self.apply(event_any).map(|_| ()).map_err(to_pyruntime_err)
164    }
165}