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