nautilus_model/orders/
limit_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::ops::{Deref, DerefMut};
17
18use indexmap::IndexMap;
19use nautilus_core::{UnixNanos, UUID4};
20use rust_decimal::Decimal;
21use serde::{Deserialize, Serialize};
22use ustr::Ustr;
23
24use super::{
25    any::OrderAny,
26    base::{Order, OrderCore, OrderError},
27};
28use crate::{
29    enums::{
30        ContingencyType, LiquiditySide, OrderSide, OrderStatus, OrderType, TimeInForce,
31        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::{Price, 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 LimitIfTouchedOrder {
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 LimitIfTouchedOrder {
60    /// Creates a new [`LimitIfTouchedOrder`] instance.
61    #[allow(clippy::too_many_arguments)]
62    pub fn new(
63        trader_id: TraderId,
64        strategy_id: StrategyId,
65        instrument_id: InstrumentId,
66        client_order_id: ClientOrderId,
67        order_side: OrderSide,
68        quantity: Quantity,
69        price: Price,
70        trigger_price: Price,
71        trigger_type: TriggerType,
72        time_in_force: TimeInForce,
73        expire_time: Option<UnixNanos>,
74        post_only: bool,
75        reduce_only: bool,
76        quote_quantity: bool,
77        display_qty: Option<Quantity>,
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    ) -> Self {
91        let init_order = OrderInitialized::new(
92            trader_id,
93            strategy_id,
94            instrument_id,
95            client_order_id,
96            order_side,
97            OrderType::LimitIfTouched,
98            quantity,
99            time_in_force,
100            post_only,
101            reduce_only,
102            quote_quantity,
103            false,
104            init_id,
105            ts_init,
106            ts_init,
107            Some(price),
108            Some(trigger_price),
109            Some(trigger_type),
110            None,
111            None,
112            None,
113            expire_time,
114            display_qty,
115            emulation_trigger,
116            trigger_instrument_id,
117            contingency_type,
118            order_list_id,
119            linked_order_ids,
120            parent_order_id,
121            exec_algorithm_id,
122            exec_algorithm_params,
123            exec_spawn_id,
124            tags,
125        );
126        Self {
127            core: OrderCore::new(init_order),
128            price,
129            trigger_price,
130            trigger_type,
131            expire_time,
132            is_post_only: post_only,
133            display_qty,
134            trigger_instrument_id,
135            is_triggered: false,
136            ts_triggered: None,
137        }
138    }
139}
140
141impl Deref for LimitIfTouchedOrder {
142    type Target = OrderCore;
143
144    fn deref(&self) -> &Self::Target {
145        &self.core
146    }
147}
148
149impl DerefMut for LimitIfTouchedOrder {
150    fn deref_mut(&mut self) -> &mut Self::Target {
151        &mut self.core
152    }
153}
154
155impl Order for LimitIfTouchedOrder {
156    fn into_any(self) -> OrderAny {
157        OrderAny::LimitIfTouched(self)
158    }
159
160    fn status(&self) -> OrderStatus {
161        self.status
162    }
163
164    fn trader_id(&self) -> TraderId {
165        self.trader_id
166    }
167
168    fn strategy_id(&self) -> StrategyId {
169        self.strategy_id
170    }
171
172    fn instrument_id(&self) -> InstrumentId {
173        self.instrument_id
174    }
175
176    fn symbol(&self) -> Symbol {
177        self.instrument_id.symbol
178    }
179
180    fn venue(&self) -> Venue {
181        self.instrument_id.venue
182    }
183
184    fn client_order_id(&self) -> ClientOrderId {
185        self.client_order_id
186    }
187
188    fn venue_order_id(&self) -> Option<VenueOrderId> {
189        self.venue_order_id
190    }
191
192    fn position_id(&self) -> Option<PositionId> {
193        self.position_id
194    }
195
196    fn account_id(&self) -> Option<AccountId> {
197        self.account_id
198    }
199
200    fn last_trade_id(&self) -> Option<TradeId> {
201        self.last_trade_id
202    }
203
204    fn side(&self) -> OrderSide {
205        self.side
206    }
207
208    fn order_type(&self) -> OrderType {
209        self.order_type
210    }
211
212    fn quantity(&self) -> Quantity {
213        self.quantity
214    }
215
216    fn time_in_force(&self) -> TimeInForce {
217        self.time_in_force
218    }
219
220    fn expire_time(&self) -> Option<UnixNanos> {
221        self.expire_time
222    }
223
224    fn price(&self) -> Option<Price> {
225        Some(self.price)
226    }
227
228    fn trigger_price(&self) -> Option<Price> {
229        Some(self.trigger_price)
230    }
231
232    fn trigger_type(&self) -> Option<TriggerType> {
233        Some(self.trigger_type)
234    }
235
236    fn liquidity_side(&self) -> Option<LiquiditySide> {
237        self.liquidity_side
238    }
239
240    fn is_post_only(&self) -> bool {
241        self.is_post_only
242    }
243
244    fn is_reduce_only(&self) -> bool {
245        self.is_reduce_only
246    }
247
248    fn is_quote_quantity(&self) -> bool {
249        self.is_quote_quantity
250    }
251
252    fn display_qty(&self) -> Option<Quantity> {
253        self.display_qty
254    }
255
256    fn limit_offset(&self) -> Option<Decimal> {
257        None
258    }
259
260    fn trailing_offset(&self) -> Option<Decimal> {
261        None
262    }
263
264    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
265        None
266    }
267
268    fn emulation_trigger(&self) -> Option<TriggerType> {
269        self.emulation_trigger
270    }
271
272    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
273        self.trigger_instrument_id
274    }
275
276    fn contingency_type(&self) -> Option<ContingencyType> {
277        self.contingency_type
278    }
279
280    fn order_list_id(&self) -> Option<OrderListId> {
281        self.order_list_id
282    }
283
284    fn linked_order_ids(&self) -> Option<&[ClientOrderId]> {
285        self.linked_order_ids.as_deref()
286    }
287
288    fn parent_order_id(&self) -> Option<ClientOrderId> {
289        self.parent_order_id
290    }
291
292    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
293        self.exec_algorithm_id
294    }
295
296    fn exec_algorithm_params(&self) -> Option<&IndexMap<Ustr, Ustr>> {
297        self.exec_algorithm_params.as_ref()
298    }
299
300    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
301        self.exec_spawn_id
302    }
303
304    fn tags(&self) -> Option<&[Ustr]> {
305        self.tags.as_deref()
306    }
307
308    fn filled_qty(&self) -> Quantity {
309        self.filled_qty
310    }
311
312    fn leaves_qty(&self) -> Quantity {
313        self.leaves_qty
314    }
315
316    fn avg_px(&self) -> Option<f64> {
317        self.avg_px
318    }
319
320    fn slippage(&self) -> Option<f64> {
321        self.slippage
322    }
323
324    fn init_id(&self) -> UUID4 {
325        self.init_id
326    }
327
328    fn ts_init(&self) -> UnixNanos {
329        self.ts_init
330    }
331
332    fn ts_last(&self) -> UnixNanos {
333        self.ts_last
334    }
335
336    fn events(&self) -> Vec<&OrderEventAny> {
337        self.events.iter().collect()
338    }
339
340    fn venue_order_ids(&self) -> Vec<&VenueOrderId> {
341        self.venue_order_ids.iter().collect()
342    }
343
344    fn trade_ids(&self) -> Vec<&TradeId> {
345        self.trade_ids.iter().collect()
346    }
347
348    fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
349        if let OrderEventAny::Updated(ref event) = event {
350            self.update(event);
351        };
352        let is_order_filled = matches!(event, OrderEventAny::Filled(_));
353
354        self.core.apply(event)?;
355
356        if is_order_filled {
357            self.core.set_slippage(self.price);
358        };
359
360        Ok(())
361    }
362
363    fn update(&mut self, event: &OrderUpdated) {
364        if let Some(price) = event.price {
365            self.price = price;
366        }
367
368        if let Some(trigger_price) = event.trigger_price {
369            self.trigger_price = trigger_price;
370        }
371
372        self.quantity = event.quantity;
373        self.leaves_qty = self.quantity - self.filled_qty;
374    }
375}
376
377impl From<OrderAny> for LimitIfTouchedOrder {
378    fn from(order: OrderAny) -> LimitIfTouchedOrder {
379        match order {
380            OrderAny::LimitIfTouched(order) => order,
381            _ => {
382                panic!(
383                    "Invalid `OrderAny` not `{}`, was {order:?}",
384                    stringify!(LimitIfTouchedOrder),
385                )
386            }
387        }
388    }
389}
390
391impl From<OrderInitialized> for LimitIfTouchedOrder {
392    fn from(event: OrderInitialized) -> Self {
393        Self::new(
394            event.trader_id,
395            event.strategy_id,
396            event.instrument_id,
397            event.client_order_id,
398            event.order_side,
399            event.quantity,
400            event
401                .price // TODO: Improve this error, model order domain errors
402                .expect("Error initializing order: `price` was `None` for `LimitIfTouchedOrder"),
403            event
404                .trigger_price // TODO: Improve this error, model order domain errors
405                .expect(
406                    "Error initializing order: `trigger_price` was `None` for `LimitIfTouchedOrder",
407                ),
408            event
409                .trigger_type
410                .expect("Error initializing order: `trigger_type` was `None`"),
411            event.time_in_force,
412            event.expire_time,
413            event.post_only,
414            event.reduce_only,
415            event.quote_quantity,
416            event.display_qty,
417            event.emulation_trigger,
418            event.trigger_instrument_id,
419            event.contingency_type,
420            event.order_list_id,
421            event.linked_order_ids,
422            event.parent_order_id,
423            event.exec_algorithm_id,
424            event.exec_algorithm_params,
425            event.exec_spawn_id,
426            event.tags,
427            event.event_id,
428            event.ts_event,
429        )
430    }
431}