Skip to main content

nautilus_model/orders/
stop_limit.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, OrderError, check_display_qty, check_time_in_force};
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    types::{Currency, Money, Price, Quantity, quantity::check_positive_quantity},
39};
40
41#[derive(Clone, Debug, Serialize, Deserialize)]
42#[cfg_attr(
43    feature = "python",
44    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
45)]
46#[cfg_attr(
47    feature = "python",
48    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
49)]
50pub struct StopLimitOrder {
51    pub price: Price,
52    pub trigger_price: Price,
53    pub trigger_type: TriggerType,
54    pub expire_time: Option<UnixNanos>,
55    pub is_post_only: bool,
56    pub display_qty: Option<Quantity>,
57    pub trigger_instrument_id: Option<InstrumentId>,
58    pub is_triggered: bool,
59    pub ts_triggered: Option<UnixNanos>,
60    core: OrderCore,
61}
62
63impl StopLimitOrder {
64    /// Creates a new [`StopLimitOrder`] 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    #[expect(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        time_in_force: TimeInForce,
84        expire_time: Option<UnixNanos>,
85        post_only: bool,
86        reduce_only: bool,
87        quote_quantity: bool,
88        display_qty: Option<Quantity>,
89        emulation_trigger: Option<TriggerType>,
90        trigger_instrument_id: Option<InstrumentId>,
91        contingency_type: Option<ContingencyType>,
92        order_list_id: Option<OrderListId>,
93        linked_order_ids: Option<Vec<ClientOrderId>>,
94        parent_order_id: Option<ClientOrderId>,
95        exec_algorithm_id: Option<ExecAlgorithmId>,
96        exec_algorithm_params: Option<IndexMap<Ustr, Ustr>>,
97        exec_spawn_id: Option<ClientOrderId>,
98        tags: Option<Vec<Ustr>>,
99        init_id: UUID4,
100        ts_init: UnixNanos,
101    ) -> Result<Self, OrderError> {
102        check_positive_quantity(quantity, stringify!(quantity))?;
103        check_display_qty(display_qty, quantity)?;
104        check_time_in_force(time_in_force, expire_time)?;
105
106        let init_order = OrderInitialized::new(
107            trader_id,
108            strategy_id,
109            instrument_id,
110            client_order_id,
111            order_side,
112            OrderType::StopLimit,
113            quantity,
114            time_in_force,
115            post_only,
116            reduce_only,
117            quote_quantity,
118            false,
119            init_id,
120            ts_init,
121            ts_init,
122            Some(price),
123            Some(trigger_price),
124            Some(trigger_type),
125            None,
126            None,
127            None,
128            expire_time,
129            display_qty,
130            emulation_trigger,
131            trigger_instrument_id,
132            contingency_type,
133            order_list_id,
134            linked_order_ids,
135            parent_order_id,
136            exec_algorithm_id,
137            exec_algorithm_params,
138            exec_spawn_id,
139            tags,
140        );
141
142        Ok(Self {
143            core: OrderCore::new(init_order),
144            price,
145            trigger_price,
146            trigger_type,
147            expire_time,
148            is_post_only: post_only,
149            display_qty,
150            trigger_instrument_id,
151            is_triggered: false,
152            ts_triggered: None,
153        })
154    }
155
156    /// Creates a new [`StopLimitOrder`] instance.
157    ///
158    /// # Panics
159    ///
160    /// Panics if any order validation fails (see [`StopLimitOrder::new_checked`]).
161    #[expect(clippy::too_many_arguments)]
162    #[must_use]
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        price: Price,
171        trigger_price: Price,
172        trigger_type: TriggerType,
173        time_in_force: TimeInForce,
174        expire_time: Option<UnixNanos>,
175        post_only: bool,
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            price,
200            trigger_price,
201            trigger_type,
202            time_in_force,
203            expire_time,
204            post_only,
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        .unwrap_or_else(|e| panic!("{FAILED}: {e}"))
222    }
223}
224
225impl Deref for StopLimitOrder {
226    type Target = OrderCore;
227    fn deref(&self) -> &Self::Target {
228        &self.core
229    }
230}
231
232impl DerefMut for StopLimitOrder {
233    fn deref_mut(&mut self) -> &mut Self::Target {
234        &mut self.core
235    }
236}
237
238impl PartialEq for StopLimitOrder {
239    fn eq(&self, other: &Self) -> bool {
240        self.client_order_id == other.client_order_id
241    }
242}
243
244impl Order for StopLimitOrder {
245    fn into_any(self) -> OrderAny {
246        OrderAny::StopLimit(self)
247    }
248
249    fn status(&self) -> OrderStatus {
250        self.status
251    }
252
253    fn trader_id(&self) -> TraderId {
254        self.trader_id
255    }
256
257    fn strategy_id(&self) -> StrategyId {
258        self.strategy_id
259    }
260
261    fn instrument_id(&self) -> InstrumentId {
262        self.instrument_id
263    }
264
265    fn symbol(&self) -> Symbol {
266        self.instrument_id.symbol
267    }
268
269    fn venue(&self) -> Venue {
270        self.instrument_id.venue
271    }
272
273    fn client_order_id(&self) -> ClientOrderId {
274        self.client_order_id
275    }
276
277    fn venue_order_id(&self) -> Option<VenueOrderId> {
278        self.venue_order_id
279    }
280
281    fn position_id(&self) -> Option<PositionId> {
282        self.position_id
283    }
284
285    fn account_id(&self) -> Option<AccountId> {
286        self.account_id
287    }
288
289    fn last_trade_id(&self) -> Option<TradeId> {
290        self.last_trade_id
291    }
292
293    fn order_side(&self) -> OrderSide {
294        self.side
295    }
296
297    fn order_type(&self) -> OrderType {
298        self.order_type
299    }
300
301    fn quantity(&self) -> Quantity {
302        self.quantity
303    }
304
305    fn time_in_force(&self) -> TimeInForce {
306        self.time_in_force
307    }
308
309    fn expire_time(&self) -> Option<UnixNanos> {
310        self.expire_time
311    }
312
313    fn price(&self) -> Option<Price> {
314        Some(self.price)
315    }
316
317    fn trigger_price(&self) -> Option<Price> {
318        Some(self.trigger_price)
319    }
320
321    fn trigger_type(&self) -> Option<TriggerType> {
322        Some(self.trigger_type)
323    }
324
325    fn liquidity_side(&self) -> Option<LiquiditySide> {
326        self.liquidity_side
327    }
328
329    fn is_post_only(&self) -> bool {
330        self.is_post_only
331    }
332
333    fn is_reduce_only(&self) -> bool {
334        self.is_reduce_only
335    }
336
337    fn is_quote_quantity(&self) -> bool {
338        self.is_quote_quantity
339    }
340
341    fn has_price(&self) -> bool {
342        true
343    }
344
345    fn display_qty(&self) -> Option<Quantity> {
346        self.display_qty
347    }
348
349    fn limit_offset(&self) -> Option<Decimal> {
350        None
351    }
352
353    fn trailing_offset(&self) -> Option<Decimal> {
354        None
355    }
356
357    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
358        None
359    }
360
361    fn emulation_trigger(&self) -> Option<TriggerType> {
362        self.emulation_trigger
363    }
364
365    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
366        self.trigger_instrument_id
367    }
368
369    fn contingency_type(&self) -> Option<ContingencyType> {
370        self.contingency_type
371    }
372
373    fn order_list_id(&self) -> Option<OrderListId> {
374        self.order_list_id
375    }
376
377    fn linked_order_ids(&self) -> Option<&[ClientOrderId]> {
378        self.linked_order_ids.as_deref()
379    }
380
381    fn parent_order_id(&self) -> Option<ClientOrderId> {
382        self.parent_order_id
383    }
384
385    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
386        self.exec_algorithm_id
387    }
388
389    fn exec_algorithm_params(&self) -> Option<&IndexMap<Ustr, Ustr>> {
390        self.exec_algorithm_params.as_ref()
391    }
392
393    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
394        self.exec_spawn_id
395    }
396
397    fn tags(&self) -> Option<&[Ustr]> {
398        self.tags.as_deref()
399    }
400
401    fn filled_qty(&self) -> Quantity {
402        self.filled_qty
403    }
404
405    fn leaves_qty(&self) -> Quantity {
406        self.leaves_qty
407    }
408
409    fn overfill_qty(&self) -> Quantity {
410        self.overfill_qty
411    }
412
413    fn avg_px(&self) -> Option<f64> {
414        self.avg_px
415    }
416
417    fn slippage(&self) -> Option<f64> {
418        self.slippage
419    }
420
421    fn init_id(&self) -> UUID4 {
422        self.init_id
423    }
424
425    fn ts_init(&self) -> UnixNanos {
426        self.ts_init
427    }
428
429    fn ts_submitted(&self) -> Option<UnixNanos> {
430        self.ts_submitted
431    }
432
433    fn ts_accepted(&self) -> Option<UnixNanos> {
434        self.ts_accepted
435    }
436
437    fn ts_closed(&self) -> Option<UnixNanos> {
438        self.ts_closed
439    }
440
441    fn ts_last(&self) -> UnixNanos {
442        self.ts_last
443    }
444
445    fn events(&self) -> Vec<&OrderEventAny> {
446        self.events.iter().collect()
447    }
448
449    fn venue_order_ids(&self) -> Vec<&VenueOrderId> {
450        self.venue_order_ids.iter().collect()
451    }
452
453    fn commissions(&self) -> &IndexMap<Currency, Money> {
454        &self.commissions
455    }
456
457    fn trade_ids(&self) -> Vec<&TradeId> {
458        self.trade_ids.iter().collect()
459    }
460
461    fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
462        let is_order_filled = matches!(event, OrderEventAny::Filled(_));
463        let is_order_triggered = matches!(event, OrderEventAny::Triggered(_));
464        let ts_event = if is_order_triggered {
465            Some(event.ts_event())
466        } else {
467            None
468        };
469
470        self.core.apply(event.clone())?;
471
472        if let OrderEventAny::Updated(ref event) = event {
473            self.update(event);
474        }
475
476        if is_order_triggered {
477            self.is_triggered = true;
478            self.ts_triggered = ts_event;
479        }
480
481        if is_order_filled {
482            self.core.set_slippage(self.price);
483        }
484
485        Ok(())
486    }
487
488    fn update(&mut self, event: &OrderUpdated) {
489        if let Some(price) = event.price {
490            self.price = price;
491        }
492
493        if let Some(trigger_price) = event.trigger_price {
494            self.trigger_price = trigger_price;
495        }
496
497        self.quantity = event.quantity;
498        self.leaves_qty = self.quantity.saturating_sub(self.filled_qty);
499    }
500
501    fn is_triggered(&self) -> Option<bool> {
502        Some(self.is_triggered)
503    }
504
505    fn set_position_id(&mut self, position_id: Option<PositionId>) {
506        self.position_id = position_id;
507    }
508
509    fn set_quantity(&mut self, quantity: Quantity) {
510        self.quantity = quantity;
511    }
512
513    fn set_leaves_qty(&mut self, leaves_qty: Quantity) {
514        self.leaves_qty = leaves_qty;
515    }
516
517    fn set_emulation_trigger(&mut self, emulation_trigger: Option<TriggerType>) {
518        self.emulation_trigger = emulation_trigger;
519    }
520
521    fn set_is_quote_quantity(&mut self, is_quote_quantity: bool) {
522        self.is_quote_quantity = is_quote_quantity;
523    }
524
525    fn set_liquidity_side(&mut self, liquidity_side: LiquiditySide) {
526        self.liquidity_side = Some(liquidity_side);
527    }
528
529    fn would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
530        self.core.would_reduce_only(side, position_qty)
531    }
532
533    fn previous_status(&self) -> Option<OrderStatus> {
534        self.core.previous_status
535    }
536}
537
538impl From<OrderInitialized> for StopLimitOrder {
539    fn from(event: OrderInitialized) -> Self {
540        Self::new(
541            event.trader_id,
542            event.strategy_id,
543            event.instrument_id,
544            event.client_order_id,
545            event.order_side,
546            event.quantity,
547            event.price.expect("`price` was None for StopLimitOrder"),
548            event
549                .trigger_price
550                .expect("`trigger_price` was None for StopLimitOrder"),
551            event
552                .trigger_type
553                .expect("`trigger_type` was None for StopLimitOrder"),
554            event.time_in_force,
555            event.expire_time,
556            event.post_only,
557            event.reduce_only,
558            event.quote_quantity,
559            event.display_qty,
560            event.emulation_trigger,
561            event.trigger_instrument_id,
562            event.contingency_type,
563            event.order_list_id,
564            event.linked_order_ids,
565            event.parent_order_id,
566            event.exec_algorithm_id,
567            event.exec_algorithm_params,
568            event.exec_spawn_id,
569            event.tags,
570            event.event_id,
571            event.ts_event,
572        )
573    }
574}
575
576impl Display for StopLimitOrder {
577    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
578        write!(
579            f,
580            "StopLimitOrder({} {} {} {} @ {}-STOP[{}] {}-LIMIT {}, status={}, client_order_id={}, venue_order_id={}, position_id={}, tags={})",
581            self.side,
582            self.quantity.to_formatted_string(),
583            self.instrument_id,
584            self.order_type,
585            self.trigger_price,
586            self.trigger_type,
587            self.price,
588            self.time_in_force,
589            self.status,
590            self.client_order_id,
591            self.venue_order_id
592                .map_or("None".to_string(), |venue_order_id| format!(
593                    "{venue_order_id}"
594                )),
595            self.position_id
596                .map_or("None".to_string(), |position_id| format!("{position_id}")),
597            self.tags.clone().map_or("None".to_string(), |tags| tags
598                .iter()
599                .map(|s| s.to_string())
600                .collect::<Vec<String>>()
601                .join(", ")),
602        )
603    }
604}
605
606#[cfg(test)]
607mod tests {
608    use nautilus_core::UnixNanos;
609    use rstest::rstest;
610
611    use super::*;
612    use crate::{
613        enums::{OrderSide, TimeInForce, TriggerType},
614        events::order::spec::OrderInitializedSpec,
615        identifiers::InstrumentId,
616        instruments::{CurrencyPair, stubs::*},
617        orders::{OrderTestBuilder, stubs::TestOrderStubs},
618        types::{Price, Quantity},
619    };
620
621    #[rstest]
622    fn test_initialize(audusd_sim: CurrencyPair) {
623        // ---------------------------------------------------------------------
624        let order = OrderTestBuilder::new(OrderType::StopLimit)
625            .instrument_id(audusd_sim.id)
626            .side(OrderSide::Buy)
627            .trigger_price(Price::from("0.68000"))
628            .price(Price::from("0.68100"))
629            .trigger_type(TriggerType::LastPrice)
630            .quantity(Quantity::from(1))
631            .build();
632
633        assert_eq!(order.trigger_price(), Some(Price::from("0.68000")));
634        assert_eq!(order.price(), Some(Price::from("0.68100")));
635
636        assert_eq!(order.time_in_force(), TimeInForce::Gtc);
637
638        assert_eq!(order.is_triggered(), Some(false));
639        assert_eq!(order.filled_qty(), Quantity::from(0));
640        assert_eq!(order.leaves_qty(), Quantity::from(1));
641
642        assert_eq!(order.display_qty(), None);
643        assert_eq!(order.trigger_instrument_id(), None);
644        assert_eq!(order.order_list_id(), None);
645    }
646
647    #[rstest]
648    fn test_display(audusd_sim: CurrencyPair) {
649        let order = OrderTestBuilder::new(OrderType::MarketToLimit)
650            .instrument_id(audusd_sim.id)
651            .side(OrderSide::Buy)
652            .quantity(Quantity::from(1))
653            .build();
654
655        assert_eq!(
656            order.to_string(),
657            "MarketToLimitOrder(BUY 1 AUD/USD.SIM MARKET_TO_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)"
658        );
659    }
660
661    #[rstest]
662    #[should_panic(expected = "display_qty` may not exceed `quantity")]
663    fn test_display_qty_gt_quantity_err(audusd_sim: CurrencyPair) {
664        let _ = OrderTestBuilder::new(OrderType::StopLimit)
665            .instrument_id(audusd_sim.id)
666            .side(OrderSide::Buy)
667            .trigger_price(Price::from("30300"))
668            .price(Price::from("30100"))
669            .trigger_type(TriggerType::LastPrice)
670            .quantity(Quantity::from(1))
671            .display_qty(Quantity::from(2))
672            .build();
673    }
674
675    #[rstest]
676    #[should_panic(expected = "Quantity must be non-negative")]
677    fn test_display_qty_negative_err(audusd_sim: CurrencyPair) {
678        let _ = OrderTestBuilder::new(OrderType::StopLimit)
679            .instrument_id(audusd_sim.id)
680            .side(OrderSide::Buy)
681            .trigger_price(Price::from("30300"))
682            .price(Price::from("30100"))
683            .trigger_type(TriggerType::LastPrice)
684            .quantity(Quantity::from(1))
685            .display_qty(Quantity::from("-1"))
686            .build();
687    }
688
689    #[rstest]
690    #[should_panic(expected = "expire_time` is required for `GTD` order")]
691    fn test_gtd_without_expire_time_err(audusd_sim: CurrencyPair) {
692        let _ = OrderTestBuilder::new(OrderType::StopLimit)
693            .instrument_id(audusd_sim.id)
694            .side(OrderSide::Buy)
695            .trigger_price(Price::from("30300"))
696            .price(Price::from("30100"))
697            .trigger_type(TriggerType::LastPrice)
698            .time_in_force(TimeInForce::Gtd)
699            .quantity(Quantity::from(1))
700            .build();
701    }
702    #[rstest]
703    fn test_stop_limit_order_update() {
704        // Create and accept a basic stop limit order
705        let order = OrderTestBuilder::new(OrderType::StopLimit)
706            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
707            .quantity(Quantity::from(10))
708            .price(Price::new(100.0, 2))
709            .trigger_price(Price::new(95.0, 2))
710            .build();
711
712        let mut accepted_order = TestOrderStubs::make_accepted_order(&order);
713
714        // Update with new values
715        let updated_price = Price::new(105.0, 2);
716        let updated_trigger_price = Price::new(90.0, 2);
717        let updated_quantity = Quantity::from(5);
718
719        let event = OrderUpdated {
720            client_order_id: accepted_order.client_order_id(),
721            strategy_id: accepted_order.strategy_id(),
722            price: Some(updated_price),
723            trigger_price: Some(updated_trigger_price),
724            quantity: updated_quantity,
725            ..Default::default()
726        };
727
728        accepted_order.apply(OrderEventAny::Updated(event)).unwrap();
729
730        // Verify updates were applied correctly
731        assert_eq!(accepted_order.quantity(), updated_quantity);
732        assert_eq!(accepted_order.price(), Some(updated_price));
733        assert_eq!(accepted_order.trigger_price(), Some(updated_trigger_price));
734    }
735
736    #[rstest]
737    fn test_stop_limit_order_expire_time() {
738        // Create a stop limit order with an expire time
739        let expire_time = UnixNanos::from(1_234_567_890);
740        let order = OrderTestBuilder::new(OrderType::StopLimit)
741            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
742            .quantity(Quantity::from(10))
743            .price(Price::new(100.0, 2))
744            .trigger_price(Price::new(95.0, 2))
745            .expire_time(expire_time)
746            .build();
747
748        // Assert that the expire time is set correctly
749        assert_eq!(order.expire_time(), Some(expire_time));
750    }
751
752    #[rstest]
753    fn test_stop_limit_order_post_only() {
754        // Create a stop limit order with post_only flag set to true
755        let order = OrderTestBuilder::new(OrderType::StopLimit)
756            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
757            .quantity(Quantity::from(10))
758            .price(Price::new(100.0, 2))
759            .trigger_price(Price::new(95.0, 2))
760            .post_only(true)
761            .build();
762
763        // Assert that post_only is set correctly
764        assert!(order.is_post_only());
765    }
766
767    #[rstest]
768    fn test_stop_limit_order_reduce_only() {
769        // Create a stop limit order with reduce_only flag set to true
770        let order = OrderTestBuilder::new(OrderType::StopLimit)
771            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
772            .quantity(Quantity::from(10))
773            .price(Price::new(100.0, 2))
774            .trigger_price(Price::new(95.0, 2))
775            .reduce_only(true)
776            .build();
777
778        // Assert that reduce_only is set correctly
779        assert!(order.is_reduce_only());
780    }
781
782    #[rstest]
783    fn test_stop_limit_order_trigger_instrument_id() {
784        // Create a stop limit order with a trigger instrument ID
785        let trigger_instrument_id = InstrumentId::from("ETH-USDT.BINANCE");
786        let order = OrderTestBuilder::new(OrderType::StopLimit)
787            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
788            .quantity(Quantity::from(10))
789            .price(Price::new(100.0, 2))
790            .trigger_price(Price::new(95.0, 2))
791            .trigger_instrument_id(trigger_instrument_id)
792            .build();
793
794        // Assert that the trigger instrument ID is set correctly
795        assert_eq!(order.trigger_instrument_id(), Some(trigger_instrument_id));
796    }
797
798    #[rstest]
799    fn test_stop_limit_order_would_reduce_only() {
800        // Create a stop limit order with a sell side
801        let order = OrderTestBuilder::new(OrderType::StopLimit)
802            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
803            .side(OrderSide::Sell)
804            .quantity(Quantity::from(10))
805            .price(Price::new(100.0, 2))
806            .trigger_price(Price::new(95.0, 2))
807            .build();
808
809        // Test would_reduce_only functionality
810        assert!(order.would_reduce_only(PositionSide::Long, Quantity::from(15)));
811        assert!(!order.would_reduce_only(PositionSide::Short, Quantity::from(15)));
812        assert!(!order.would_reduce_only(PositionSide::Long, Quantity::from(5)));
813    }
814
815    #[rstest]
816    fn test_stop_limit_order_display_string() {
817        // Create a stop limit order
818        let order = OrderTestBuilder::new(OrderType::StopLimit)
819            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
820            .side(OrderSide::Buy)
821            .quantity(Quantity::from(10))
822            .price(Price::new(100.0, 2))
823            .trigger_price(Price::new(95.0, 2))
824            .client_order_id(ClientOrderId::from("ORDER-001"))
825            .build();
826
827        // Expected string representation - updated to match the actual format
828        let expected = "StopLimitOrder(BUY 10 BTC-USDT.BINANCE STOP_LIMIT @ 95.00-STOP[DEFAULT] 100.00-LIMIT GTC, status=INITIALIZED, client_order_id=ORDER-001, venue_order_id=None, position_id=None, tags=None)";
829
830        // Assert string representations match
831        assert_eq!(order.to_string(), expected);
832        assert_eq!(format!("{order}"), expected);
833    }
834
835    #[rstest]
836    fn test_stop_limit_order_from_order_initialized() {
837        // Create an OrderInitialized event with all required fields for a StopLimitOrder
838        let order_initialized = OrderInitializedSpec::builder()
839            .order_type(OrderType::StopLimit)
840            .quantity(Quantity::from(10))
841            .price(Price::new(100.0, 2))
842            .trigger_price(Price::new(95.0, 2))
843            .trigger_type(TriggerType::Default)
844            .post_only(true)
845            .reduce_only(true)
846            .expire_time(UnixNanos::from(1_234_567_890))
847            .display_qty(Quantity::from(5))
848            .build();
849
850        // Convert the OrderInitialized event into a StopLimitOrder
851        let order: StopLimitOrder = order_initialized.clone().into();
852
853        // Assert essential fields match the OrderInitialized fields
854        assert_eq!(order.trader_id(), order_initialized.trader_id);
855        assert_eq!(order.strategy_id(), order_initialized.strategy_id);
856        assert_eq!(order.instrument_id(), order_initialized.instrument_id);
857        assert_eq!(order.client_order_id(), order_initialized.client_order_id);
858        assert_eq!(order.order_side(), order_initialized.order_side);
859        assert_eq!(order.quantity(), order_initialized.quantity);
860
861        // Assert specific fields for StopLimitOrder
862        assert_eq!(order.price, order_initialized.price.unwrap());
863        assert_eq!(
864            order.trigger_price,
865            order_initialized.trigger_price.unwrap()
866        );
867        assert_eq!(order.trigger_type, order_initialized.trigger_type.unwrap());
868        assert_eq!(order.expire_time(), order_initialized.expire_time);
869        assert_eq!(order.is_post_only(), order_initialized.post_only);
870        assert_eq!(order.is_reduce_only(), order_initialized.reduce_only);
871        assert_eq!(order.display_qty(), order_initialized.display_qty);
872
873        // Verify order type
874        assert_eq!(order.order_type(), OrderType::StopLimit);
875
876        // Verify not triggered by default
877        assert_eq!(order.is_triggered(), Some(false));
878    }
879}