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