Skip to main content

nautilus_model/orders/
trailing_stop_market.rs

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