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