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::{
17    fmt::Display,
18    ops::{Deref, DerefMut},
19};
20
21use indexmap::IndexMap;
22use nautilus_core::{UUID4, UnixNanos, correctness::FAILED};
23use rust_decimal::Decimal;
24use serde::{Deserialize, Serialize};
25use ustr::Ustr;
26
27use super::{Order, OrderAny, OrderCore, OrderError};
28use crate::{
29    enums::{
30        ContingencyType, LiquiditySide, OrderSide, OrderStatus, OrderType, PositionSide,
31        TimeInForce, 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    orders::{check_display_qty, check_time_in_force},
39    types::{Currency, Money, Price, Quantity, quantity::check_positive_quantity},
40};
41
42#[derive(Clone, Debug, Serialize, Deserialize)]
43#[cfg_attr(
44    feature = "python",
45    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
46)]
47pub struct TrailingStopLimitOrder {
48    core: OrderCore,
49    pub activation_price: Option<Price>,
50    pub price: Price,
51    pub trigger_price: Price,
52    pub trigger_type: TriggerType,
53    pub limit_offset: Decimal,
54    pub trailing_offset: Decimal,
55    pub trailing_offset_type: TrailingOffsetType,
56    pub expire_time: Option<UnixNanos>,
57    pub is_post_only: bool,
58    pub display_qty: Option<Quantity>,
59    pub trigger_instrument_id: Option<InstrumentId>,
60    pub is_activated: bool,
61    pub is_triggered: bool,
62    pub ts_triggered: Option<UnixNanos>,
63}
64
65impl TrailingStopLimitOrder {
66    /// Creates a new [`TrailingStopLimitOrder`] instance.
67    ///
68    /// # Errors
69    ///
70    /// Returns an error if:
71    /// - The `quantity` is not positive.
72    /// - The `display_qty` (when provided) exceeds `quantity`.
73    /// - The `time_in_force` is `GTD` **and** `expire_time` is `None` or zero.
74    #[allow(clippy::too_many_arguments)]
75    pub fn new_checked(
76        trader_id: TraderId,
77        strategy_id: StrategyId,
78        instrument_id: InstrumentId,
79        client_order_id: ClientOrderId,
80        order_side: OrderSide,
81        quantity: Quantity,
82        price: Price,
83        trigger_price: Price,
84        trigger_type: TriggerType,
85        limit_offset: Decimal,
86        trailing_offset: Decimal,
87        trailing_offset_type: TrailingOffsetType,
88        time_in_force: TimeInForce,
89        expire_time: Option<UnixNanos>,
90        post_only: bool,
91        reduce_only: bool,
92        quote_quantity: bool,
93        display_qty: Option<Quantity>,
94        emulation_trigger: Option<TriggerType>,
95        trigger_instrument_id: Option<InstrumentId>,
96        contingency_type: Option<ContingencyType>,
97        order_list_id: Option<OrderListId>,
98        linked_order_ids: Option<Vec<ClientOrderId>>,
99        parent_order_id: Option<ClientOrderId>,
100        exec_algorithm_id: Option<ExecAlgorithmId>,
101        exec_algorithm_params: Option<IndexMap<Ustr, Ustr>>,
102        exec_spawn_id: Option<ClientOrderId>,
103        tags: Option<Vec<Ustr>>,
104        init_id: UUID4,
105        ts_init: UnixNanos,
106    ) -> anyhow::Result<Self> {
107        check_positive_quantity(quantity, stringify!(quantity))?;
108        check_display_qty(display_qty, quantity)?;
109        check_time_in_force(time_in_force, expire_time)?;
110
111        let init_order = OrderInitialized::new(
112            trader_id,
113            strategy_id,
114            instrument_id,
115            client_order_id,
116            order_side,
117            OrderType::TrailingStopLimit,
118            quantity,
119            time_in_force,
120            post_only,
121            reduce_only,
122            quote_quantity,
123            false,
124            init_id,
125            ts_init,
126            ts_init,
127            Some(price),
128            Some(trigger_price),
129            Some(trigger_type),
130            Some(limit_offset),
131            Some(trailing_offset),
132            Some(trailing_offset_type),
133            expire_time,
134            display_qty,
135            emulation_trigger,
136            trigger_instrument_id,
137            contingency_type,
138            order_list_id,
139            linked_order_ids,
140            parent_order_id,
141            exec_algorithm_id,
142            exec_algorithm_params,
143            exec_spawn_id,
144            tags,
145        );
146
147        Ok(Self {
148            core: OrderCore::new(init_order),
149            activation_price: None,
150            price,
151            trigger_price,
152            trigger_type,
153            limit_offset,
154            trailing_offset,
155            trailing_offset_type,
156            expire_time,
157            is_post_only: post_only,
158            display_qty,
159            trigger_instrument_id,
160            is_activated: false,
161            is_triggered: false,
162            ts_triggered: None,
163        })
164    }
165
166    /// Creates a new [`TrailingStopLimitOrder`] instance.
167    ///
168    /// # Panics
169    ///
170    /// Panics if any order validation fails (see [`TrailingStopLimitOrder::new_checked`]).
171    #[allow(clippy::too_many_arguments)]
172    pub fn new(
173        trader_id: TraderId,
174        strategy_id: StrategyId,
175        instrument_id: InstrumentId,
176        client_order_id: ClientOrderId,
177        order_side: OrderSide,
178        quantity: Quantity,
179        price: Price,
180        trigger_price: Price,
181        trigger_type: TriggerType,
182        limit_offset: Decimal,
183        trailing_offset: Decimal,
184        trailing_offset_type: TrailingOffsetType,
185        time_in_force: TimeInForce,
186        expire_time: Option<UnixNanos>,
187        post_only: bool,
188        reduce_only: bool,
189        quote_quantity: bool,
190        display_qty: Option<Quantity>,
191        emulation_trigger: Option<TriggerType>,
192        trigger_instrument_id: Option<InstrumentId>,
193        contingency_type: Option<ContingencyType>,
194        order_list_id: Option<OrderListId>,
195        linked_order_ids: Option<Vec<ClientOrderId>>,
196        parent_order_id: Option<ClientOrderId>,
197        exec_algorithm_id: Option<ExecAlgorithmId>,
198        exec_algorithm_params: Option<IndexMap<Ustr, Ustr>>,
199        exec_spawn_id: Option<ClientOrderId>,
200        tags: Option<Vec<Ustr>>,
201        init_id: UUID4,
202        ts_init: UnixNanos,
203    ) -> Self {
204        Self::new_checked(
205            trader_id,
206            strategy_id,
207            instrument_id,
208            client_order_id,
209            order_side,
210            quantity,
211            price,
212            trigger_price,
213            trigger_type,
214            limit_offset,
215            trailing_offset,
216            trailing_offset_type,
217            time_in_force,
218            expire_time,
219            post_only,
220            reduce_only,
221            quote_quantity,
222            display_qty,
223            emulation_trigger,
224            trigger_instrument_id,
225            contingency_type,
226            order_list_id,
227            linked_order_ids,
228            parent_order_id,
229            exec_algorithm_id,
230            exec_algorithm_params,
231            exec_spawn_id,
232            tags,
233            init_id,
234            ts_init,
235        )
236        .expect(FAILED)
237    }
238
239    pub fn has_activation_price(&self) -> bool {
240        self.activation_price.is_some()
241    }
242
243    pub fn set_activated(&mut self) {
244        debug_assert!(!self.is_activated, "double activation");
245        self.is_activated = true;
246    }
247}
248
249impl Deref for TrailingStopLimitOrder {
250    type Target = OrderCore;
251    fn deref(&self) -> &Self::Target {
252        &self.core
253    }
254}
255
256impl DerefMut for TrailingStopLimitOrder {
257    fn deref_mut(&mut self) -> &mut Self::Target {
258        &mut self.core
259    }
260}
261
262impl Order for TrailingStopLimitOrder {
263    fn activation_price(&self) -> Option<Price> {
264        self.activation_price
265    }
266    fn into_any(self) -> OrderAny {
267        OrderAny::TrailingStopLimit(self)
268    }
269
270    fn status(&self) -> OrderStatus {
271        self.status
272    }
273
274    fn trader_id(&self) -> TraderId {
275        self.trader_id
276    }
277
278    fn strategy_id(&self) -> StrategyId {
279        self.strategy_id
280    }
281
282    fn instrument_id(&self) -> InstrumentId {
283        self.instrument_id
284    }
285
286    fn symbol(&self) -> Symbol {
287        self.instrument_id.symbol
288    }
289
290    fn venue(&self) -> Venue {
291        self.instrument_id.venue
292    }
293
294    fn client_order_id(&self) -> ClientOrderId {
295        self.client_order_id
296    }
297
298    fn venue_order_id(&self) -> Option<VenueOrderId> {
299        self.venue_order_id
300    }
301
302    fn position_id(&self) -> Option<PositionId> {
303        self.position_id
304    }
305
306    fn account_id(&self) -> Option<AccountId> {
307        self.account_id
308    }
309
310    fn last_trade_id(&self) -> Option<TradeId> {
311        self.last_trade_id
312    }
313
314    fn order_side(&self) -> OrderSide {
315        self.side
316    }
317
318    fn order_type(&self) -> OrderType {
319        self.order_type
320    }
321
322    fn quantity(&self) -> Quantity {
323        self.quantity
324    }
325
326    fn time_in_force(&self) -> TimeInForce {
327        self.time_in_force
328    }
329
330    fn expire_time(&self) -> Option<UnixNanos> {
331        self.expire_time
332    }
333
334    fn price(&self) -> Option<Price> {
335        Some(self.price)
336    }
337
338    fn trigger_price(&self) -> Option<Price> {
339        Some(self.trigger_price)
340    }
341
342    fn trigger_type(&self) -> Option<TriggerType> {
343        Some(self.trigger_type)
344    }
345
346    fn liquidity_side(&self) -> Option<LiquiditySide> {
347        self.liquidity_side
348    }
349
350    fn is_post_only(&self) -> bool {
351        self.is_post_only
352    }
353
354    fn is_reduce_only(&self) -> bool {
355        self.is_reduce_only
356    }
357
358    fn is_quote_quantity(&self) -> bool {
359        self.is_quote_quantity
360    }
361
362    fn has_price(&self) -> bool {
363        true
364    }
365
366    fn display_qty(&self) -> Option<Quantity> {
367        self.display_qty
368    }
369
370    fn limit_offset(&self) -> Option<Decimal> {
371        Some(self.limit_offset)
372    }
373
374    fn trailing_offset(&self) -> Option<Decimal> {
375        Some(self.trailing_offset)
376    }
377
378    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
379        Some(self.trailing_offset_type)
380    }
381
382    fn emulation_trigger(&self) -> Option<TriggerType> {
383        self.emulation_trigger
384    }
385
386    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
387        self.trigger_instrument_id
388    }
389
390    fn contingency_type(&self) -> Option<ContingencyType> {
391        self.contingency_type
392    }
393
394    fn order_list_id(&self) -> Option<OrderListId> {
395        self.order_list_id
396    }
397
398    fn linked_order_ids(&self) -> Option<&[ClientOrderId]> {
399        self.linked_order_ids.as_deref()
400    }
401
402    fn parent_order_id(&self) -> Option<ClientOrderId> {
403        self.parent_order_id
404    }
405
406    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
407        self.exec_algorithm_id
408    }
409
410    fn exec_algorithm_params(&self) -> Option<&IndexMap<Ustr, Ustr>> {
411        self.exec_algorithm_params.as_ref()
412    }
413
414    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
415        self.exec_spawn_id
416    }
417
418    fn tags(&self) -> Option<&[Ustr]> {
419        self.tags.as_deref()
420    }
421
422    fn filled_qty(&self) -> Quantity {
423        self.filled_qty
424    }
425
426    fn leaves_qty(&self) -> Quantity {
427        self.leaves_qty
428    }
429
430    fn overfill_qty(&self) -> Quantity {
431        self.overfill_qty
432    }
433
434    fn avg_px(&self) -> Option<f64> {
435        self.avg_px
436    }
437
438    fn slippage(&self) -> Option<f64> {
439        self.slippage
440    }
441
442    fn init_id(&self) -> UUID4 {
443        self.init_id
444    }
445
446    fn ts_init(&self) -> UnixNanos {
447        self.ts_init
448    }
449
450    fn ts_submitted(&self) -> Option<UnixNanos> {
451        self.ts_submitted
452    }
453
454    fn ts_accepted(&self) -> Option<UnixNanos> {
455        self.ts_accepted
456    }
457
458    fn ts_closed(&self) -> Option<UnixNanos> {
459        self.ts_closed
460    }
461
462    fn ts_last(&self) -> UnixNanos {
463        self.ts_last
464    }
465
466    fn events(&self) -> Vec<&OrderEventAny> {
467        self.events.iter().collect()
468    }
469
470    fn venue_order_ids(&self) -> Vec<&VenueOrderId> {
471        self.venue_order_ids.iter().collect()
472    }
473
474    fn trade_ids(&self) -> Vec<&TradeId> {
475        self.trade_ids.iter().collect()
476    }
477
478    fn commissions(&self) -> &IndexMap<Currency, Money> {
479        &self.commissions
480    }
481
482    fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
483        if let OrderEventAny::Updated(ref event) = event {
484            self.update(event);
485        }
486
487        let is_order_filled = matches!(event, OrderEventAny::Filled(_));
488        let is_order_triggered = matches!(event, OrderEventAny::Triggered(_));
489        let ts_event = if is_order_triggered {
490            Some(event.ts_event())
491        } else {
492            None
493        };
494
495        self.core.apply(event)?;
496
497        if is_order_triggered {
498            self.is_triggered = true;
499            self.ts_triggered = ts_event;
500        }
501
502        if is_order_filled {
503            self.core.set_slippage(self.price);
504        }
505
506        Ok(())
507    }
508
509    fn update(&mut self, event: &OrderUpdated) {
510        if let Some(price) = event.price {
511            self.price = price;
512        }
513        if let Some(trigger_price) = event.trigger_price {
514            self.trigger_price = trigger_price;
515        }
516        self.quantity = event.quantity;
517        self.leaves_qty = self.quantity.saturating_sub(self.filled_qty);
518    }
519
520    fn is_triggered(&self) -> Option<bool> {
521        Some(self.is_triggered)
522    }
523
524    fn set_position_id(&mut self, position_id: Option<PositionId>) {
525        self.position_id = position_id;
526    }
527
528    fn set_quantity(&mut self, quantity: Quantity) {
529        self.quantity = quantity;
530    }
531
532    fn set_leaves_qty(&mut self, leaves_qty: Quantity) {
533        self.leaves_qty = leaves_qty;
534    }
535
536    fn set_emulation_trigger(&mut self, emulation_trigger: Option<TriggerType>) {
537        self.emulation_trigger = emulation_trigger;
538    }
539
540    fn set_is_quote_quantity(&mut self, is_quote_quantity: bool) {
541        self.is_quote_quantity = is_quote_quantity;
542    }
543
544    fn set_liquidity_side(&mut self, liquidity_side: LiquiditySide) {
545        self.liquidity_side = Some(liquidity_side);
546    }
547
548    fn would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
549        self.core.would_reduce_only(side, position_qty)
550    }
551
552    fn previous_status(&self) -> Option<OrderStatus> {
553        self.core.previous_status
554    }
555}
556
557impl Display for TrailingStopLimitOrder {
558    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
559        write!(
560            f,
561            "TrailingStopLimitOrder({} {} {} {} {}, status={}, client_order_id={}, venue_order_id={}, position_id={}, exec_algorithm_id={}, exec_spawn_id={}, tags={:?}, activation_price={:?}, is_activated={})",
562            self.side,
563            self.quantity.to_formatted_string(),
564            self.instrument_id,
565            self.order_type,
566            self.time_in_force,
567            self.status,
568            self.client_order_id,
569            self.venue_order_id
570                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
571            self.position_id
572                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
573            self.exec_algorithm_id
574                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
575            self.exec_spawn_id
576                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
577            self.tags,
578            self.activation_price,
579            self.is_activated
580        )
581    }
582}
583
584impl From<OrderInitialized> for TrailingStopLimitOrder {
585    fn from(event: OrderInitialized) -> Self {
586        Self::new(
587            event.trader_id,
588            event.strategy_id,
589            event.instrument_id,
590            event.client_order_id,
591            event.order_side,
592            event.quantity,
593            event
594                .price
595                .expect("Error initializing order: price is None"),
596            event
597                .trigger_price
598                .expect("Error initializing order: trigger_price is None"),
599            event
600                .trigger_type
601                .expect("Error initializing order: trigger_type is None"),
602            event.limit_offset.unwrap(),
603            event.trailing_offset.unwrap(),
604            event.trailing_offset_type.unwrap(),
605            event.time_in_force,
606            event.expire_time,
607            event.post_only,
608            event.reduce_only,
609            event.quote_quantity,
610            event.display_qty,
611            event.emulation_trigger,
612            event.trigger_instrument_id,
613            event.contingency_type,
614            event.order_list_id,
615            event.linked_order_ids,
616            event.parent_order_id,
617            event.exec_algorithm_id,
618            event.exec_algorithm_params,
619            event.exec_spawn_id,
620            event.tags,
621            event.event_id,
622            event.ts_event,
623        )
624    }
625}
626
627#[cfg(test)]
628mod tests {
629    use rstest::rstest;
630    use rust_decimal_macros::dec;
631
632    use super::*;
633    use crate::{
634        enums::{TimeInForce, TrailingOffsetType, TriggerType},
635        events::order::initialized::OrderInitializedBuilder,
636        identifiers::InstrumentId,
637        instruments::{CurrencyPair, stubs::*},
638        orders::{OrderTestBuilder, stubs::TestOrderStubs},
639        types::{Price, Quantity},
640    };
641
642    #[rstest]
643    fn test_initialize(_audusd_sim: CurrencyPair) {
644        // Create and accept a basic trailing stop limit order
645        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
646            .instrument_id(_audusd_sim.id)
647            .side(OrderSide::Buy)
648            .price(Price::from("0.67500"))
649            .limit_offset(dec!(5))
650            .trigger_price(Price::from("0.68000"))
651            .trailing_offset(dec!(10))
652            .quantity(Quantity::from(1))
653            .build();
654
655        assert_eq!(order.trigger_price(), Some(Price::from("0.68000")));
656        assert_eq!(order.price(), Some(Price::from("0.67500")));
657        assert_eq!(order.time_in_force(), TimeInForce::Gtc);
658        assert_eq!(order.is_triggered(), Some(false));
659        assert_eq!(order.filled_qty(), Quantity::from(0));
660        assert_eq!(order.leaves_qty(), Quantity::from(1));
661        assert_eq!(order.display_qty(), None);
662        assert_eq!(order.trigger_instrument_id(), None);
663        assert_eq!(order.order_list_id(), None);
664    }
665
666    #[rstest]
667    fn test_display(_audusd_sim: CurrencyPair) {
668        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
669            .instrument_id(_audusd_sim.id)
670            .side(OrderSide::Buy)
671            .price(Price::from("0.67500"))
672            .trigger_price(Price::from("0.68000"))
673            .trigger_type(TriggerType::LastPrice)
674            .limit_offset(dec!(5))
675            .trailing_offset(dec!(10))
676            .trailing_offset_type(TrailingOffsetType::Price)
677            .quantity(Quantity::from(1))
678            .build();
679
680        assert_eq!(
681            order.to_string(),
682            "TrailingStopLimitOrder(BUY 1 AUD/USD.SIM TRAILING_STOP_LIMIT GTC, status=INITIALIZED, client_order_id=O-19700101-000000-001-001-1, venue_order_id=None, position_id=None, exec_algorithm_id=None, exec_spawn_id=None, tags=None, activation_price=None, is_activated=false)"
683        );
684    }
685
686    #[rstest]
687    #[should_panic(expected = "Condition failed: `display_qty` may not exceed `quantity`")]
688    fn test_display_qty_gt_quantity_err(audusd_sim: CurrencyPair) {
689        OrderTestBuilder::new(OrderType::TrailingStopLimit)
690            .instrument_id(audusd_sim.id)
691            .side(OrderSide::Buy)
692            .price(Price::from("0.67500"))
693            .trigger_price(Price::from("0.68000"))
694            .trigger_type(TriggerType::LastPrice)
695            .limit_offset(dec!(5))
696            .trailing_offset(dec!(10))
697            .trailing_offset_type(TrailingOffsetType::Price)
698            .quantity(Quantity::from(1))
699            .display_qty(Quantity::from(2))
700            .build();
701    }
702
703    #[rstest]
704    #[should_panic(
705        expected = "Condition failed: invalid `Quantity` for 'quantity' not positive, was 0"
706    )]
707    fn test_quantity_zero_err(audusd_sim: CurrencyPair) {
708        OrderTestBuilder::new(OrderType::TrailingStopLimit)
709            .instrument_id(audusd_sim.id)
710            .side(OrderSide::Buy)
711            .price(Price::from("0.67500"))
712            .trigger_price(Price::from("0.68000"))
713            .trigger_type(TriggerType::LastPrice)
714            .limit_offset(dec!(5))
715            .trailing_offset(dec!(10))
716            .trailing_offset_type(TrailingOffsetType::Price)
717            .quantity(Quantity::from(0))
718            .build();
719    }
720
721    #[rstest]
722    #[should_panic(expected = "Condition failed: `expire_time` is required for `GTD` order")]
723    fn test_gtd_without_expire_err(audusd_sim: CurrencyPair) {
724        OrderTestBuilder::new(OrderType::TrailingStopLimit)
725            .instrument_id(audusd_sim.id)
726            .side(OrderSide::Buy)
727            .price(Price::from("0.67500"))
728            .trigger_price(Price::from("0.68000"))
729            .trigger_type(TriggerType::LastPrice)
730            .limit_offset(dec!(5))
731            .trailing_offset(dec!(10))
732            .trailing_offset_type(TrailingOffsetType::Price)
733            .time_in_force(TimeInForce::Gtd)
734            .quantity(Quantity::from(1))
735            .build();
736    }
737
738    #[rstest]
739    fn test_trailing_stop_limit_order_update() {
740        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
741            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
742            .quantity(Quantity::from(10))
743            .price(Price::new(100.0, 2))
744            .trigger_price(Price::new(95.0, 2))
745            .limit_offset(dec!(2.0))
746            .trailing_offset(dec!(1.0))
747            .trailing_offset_type(TrailingOffsetType::Price)
748            .build();
749
750        let mut accepted_order = TestOrderStubs::make_accepted_order(&order);
751
752        let updated_trigger_price = Price::new(90.0, 2);
753        let updated_quantity = Quantity::from(5);
754
755        let event = OrderUpdated {
756            client_order_id: accepted_order.client_order_id(),
757            strategy_id: accepted_order.strategy_id(),
758            trigger_price: Some(updated_trigger_price),
759            quantity: updated_quantity,
760            ..Default::default()
761        };
762
763        accepted_order.apply(OrderEventAny::Updated(event)).unwrap();
764
765        assert_eq!(accepted_order.quantity(), updated_quantity);
766        assert_eq!(accepted_order.trigger_price(), Some(updated_trigger_price));
767    }
768
769    #[rstest]
770    fn test_trailing_stop_limit_order_trigger_instrument_id() {
771        let trigger_instrument_id = InstrumentId::from("ETH-USDT.BINANCE");
772        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
773            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
774            .quantity(Quantity::from(10))
775            .price(Price::new(100.0, 2))
776            .trigger_price(Price::new(95.0, 2))
777            .limit_offset(dec!(2.0))
778            .trailing_offset(dec!(1.0))
779            .trailing_offset_type(TrailingOffsetType::Price)
780            .trigger_instrument_id(trigger_instrument_id)
781            .build();
782
783        assert_eq!(order.trigger_instrument_id(), Some(trigger_instrument_id));
784    }
785
786    #[rstest]
787    fn test_trailing_stop_limit_order_from_order_initialized() {
788        let order_initialized = OrderInitializedBuilder::default()
789            .order_type(OrderType::TrailingStopLimit)
790            .price(Some(Price::new(100.0, 2)))
791            .trigger_price(Some(Price::new(95.0, 2)))
792            .trigger_type(Some(TriggerType::Default))
793            .limit_offset(Some(dec!(2.0)))
794            .trailing_offset(Some(dec!(1.0)))
795            .trailing_offset_type(Some(TrailingOffsetType::Price))
796            .build()
797            .unwrap();
798
799        let order: TrailingStopLimitOrder = order_initialized.clone().into();
800
801        assert_eq!(order.trader_id(), order_initialized.trader_id);
802        assert_eq!(order.strategy_id(), order_initialized.strategy_id);
803        assert_eq!(order.instrument_id(), order_initialized.instrument_id);
804        assert_eq!(order.client_order_id(), order_initialized.client_order_id);
805        assert_eq!(order.order_side(), order_initialized.order_side);
806        assert_eq!(order.quantity(), order_initialized.quantity);
807        assert_eq!(order.price, order_initialized.price.unwrap());
808        assert_eq!(
809            order.trigger_price,
810            order_initialized.trigger_price.unwrap()
811        );
812        assert_eq!(order.trigger_type, order_initialized.trigger_type.unwrap());
813        assert_eq!(order.limit_offset, order_initialized.limit_offset.unwrap());
814        assert_eq!(
815            order.trailing_offset,
816            order_initialized.trailing_offset.unwrap()
817        );
818        assert_eq!(
819            order.trailing_offset_type,
820            order_initialized.trailing_offset_type.unwrap()
821        );
822        assert_eq!(order.time_in_force(), order_initialized.time_in_force);
823        assert_eq!(order.expire_time(), order_initialized.expire_time);
824    }
825}