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 avg_px(&self) -> Option<f64> {
405        self.avg_px
406    }
407
408    fn slippage(&self) -> Option<f64> {
409        self.slippage
410    }
411
412    fn init_id(&self) -> UUID4 {
413        self.init_id
414    }
415
416    fn ts_init(&self) -> UnixNanos {
417        self.ts_init
418    }
419
420    fn ts_submitted(&self) -> Option<UnixNanos> {
421        self.ts_submitted
422    }
423
424    fn ts_accepted(&self) -> Option<UnixNanos> {
425        self.ts_accepted
426    }
427
428    fn ts_closed(&self) -> Option<UnixNanos> {
429        self.ts_closed
430    }
431
432    fn ts_last(&self) -> UnixNanos {
433        self.ts_last
434    }
435
436    fn events(&self) -> Vec<&OrderEventAny> {
437        self.events.iter().collect()
438    }
439
440    fn venue_order_ids(&self) -> Vec<&VenueOrderId> {
441        self.venue_order_ids.iter().collect()
442    }
443
444    fn commissions(&self) -> &IndexMap<Currency, Money> {
445        &self.commissions
446    }
447
448    fn trade_ids(&self) -> Vec<&TradeId> {
449        self.trade_ids.iter().collect()
450    }
451
452    fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
453        if let OrderEventAny::Updated(ref event) = event {
454            self.update(event);
455        };
456
457        let is_order_filled = matches!(event, OrderEventAny::Filled(_));
458        let is_order_triggered = matches!(event, OrderEventAny::Triggered(_));
459        let ts_event = if is_order_triggered {
460            Some(event.ts_event())
461        } else {
462            None
463        };
464
465        self.core.apply(event)?;
466
467        if is_order_triggered {
468            self.is_triggered = true;
469            self.ts_triggered = ts_event;
470        }
471
472        if is_order_filled {
473            self.core.set_slippage(self.price);
474        };
475
476        Ok(())
477    }
478
479    fn update(&mut self, event: &OrderUpdated) {
480        self.quantity = event.quantity;
481
482        if let Some(price) = event.price {
483            self.price = price;
484        }
485
486        if let Some(trigger_price) = event.trigger_price {
487            self.trigger_price = trigger_price;
488        }
489
490        self.quantity = event.quantity;
491        self.leaves_qty = self.quantity.saturating_sub(self.filled_qty);
492    }
493
494    fn is_triggered(&self) -> Option<bool> {
495        Some(self.is_triggered)
496    }
497
498    fn set_position_id(&mut self, position_id: Option<PositionId>) {
499        self.position_id = position_id;
500    }
501
502    fn set_quantity(&mut self, quantity: Quantity) {
503        self.quantity = quantity;
504    }
505
506    fn set_leaves_qty(&mut self, leaves_qty: Quantity) {
507        self.leaves_qty = leaves_qty;
508    }
509
510    fn set_emulation_trigger(&mut self, emulation_trigger: Option<TriggerType>) {
511        self.emulation_trigger = emulation_trigger;
512    }
513
514    fn set_is_quote_quantity(&mut self, is_quote_quantity: bool) {
515        self.is_quote_quantity = is_quote_quantity;
516    }
517
518    fn set_liquidity_side(&mut self, liquidity_side: LiquiditySide) {
519        self.liquidity_side = Some(liquidity_side)
520    }
521
522    fn would_reduce_only(&self, side: PositionSide, position_qty: Quantity) -> bool {
523        self.core.would_reduce_only(side, position_qty)
524    }
525
526    fn previous_status(&self) -> Option<OrderStatus> {
527        self.core.previous_status
528    }
529}
530
531impl From<OrderInitialized> for StopLimitOrder {
532    fn from(event: OrderInitialized) -> Self {
533        Self::new(
534            event.trader_id,
535            event.strategy_id,
536            event.instrument_id,
537            event.client_order_id,
538            event.order_side,
539            event.quantity,
540            event.price.expect("`price` was None for StopLimitOrder"),
541            event
542                .trigger_price
543                .expect("`trigger_price` was None for StopLimitOrder"),
544            event
545                .trigger_type
546                .expect("`trigger_type` was None for StopLimitOrder"),
547            event.time_in_force,
548            event.expire_time,
549            event.post_only,
550            event.reduce_only,
551            event.quote_quantity,
552            event.display_qty,
553            event.emulation_trigger,
554            event.trigger_instrument_id,
555            event.contingency_type,
556            event.order_list_id,
557            event.linked_order_ids,
558            event.parent_order_id,
559            event.exec_algorithm_id,
560            event.exec_algorithm_params,
561            event.exec_spawn_id,
562            event.tags,
563            event.event_id,
564            event.ts_event,
565        )
566    }
567}
568
569impl Display for StopLimitOrder {
570    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
571        write!(
572            f,
573            "StopLimitOrder({} {} {} {} @ {}-STOP[{}] {}-LIMIT {}, status={}, client_order_id={}, venue_order_id={}, position_id={}, tags={})",
574            self.side,
575            self.quantity.to_formatted_string(),
576            self.instrument_id,
577            self.order_type,
578            self.trigger_price,
579            self.trigger_type,
580            self.price,
581            self.time_in_force,
582            self.status,
583            self.client_order_id,
584            self.venue_order_id
585                .map_or("None".to_string(), |venue_order_id| format!(
586                    "{venue_order_id}"
587                )),
588            self.position_id
589                .map_or("None".to_string(), |position_id| format!("{position_id}")),
590            self.tags.clone().map_or("None".to_string(), |tags| tags
591                .iter()
592                .map(|s| s.to_string())
593                .collect::<Vec<String>>()
594                .join(", ")),
595        )
596    }
597}
598
599#[cfg(test)]
600mod tests {
601    use nautilus_core::UnixNanos;
602    use rstest::rstest;
603
604    use super::*;
605    use crate::{
606        enums::{OrderSide, TimeInForce, TriggerType},
607        events::order::initialized::OrderInitializedBuilder,
608        identifiers::InstrumentId,
609        instruments::{CurrencyPair, stubs::*},
610        orders::{OrderTestBuilder, stubs::TestOrderStubs},
611        types::{Price, Quantity},
612    };
613
614    #[rstest]
615    fn test_initialize(_audusd_sim: CurrencyPair) {
616        // ---------------------------------------------------------------------
617        let order = OrderTestBuilder::new(OrderType::StopLimit)
618            .instrument_id(_audusd_sim.id)
619            .side(OrderSide::Buy)
620            .trigger_price(Price::from("0.68000"))
621            .price(Price::from("0.68100"))
622            .trigger_type(TriggerType::LastPrice)
623            .quantity(Quantity::from(1))
624            .build();
625
626        assert_eq!(order.trigger_price(), Some(Price::from("0.68000")));
627        assert_eq!(order.price(), Some(Price::from("0.68100")));
628
629        assert_eq!(order.time_in_force(), TimeInForce::Gtc);
630
631        assert_eq!(order.is_triggered(), Some(false));
632        assert_eq!(order.filled_qty(), Quantity::from(0));
633        assert_eq!(order.leaves_qty(), Quantity::from(1));
634
635        assert_eq!(order.display_qty(), None);
636        assert_eq!(order.trigger_instrument_id(), None);
637        assert_eq!(order.order_list_id(), None);
638    }
639
640    #[rstest]
641    fn test_display(audusd_sim: CurrencyPair) {
642        let order = OrderTestBuilder::new(OrderType::MarketToLimit)
643            .instrument_id(audusd_sim.id)
644            .side(OrderSide::Buy)
645            .quantity(Quantity::from(1))
646            .build();
647
648        assert_eq!(
649            order.to_string(),
650            "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)"
651        );
652    }
653
654    #[rstest]
655    #[should_panic]
656    fn test_display_qty_gt_quantity_err(audusd_sim: CurrencyPair) {
657        OrderTestBuilder::new(OrderType::StopLimit)
658            .instrument_id(audusd_sim.id)
659            .side(OrderSide::Buy)
660            .trigger_price(Price::from("30300"))
661            .price(Price::from("30100"))
662            .trigger_type(TriggerType::LastPrice)
663            .quantity(Quantity::from(1))
664            .display_qty(Quantity::from(2))
665            .build();
666    }
667
668    #[rstest]
669    #[should_panic]
670    fn test_display_qty_negative_err(audusd_sim: CurrencyPair) {
671        OrderTestBuilder::new(OrderType::StopLimit)
672            .instrument_id(audusd_sim.id)
673            .side(OrderSide::Buy)
674            .trigger_price(Price::from("30300"))
675            .price(Price::from("30100"))
676            .trigger_type(TriggerType::LastPrice)
677            .quantity(Quantity::from(1))
678            .display_qty(Quantity::from("-1"))
679            .build();
680    }
681
682    #[rstest]
683    #[should_panic]
684    fn test_gtd_without_expire_time_err(audusd_sim: CurrencyPair) {
685        OrderTestBuilder::new(OrderType::StopLimit)
686            .instrument_id(audusd_sim.id)
687            .side(OrderSide::Buy)
688            .trigger_price(Price::from("30300"))
689            .price(Price::from("30100"))
690            .trigger_type(TriggerType::LastPrice)
691            .time_in_force(TimeInForce::Gtd)
692            .quantity(Quantity::from(1))
693            .build();
694    }
695    #[rstest]
696    fn test_stop_limit_order_update() {
697        // Create and accept a basic stop limit order
698        let order = OrderTestBuilder::new(OrderType::StopLimit)
699            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
700            .quantity(Quantity::from(10))
701            .price(Price::new(100.0, 2))
702            .trigger_price(Price::new(95.0, 2))
703            .build();
704
705        let mut accepted_order = TestOrderStubs::make_accepted_order(&order);
706
707        // Update with new values
708        let updated_price = Price::new(105.0, 2);
709        let updated_trigger_price = Price::new(90.0, 2);
710        let updated_quantity = Quantity::from(5);
711
712        let event = OrderUpdated {
713            client_order_id: accepted_order.client_order_id(),
714            strategy_id: accepted_order.strategy_id(),
715            price: Some(updated_price),
716            trigger_price: Some(updated_trigger_price),
717            quantity: updated_quantity,
718            ..Default::default()
719        };
720
721        accepted_order.apply(OrderEventAny::Updated(event)).unwrap();
722
723        // Verify updates were applied correctly
724        assert_eq!(accepted_order.quantity(), updated_quantity);
725        assert_eq!(accepted_order.price(), Some(updated_price));
726        assert_eq!(accepted_order.trigger_price(), Some(updated_trigger_price));
727    }
728
729    #[rstest]
730    fn test_stop_limit_order_expire_time() {
731        // Create a stop limit order with an expire time
732        let expire_time = UnixNanos::from(1234567890);
733        let order = OrderTestBuilder::new(OrderType::StopLimit)
734            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
735            .quantity(Quantity::from(10))
736            .price(Price::new(100.0, 2))
737            .trigger_price(Price::new(95.0, 2))
738            .expire_time(expire_time)
739            .build();
740
741        // Assert that the expire time is set correctly
742        assert_eq!(order.expire_time(), Some(expire_time));
743    }
744
745    #[rstest]
746    fn test_stop_limit_order_post_only() {
747        // Create a stop limit order with post_only flag set to true
748        let order = OrderTestBuilder::new(OrderType::StopLimit)
749            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
750            .quantity(Quantity::from(10))
751            .price(Price::new(100.0, 2))
752            .trigger_price(Price::new(95.0, 2))
753            .post_only(true)
754            .build();
755
756        // Assert that post_only is set correctly
757        assert!(order.is_post_only());
758    }
759
760    #[rstest]
761    fn test_stop_limit_order_reduce_only() {
762        // Create a stop limit order with reduce_only flag set to true
763        let order = OrderTestBuilder::new(OrderType::StopLimit)
764            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
765            .quantity(Quantity::from(10))
766            .price(Price::new(100.0, 2))
767            .trigger_price(Price::new(95.0, 2))
768            .reduce_only(true)
769            .build();
770
771        // Assert that reduce_only is set correctly
772        assert!(order.is_reduce_only());
773    }
774
775    #[rstest]
776    fn test_stop_limit_order_trigger_instrument_id() {
777        // Create a stop limit order with a trigger instrument ID
778        let trigger_instrument_id = InstrumentId::from("ETH-USDT.BINANCE");
779        let order = OrderTestBuilder::new(OrderType::StopLimit)
780            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
781            .quantity(Quantity::from(10))
782            .price(Price::new(100.0, 2))
783            .trigger_price(Price::new(95.0, 2))
784            .trigger_instrument_id(trigger_instrument_id)
785            .build();
786
787        // Assert that the trigger instrument ID is set correctly
788        assert_eq!(order.trigger_instrument_id(), Some(trigger_instrument_id));
789    }
790
791    #[rstest]
792    fn test_stop_limit_order_would_reduce_only() {
793        // Create a stop limit order with a sell side
794        let order = OrderTestBuilder::new(OrderType::StopLimit)
795            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
796            .side(OrderSide::Sell)
797            .quantity(Quantity::from(10))
798            .price(Price::new(100.0, 2))
799            .trigger_price(Price::new(95.0, 2))
800            .build();
801
802        // Test would_reduce_only functionality
803        assert!(order.would_reduce_only(PositionSide::Long, Quantity::from(15)));
804        assert!(!order.would_reduce_only(PositionSide::Short, Quantity::from(15)));
805        assert!(!order.would_reduce_only(PositionSide::Long, Quantity::from(5)));
806    }
807
808    #[rstest]
809    fn test_stop_limit_order_display_string() {
810        // Create a stop limit order
811        let order = OrderTestBuilder::new(OrderType::StopLimit)
812            .instrument_id(InstrumentId::from("BTC-USDT.BINANCE"))
813            .side(OrderSide::Buy)
814            .quantity(Quantity::from(10))
815            .price(Price::new(100.0, 2))
816            .trigger_price(Price::new(95.0, 2))
817            .client_order_id(ClientOrderId::from("ORDER-001"))
818            .build();
819
820        // Expected string representation - updated to match the actual format
821        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)";
822
823        // Assert string representations match
824        assert_eq!(order.to_string(), expected);
825        assert_eq!(format!("{order}"), expected);
826    }
827
828    #[rstest]
829    fn test_stop_limit_order_from_order_initialized() {
830        // Create an OrderInitialized event with all required fields for a StopLimitOrder
831        let order_initialized = OrderInitializedBuilder::default()
832            .order_type(OrderType::StopLimit)
833            .quantity(Quantity::from(10))
834            .price(Some(Price::new(100.0, 2)))
835            .trigger_price(Some(Price::new(95.0, 2)))
836            .trigger_type(Some(TriggerType::Default))
837            .post_only(true)
838            .reduce_only(true)
839            .expire_time(Some(UnixNanos::from(1234567890)))
840            .display_qty(Some(Quantity::from(5)))
841            .build()
842            .unwrap();
843
844        // Convert the OrderInitialized event into a StopLimitOrder
845        let order: StopLimitOrder = order_initialized.clone().into();
846
847        // Assert essential fields match the OrderInitialized fields
848        assert_eq!(order.trader_id(), order_initialized.trader_id);
849        assert_eq!(order.strategy_id(), order_initialized.strategy_id);
850        assert_eq!(order.instrument_id(), order_initialized.instrument_id);
851        assert_eq!(order.client_order_id(), order_initialized.client_order_id);
852        assert_eq!(order.order_side(), order_initialized.order_side);
853        assert_eq!(order.quantity(), order_initialized.quantity);
854
855        // Assert specific fields for StopLimitOrder
856        assert_eq!(order.price, order_initialized.price.unwrap());
857        assert_eq!(
858            order.trigger_price,
859            order_initialized.trigger_price.unwrap()
860        );
861        assert_eq!(order.trigger_type, order_initialized.trigger_type.unwrap());
862        assert_eq!(order.expire_time(), order_initialized.expire_time);
863        assert_eq!(order.is_post_only(), order_initialized.post_only);
864        assert_eq!(order.is_reduce_only(), order_initialized.reduce_only);
865        assert_eq!(order.display_qty(), order_initialized.display_qty);
866
867        // Verify order type
868        assert_eq!(order.order_type(), OrderType::StopLimit);
869
870        // Verify not triggered by default
871        assert_eq!(order.is_triggered(), Some(false));
872    }
873}