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