nautilus_model/orders/
stop_limit.rs

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