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 avg_px(&self) -> Option<f64> {
431        self.avg_px
432    }
433
434    fn slippage(&self) -> Option<f64> {
435        self.slippage
436    }
437
438    fn init_id(&self) -> UUID4 {
439        self.init_id
440    }
441
442    fn ts_init(&self) -> UnixNanos {
443        self.ts_init
444    }
445
446    fn ts_submitted(&self) -> Option<UnixNanos> {
447        self.ts_submitted
448    }
449
450    fn ts_accepted(&self) -> Option<UnixNanos> {
451        self.ts_accepted
452    }
453
454    fn ts_closed(&self) -> Option<UnixNanos> {
455        self.ts_closed
456    }
457
458    fn ts_last(&self) -> UnixNanos {
459        self.ts_last
460    }
461
462    fn events(&self) -> Vec<&OrderEventAny> {
463        self.events.iter().collect()
464    }
465
466    fn venue_order_ids(&self) -> Vec<&VenueOrderId> {
467        self.venue_order_ids.iter().collect()
468    }
469
470    fn trade_ids(&self) -> Vec<&TradeId> {
471        self.trade_ids.iter().collect()
472    }
473
474    fn commissions(&self) -> &IndexMap<Currency, Money> {
475        &self.commissions
476    }
477
478    fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
479        if let OrderEventAny::Updated(ref event) = event {
480            self.update(event);
481        }
482
483        let is_order_filled = matches!(event, OrderEventAny::Filled(_));
484        let is_order_triggered = matches!(event, OrderEventAny::Triggered(_));
485        let ts_event = if is_order_triggered {
486            Some(event.ts_event())
487        } else {
488            None
489        };
490
491        self.core.apply(event)?;
492
493        if is_order_triggered {
494            self.is_triggered = true;
495            self.ts_triggered = ts_event;
496        }
497
498        if is_order_filled {
499            self.core.set_slippage(self.price);
500        }
501
502        Ok(())
503    }
504
505    fn update(&mut self, event: &OrderUpdated) {
506        if let Some(price) = event.price {
507            self.price = price;
508        }
509        if let Some(trigger_price) = event.trigger_price {
510            self.trigger_price = trigger_price;
511        }
512        self.quantity = event.quantity;
513        self.leaves_qty = self.quantity.saturating_sub(self.filled_qty);
514    }
515
516    fn is_triggered(&self) -> Option<bool> {
517        Some(self.is_triggered)
518    }
519
520    fn set_position_id(&mut self, position_id: Option<PositionId>) {
521        self.position_id = position_id;
522    }
523
524    fn set_quantity(&mut self, quantity: Quantity) {
525        self.quantity = quantity;
526    }
527
528    fn set_leaves_qty(&mut self, leaves_qty: Quantity) {
529        self.leaves_qty = leaves_qty;
530    }
531
532    fn set_emulation_trigger(&mut self, emulation_trigger: Option<TriggerType>) {
533        self.emulation_trigger = emulation_trigger;
534    }
535
536    fn set_is_quote_quantity(&mut self, is_quote_quantity: bool) {
537        self.is_quote_quantity = is_quote_quantity;
538    }
539
540    fn set_liquidity_side(&mut self, liquidity_side: LiquiditySide) {
541        self.liquidity_side = Some(liquidity_side)
542    }
543
544    fn would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
545        self.core.would_reduce_only(side, position_qty)
546    }
547
548    fn previous_status(&self) -> Option<OrderStatus> {
549        self.core.previous_status
550    }
551}
552
553impl Display for TrailingStopLimitOrder {
554    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
555        write!(
556            f,
557            "TrailingStopLimitOrder({} {} {} {} {}, status={}, client_order_id={}, venue_order_id={}, position_id={}, exec_algorithm_id={}, exec_spawn_id={}, tags={:?}, activation_price={:?}, is_activated={})",
558            self.side,
559            self.quantity.to_formatted_string(),
560            self.instrument_id,
561            self.order_type,
562            self.time_in_force,
563            self.status,
564            self.client_order_id,
565            self.venue_order_id
566                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
567            self.position_id
568                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
569            self.exec_algorithm_id
570                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
571            self.exec_spawn_id
572                .map_or_else(|| "None".to_string(), |id| format!("{id}")),
573            self.tags,
574            self.activation_price,
575            self.is_activated
576        )
577    }
578}
579
580impl From<OrderInitialized> for TrailingStopLimitOrder {
581    fn from(event: OrderInitialized) -> Self {
582        Self::new(
583            event.trader_id,
584            event.strategy_id,
585            event.instrument_id,
586            event.client_order_id,
587            event.order_side,
588            event.quantity,
589            event
590                .price
591                .expect("Error initializing order: price is None"),
592            event
593                .trigger_price
594                .expect("Error initializing order: trigger_price is None"),
595            event
596                .trigger_type
597                .expect("Error initializing order: trigger_type is None"),
598            event.limit_offset.unwrap(),
599            event.trailing_offset.unwrap(),
600            event.trailing_offset_type.unwrap(),
601            event.time_in_force,
602            event.expire_time,
603            event.post_only,
604            event.reduce_only,
605            event.quote_quantity,
606            event.display_qty,
607            event.emulation_trigger,
608            event.trigger_instrument_id,
609            event.contingency_type,
610            event.order_list_id,
611            event.linked_order_ids,
612            event.parent_order_id,
613            event.exec_algorithm_id,
614            event.exec_algorithm_params,
615            event.exec_spawn_id,
616            event.tags,
617            event.event_id,
618            event.ts_event,
619        )
620    }
621}
622
623////////////////////////////////////////////////////////////////////////////////
624//  Tests
625////////////////////////////////////////////////////////////////////////////////
626#[cfg(test)]
627mod tests {
628    use rstest::rstest;
629    use rust_decimal_macros::dec;
630
631    use super::*;
632    use crate::{
633        enums::{TimeInForce, TrailingOffsetType, TriggerType},
634        events::order::initialized::OrderInitializedBuilder,
635        identifiers::InstrumentId,
636        instruments::{CurrencyPair, stubs::*},
637        orders::{OrderTestBuilder, stubs::TestOrderStubs},
638        types::{Price, Quantity},
639    };
640
641    #[rstest]
642    fn test_initialize(_audusd_sim: CurrencyPair) {
643        // Create and accept a basic trailing stop limit order
644        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
645            .instrument_id(_audusd_sim.id)
646            .side(OrderSide::Buy)
647            .price(Price::from("0.67500"))
648            .limit_offset(dec!(5))
649            .trigger_price(Price::from("0.68000"))
650            .trailing_offset(dec!(10))
651            .quantity(Quantity::from(1))
652            .build();
653
654        assert_eq!(order.trigger_price(), Some(Price::from("0.68000")));
655        assert_eq!(order.price(), Some(Price::from("0.67500")));
656        assert_eq!(order.time_in_force(), TimeInForce::Gtc);
657        assert_eq!(order.is_triggered(), Some(false));
658        assert_eq!(order.filled_qty(), Quantity::from(0));
659        assert_eq!(order.leaves_qty(), Quantity::from(1));
660        assert_eq!(order.display_qty(), None);
661        assert_eq!(order.trigger_instrument_id(), None);
662        assert_eq!(order.order_list_id(), None);
663    }
664
665    #[rstest]
666    fn test_display(_audusd_sim: CurrencyPair) {
667        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
668            .instrument_id(_audusd_sim.id)
669            .side(OrderSide::Buy)
670            .price(Price::from("0.67500"))
671            .trigger_price(Price::from("0.68000"))
672            .trigger_type(TriggerType::LastPrice)
673            .limit_offset(dec!(5))
674            .trailing_offset(dec!(10))
675            .trailing_offset_type(TrailingOffsetType::Price)
676            .quantity(Quantity::from(1))
677            .build();
678
679        assert_eq!(
680            order.to_string(),
681            "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)"
682        );
683    }
684
685    #[rstest]
686    #[should_panic(expected = "Condition failed: `display_qty` may not exceed `quantity`")]
687    fn test_display_qty_gt_quantity_err(audusd_sim: CurrencyPair) {
688        OrderTestBuilder::new(OrderType::TrailingStopLimit)
689            .instrument_id(audusd_sim.id)
690            .side(OrderSide::Buy)
691            .price(Price::from("0.67500"))
692            .trigger_price(Price::from("0.68000"))
693            .trigger_type(TriggerType::LastPrice)
694            .limit_offset(dec!(5))
695            .trailing_offset(dec!(10))
696            .trailing_offset_type(TrailingOffsetType::Price)
697            .quantity(Quantity::from(1))
698            .display_qty(Quantity::from(2))
699            .build();
700    }
701
702    #[rstest]
703    #[should_panic(
704        expected = "Condition failed: invalid `Quantity` for 'quantity' not positive, was 0"
705    )]
706    fn test_quantity_zero_err(audusd_sim: CurrencyPair) {
707        OrderTestBuilder::new(OrderType::TrailingStopLimit)
708            .instrument_id(audusd_sim.id)
709            .side(OrderSide::Buy)
710            .price(Price::from("0.67500"))
711            .trigger_price(Price::from("0.68000"))
712            .trigger_type(TriggerType::LastPrice)
713            .limit_offset(dec!(5))
714            .trailing_offset(dec!(10))
715            .trailing_offset_type(TrailingOffsetType::Price)
716            .quantity(Quantity::from(0))
717            .build();
718    }
719
720    #[rstest]
721    #[should_panic(expected = "Condition failed: `expire_time` is required for `GTD` order")]
722    fn test_gtd_without_expire_err(audusd_sim: CurrencyPair) {
723        OrderTestBuilder::new(OrderType::TrailingStopLimit)
724            .instrument_id(audusd_sim.id)
725            .side(OrderSide::Buy)
726            .price(Price::from("0.67500"))
727            .trigger_price(Price::from("0.68000"))
728            .trigger_type(TriggerType::LastPrice)
729            .limit_offset(dec!(5))
730            .trailing_offset(dec!(10))
731            .trailing_offset_type(TrailingOffsetType::Price)
732            .time_in_force(TimeInForce::Gtd)
733            .quantity(Quantity::from(1))
734            .build();
735    }
736
737    #[rstest]
738    fn test_trailing_stop_limit_order_update() {
739        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
740            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
741            .quantity(Quantity::from(10))
742            .price(Price::new(100.0, 2))
743            .trigger_price(Price::new(95.0, 2))
744            .limit_offset(dec!(2.0))
745            .trailing_offset(dec!(1.0))
746            .trailing_offset_type(TrailingOffsetType::Price)
747            .build();
748
749        let mut accepted_order = TestOrderStubs::make_accepted_order(&order);
750
751        let updated_trigger_price = Price::new(90.0, 2);
752        let updated_quantity = Quantity::from(5);
753
754        let event = OrderUpdated {
755            client_order_id: accepted_order.client_order_id(),
756            strategy_id: accepted_order.strategy_id(),
757            trigger_price: Some(updated_trigger_price),
758            quantity: updated_quantity,
759            ..Default::default()
760        };
761
762        accepted_order.apply(OrderEventAny::Updated(event)).unwrap();
763
764        assert_eq!(accepted_order.quantity(), updated_quantity);
765        assert_eq!(accepted_order.trigger_price(), Some(updated_trigger_price));
766    }
767
768    #[rstest]
769    fn test_trailing_stop_limit_order_trigger_instrument_id() {
770        let trigger_instrument_id = InstrumentId::from("ETH-USDT.BINANCE");
771        let order = OrderTestBuilder::new(OrderType::TrailingStopLimit)
772            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
773            .quantity(Quantity::from(10))
774            .price(Price::new(100.0, 2))
775            .trigger_price(Price::new(95.0, 2))
776            .limit_offset(dec!(2.0))
777            .trailing_offset(dec!(1.0))
778            .trailing_offset_type(TrailingOffsetType::Price)
779            .trigger_instrument_id(trigger_instrument_id)
780            .build();
781
782        assert_eq!(order.trigger_instrument_id(), Some(trigger_instrument_id));
783    }
784
785    #[rstest]
786    fn test_trailing_stop_limit_order_from_order_initialized() {
787        let order_initialized = OrderInitializedBuilder::default()
788            .order_type(OrderType::TrailingStopLimit)
789            .price(Some(Price::new(100.0, 2)))
790            .trigger_price(Some(Price::new(95.0, 2)))
791            .trigger_type(Some(TriggerType::Default))
792            .limit_offset(Some(dec!(2.0)))
793            .trailing_offset(Some(dec!(1.0)))
794            .trailing_offset_type(Some(TrailingOffsetType::Price))
795            .build()
796            .unwrap();
797
798        let order: TrailingStopLimitOrder = order_initialized.clone().into();
799
800        assert_eq!(order.trader_id(), order_initialized.trader_id);
801        assert_eq!(order.strategy_id(), order_initialized.strategy_id);
802        assert_eq!(order.instrument_id(), order_initialized.instrument_id);
803        assert_eq!(order.client_order_id(), order_initialized.client_order_id);
804        assert_eq!(order.order_side(), order_initialized.order_side);
805        assert_eq!(order.quantity(), order_initialized.quantity);
806        assert_eq!(order.price, order_initialized.price.unwrap());
807        assert_eq!(
808            order.trigger_price,
809            order_initialized.trigger_price.unwrap()
810        );
811        assert_eq!(order.trigger_type, order_initialized.trigger_type.unwrap());
812        assert_eq!(order.limit_offset, order_initialized.limit_offset.unwrap());
813        assert_eq!(
814            order.trailing_offset,
815            order_initialized.trailing_offset.unwrap()
816        );
817        assert_eq!(
818            order.trailing_offset_type,
819            order_initialized.trailing_offset_type.unwrap()
820        );
821        assert_eq!(order.time_in_force(), order_initialized.time_in_force);
822        assert_eq!(order.expire_time(), order_initialized.expire_time);
823    }
824}