nautilus_model/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 std::ops::{Deref, DerefMut};
17
18use indexmap::IndexMap;
19use nautilus_core::{UnixNanos, UUID4};
20use rust_decimal::Decimal;
21use serde::{Deserialize, Serialize};
22use ustr::Ustr;
23
24use super::{
25    any::OrderAny,
26    base::{Order, OrderCore, OrderError},
27};
28use crate::{
29    enums::{
30        ContingencyType, LiquiditySide, OrderSide, OrderStatus, OrderType, TimeInForce,
31        TrailingOffsetType, TriggerType,
32    },
33    events::{OrderEventAny, OrderInitialized, OrderUpdated},
34    identifiers::{
35        AccountId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, PositionId,
36        StrategyId, Symbol, TradeId, TraderId, Venue, VenueOrderId,
37    },
38    types::{Price, Quantity},
39};
40
41#[derive(Clone, Debug, Serialize, Deserialize)]
42#[cfg_attr(
43    feature = "python",
44    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
45)]
46pub struct TrailingStopLimitOrder {
47    core: OrderCore,
48    pub price: Price,
49    pub trigger_price: Price,
50    pub trigger_type: TriggerType,
51    pub limit_offset: Decimal,
52    pub trailing_offset: Decimal,
53    pub trailing_offset_type: TrailingOffsetType,
54    pub expire_time: Option<UnixNanos>,
55    pub is_post_only: bool,
56    pub display_qty: Option<Quantity>,
57    pub trigger_instrument_id: Option<InstrumentId>,
58    pub is_triggered: bool,
59    pub ts_triggered: Option<UnixNanos>,
60}
61
62impl TrailingStopLimitOrder {
63    /// Creates a new [`TrailingStopLimitOrder`] instance.
64    #[allow(clippy::too_many_arguments)]
65    pub fn new(
66        trader_id: TraderId,
67        strategy_id: StrategyId,
68        instrument_id: InstrumentId,
69        client_order_id: ClientOrderId,
70        order_side: OrderSide,
71        quantity: Quantity,
72        price: Price,
73        trigger_price: Price,
74        trigger_type: TriggerType,
75        limit_offset: Decimal,
76        trailing_offset: Decimal,
77        trailing_offset_type: TrailingOffsetType,
78        time_in_force: TimeInForce,
79        expire_time: Option<UnixNanos>,
80        post_only: bool,
81        reduce_only: bool,
82        quote_quantity: bool,
83        display_qty: Option<Quantity>,
84        emulation_trigger: Option<TriggerType>,
85        trigger_instrument_id: Option<InstrumentId>,
86        contingency_type: Option<ContingencyType>,
87        order_list_id: Option<OrderListId>,
88        linked_order_ids: Option<Vec<ClientOrderId>>,
89        parent_order_id: Option<ClientOrderId>,
90        exec_algorithm_id: Option<ExecAlgorithmId>,
91        exec_algorithm_params: Option<IndexMap<Ustr, Ustr>>,
92        exec_spawn_id: Option<ClientOrderId>,
93        tags: Option<Vec<Ustr>>,
94        init_id: UUID4,
95        ts_init: UnixNanos,
96    ) -> Self {
97        let init_order = OrderInitialized::new(
98            trader_id,
99            strategy_id,
100            instrument_id,
101            client_order_id,
102            order_side,
103            OrderType::TrailingStopLimit,
104            quantity,
105            time_in_force,
106            post_only,
107            reduce_only,
108            quote_quantity,
109            false,
110            init_id,
111            ts_init,
112            ts_init,
113            Some(price),
114            Some(trigger_price),
115            Some(trigger_type),
116            Some(limit_offset),
117            Some(trailing_offset),
118            Some(trailing_offset_type),
119            expire_time,
120            display_qty,
121            emulation_trigger,
122            trigger_instrument_id,
123            contingency_type,
124            order_list_id,
125            linked_order_ids,
126            parent_order_id,
127            exec_algorithm_id,
128            exec_algorithm_params,
129            exec_spawn_id,
130            tags,
131        );
132        Self {
133            core: OrderCore::new(init_order),
134            price,
135            trigger_price,
136            trigger_type,
137            limit_offset,
138            trailing_offset,
139            trailing_offset_type,
140            expire_time,
141            is_post_only: post_only,
142            display_qty,
143            trigger_instrument_id,
144            is_triggered: false,
145            ts_triggered: None,
146        }
147    }
148}
149
150impl Deref for TrailingStopLimitOrder {
151    type Target = OrderCore;
152
153    fn deref(&self) -> &Self::Target {
154        &self.core
155    }
156}
157
158impl DerefMut for TrailingStopLimitOrder {
159    fn deref_mut(&mut self) -> &mut Self::Target {
160        &mut self.core
161    }
162}
163
164impl Order for TrailingStopLimitOrder {
165    fn into_any(self) -> OrderAny {
166        OrderAny::TrailingStopLimit(self)
167    }
168
169    fn status(&self) -> OrderStatus {
170        self.status
171    }
172
173    fn trader_id(&self) -> TraderId {
174        self.trader_id
175    }
176
177    fn strategy_id(&self) -> StrategyId {
178        self.strategy_id
179    }
180
181    fn instrument_id(&self) -> InstrumentId {
182        self.instrument_id
183    }
184
185    fn symbol(&self) -> Symbol {
186        self.instrument_id.symbol
187    }
188
189    fn venue(&self) -> Venue {
190        self.instrument_id.venue
191    }
192
193    fn client_order_id(&self) -> ClientOrderId {
194        self.client_order_id
195    }
196
197    fn venue_order_id(&self) -> Option<VenueOrderId> {
198        self.venue_order_id
199    }
200
201    fn position_id(&self) -> Option<PositionId> {
202        self.position_id
203    }
204
205    fn account_id(&self) -> Option<AccountId> {
206        self.account_id
207    }
208
209    fn last_trade_id(&self) -> Option<TradeId> {
210        self.last_trade_id
211    }
212
213    fn side(&self) -> OrderSide {
214        self.side
215    }
216
217    fn order_type(&self) -> OrderType {
218        self.order_type
219    }
220
221    fn quantity(&self) -> Quantity {
222        self.quantity
223    }
224
225    fn time_in_force(&self) -> TimeInForce {
226        self.time_in_force
227    }
228
229    fn expire_time(&self) -> Option<UnixNanos> {
230        self.expire_time
231    }
232
233    fn price(&self) -> Option<Price> {
234        Some(self.price)
235    }
236
237    fn trigger_price(&self) -> Option<Price> {
238        Some(self.trigger_price)
239    }
240
241    fn trigger_type(&self) -> Option<TriggerType> {
242        Some(self.trigger_type)
243    }
244
245    fn liquidity_side(&self) -> Option<LiquiditySide> {
246        self.liquidity_side
247    }
248
249    fn is_post_only(&self) -> bool {
250        self.is_post_only
251    }
252
253    fn is_reduce_only(&self) -> bool {
254        self.is_reduce_only
255    }
256
257    fn is_quote_quantity(&self) -> bool {
258        self.is_quote_quantity
259    }
260
261    fn display_qty(&self) -> Option<Quantity> {
262        self.display_qty
263    }
264
265    fn limit_offset(&self) -> Option<Decimal> {
266        Some(self.limit_offset)
267    }
268
269    fn trailing_offset(&self) -> Option<Decimal> {
270        Some(self.trailing_offset)
271    }
272
273    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
274        Some(self.trailing_offset_type)
275    }
276
277    fn emulation_trigger(&self) -> Option<TriggerType> {
278        self.emulation_trigger
279    }
280
281    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
282        self.trigger_instrument_id
283    }
284
285    fn contingency_type(&self) -> Option<ContingencyType> {
286        self.contingency_type
287    }
288
289    fn order_list_id(&self) -> Option<OrderListId> {
290        self.order_list_id
291    }
292
293    fn linked_order_ids(&self) -> Option<&[ClientOrderId]> {
294        self.linked_order_ids.as_deref()
295    }
296
297    fn parent_order_id(&self) -> Option<ClientOrderId> {
298        self.parent_order_id
299    }
300
301    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
302        self.exec_algorithm_id
303    }
304
305    fn exec_algorithm_params(&self) -> Option<&IndexMap<Ustr, Ustr>> {
306        self.exec_algorithm_params.as_ref()
307    }
308
309    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
310        self.exec_spawn_id
311    }
312
313    fn tags(&self) -> Option<&[Ustr]> {
314        self.tags.as_deref()
315    }
316
317    fn filled_qty(&self) -> Quantity {
318        self.filled_qty
319    }
320
321    fn leaves_qty(&self) -> Quantity {
322        self.leaves_qty
323    }
324
325    fn avg_px(&self) -> Option<f64> {
326        self.avg_px
327    }
328
329    fn slippage(&self) -> Option<f64> {
330        self.slippage
331    }
332
333    fn init_id(&self) -> UUID4 {
334        self.init_id
335    }
336
337    fn ts_init(&self) -> UnixNanos {
338        self.ts_init
339    }
340
341    fn ts_last(&self) -> UnixNanos {
342        self.ts_last
343    }
344
345    fn events(&self) -> Vec<&OrderEventAny> {
346        self.events.iter().collect()
347    }
348
349    fn venue_order_ids(&self) -> Vec<&VenueOrderId> {
350        self.venue_order_ids.iter().collect()
351    }
352
353    fn trade_ids(&self) -> Vec<&TradeId> {
354        self.trade_ids.iter().collect()
355    }
356
357    fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
358        if let OrderEventAny::Updated(ref event) = event {
359            self.update(event);
360        };
361        let is_order_filled = matches!(event, OrderEventAny::Filled(_));
362
363        self.core.apply(event)?;
364
365        if is_order_filled {
366            self.core.set_slippage(self.price);
367        };
368
369        Ok(())
370    }
371
372    fn update(&mut self, event: &OrderUpdated) {
373        if let Some(price) = event.price {
374            self.price = price;
375        }
376
377        if let Some(trigger_price) = event.trigger_price {
378            self.trigger_price = trigger_price;
379        }
380
381        self.quantity = event.quantity;
382        self.leaves_qty = self.quantity - self.filled_qty;
383    }
384}
385
386impl From<OrderAny> for TrailingStopLimitOrder {
387    fn from(order: OrderAny) -> TrailingStopLimitOrder {
388        match order {
389            OrderAny::TrailingStopLimit(order) => order,
390            _ => {
391                panic!(
392                    "Invalid `OrderAny` not `{}`, was {order:?}",
393                    stringify!(TrailingStopLimitOrder),
394                )
395            }
396        }
397    }
398}
399
400impl From<OrderInitialized> for TrailingStopLimitOrder {
401    fn from(event: OrderInitialized) -> Self {
402        Self::new(
403            event.trader_id,
404            event.strategy_id,
405            event.instrument_id,
406            event.client_order_id,
407            event.order_side,
408            event.quantity,
409            event
410                .price // TODO: Improve this error, model order domain errors
411                .expect("Error initializing order: `price` was `None` for `TrailingStopLimitOrder`"),
412            event
413                .trigger_price // TODO: Improve this error, model order domain errors
414                .expect(
415                    "Error initializing order: `trigger_price` was `None` for `TrailingStopLimitOrder`",
416                ),
417            event
418                .trigger_type
419                .expect("Error initializing order: `trigger_type` was `None` for `TrailingStopLimitOrder`"),
420            event.limit_offset.unwrap(),  // TODO
421            event.trailing_offset.unwrap(),  // TODO
422            event.trailing_offset_type.unwrap(),  // TODO
423            event.time_in_force,
424            event.expire_time,
425            event.post_only,
426            event.reduce_only,
427            event.quote_quantity,
428            event.display_qty,
429            event.emulation_trigger,
430            event.trigger_instrument_id,
431            event.contingency_type,
432            event.order_list_id,
433            event.linked_order_ids,
434            event.parent_order_id,
435            event.exec_algorithm_id,
436            event.exec_algorithm_params,
437            event.exec_spawn_id,
438            event.tags,
439            event.event_id,
440            event.ts_event,
441        )
442    }
443}