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